当前位置:网站首页>用户交互、格式化输出、基本运算符
用户交互、格式化输出、基本运算符
2022-04-21 15:29:00 【weixin_46837396】
一 程序与用户交互
用户交互
''' 1.获取用户数据 username = input('请输入名字>>>') print(type(username)) 请输入名字>>>jyh jyh <class 'str'> # input获取到的用户都是字符串类型 2.输出内部数据 print(username) 编写方式: 1.先写print,括号内写数据 2. 先写需要打印的东西,再写句点符,输入print 按tab键 '''
括号内一次性打印多个数据,逗号隔开即可
print('hello word', 123, 'jason')
hello word 123 jason
格式化输出
提前定义好模板,再进行格式化输出。
''' (1)什么是格式化输出? 把一段字符串里面的某些内容替换掉之后再输出,就是格式化输出。 (2)为什么要格式化输出? 我们经常会输出具有某种固定格式的内容,比如:'亲爱的xxx你好!你xxx月的话费是xxx,余额是xxx',我们需要做的就是将xxx替换为具体的内容。 (3)如何格式化输出? 这就用到了占位符,如:%s、%d: %s:可以给任意数据占位 %d: 只能给数字占位 如果一个占位符,统一携程这样 my name is %s %('jyh',) #推荐建议 my name is %s %'jyh' #不推荐记忆 %s测试1: t = '亲爱的%s你好!你%s月的话费是%s,余额是%s' print(t % ('jyh', 2, 38, 80)) print(t % ('elaian', 7, 160, 97)) print(t % ('tom', 5, 68, 43)) 亲爱的jyh你好!你2月的话费是38,余额是80 亲爱的elaian你好!你7月的话费是160,余额是97 亲爱的tom你好!你5月的话费是68,余额是43 %s测试2: username = input('username>>>:') age = input('age>>>:') print('my name is %s my age is %s' % ('jyh', 25)) username>>>:jyh age>>>:18 my name is jyh my age is 25 %d测试1: print('my name is %d my age is %d' % (19, 18)) my name is 19 my age is 18 %d测试2: print('%08d' % (2222)) print('%08d' % (111)) print('%08d' % (4455567899)) 00002222 00000111 4455567899 08d意思:一共8位,如果不够用0来凑。如果超了也就超了。 补充:再pycharm中: 左键1次:插入 左键两次:选中一个单词 左键三次:选中一整行 ctrl +d 复制一整行 '''
二 基本运算符
算数运算符
''' 1.加减乘除 + - * \ 等于(==) 2.增量赋值 x += 100 # 等于x = x + 100 x -= 100 # 等于x = x - 100 x *= 100 # 等于x = x * 100 x \= 100 # 等于x = x \ 100 整除(\\) 取余(%) x = 10 int(x // 3) print(x % 3) 3 1 ''''
链式赋值
x = 100
y = x
z = x
简化
x = y = z = 100
交叉赋值
''' m = 10 n = 999 1.中间变量的方法 # 让m指向n的值,让n指向m的值 tmp = m m = n n = tmp print(m, n) 2.交叉赋值 m, n = n, m print(m, n) '''
解压赋值
num = [11, 22, 33, 44, 55, 66, 77, 88, 99]
a, b, c, d, e, f, g, h, i = num
print(a, b, c, d, e, f, g, h, i)
11 22 33 44 55 66 77 88 99
''' 正常情况下,左右两边的变量名和值个数要相等 当然这个个数可以打破 '''
a, b, *_ = num
print(a, b, _)
11 22 [33, 44, 55, 66, 77, 88, 99]
''' 如果因为某个语法,必须使用变量名,但是这个变量我用不到 ,就用下划线即可 '''
逻辑运算符
and 与
连接多个条件必须都满足
''' >>> 2 > 1 and 1 != 1 and True and 3 > 2 # 判断完第二个条件,就立即结束,得的最终结果为False False '''
or 或
连接多个条件,有一个条件满足即可
''' >>> 2 > 1 or 1 != 1 or True or 3 > 2 # 判断完第一个条件,就立即结束,得的最终结果为True True '''
not 非
将条件翻转 Ture----False
False ---Ture
''' # and、or、not三者如果混用时,是存在优先级之分的,但在日常开发中我们无需记忆优先级,应该使用()来区分优先级、提升程序的可读性 >>> (3>4 and 4>3) or ((1==3 and 'x' == 'x') or 3 >3) False '''
身份、成员运算符
1.成员运算
:判断个体是不是在某个集体内
''' name_list = ['jyh', 'tom', 'jenay', 'elaian'] print('jyh' in name_list) #True print('jyh' not in name_list) # False '''
2.身份运算
is:两个数据的内存地址是否一致
==: 两个数据的值是否一致
''' a = ['json', 'tom', 'elaian'] b = ['json', 'tom', 'elaian'] print(a == b) print(a is b) True False a = 99999 b = 99999 print(a == b) print(a is b) True True 这里简单来说有一个内存优化机制 '''
内存相同 值一定相同
值相同 内存不一定相同
版权声明
本文为[weixin_46837396]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46837396/article/details/124263805
边栏推荐
- In the SSM project, the test method has been running in circles without stopping
- AcWing 1854. Promotion count (Analog)
- EL&JSTL
- How to set start date query statement in SQL Server
- Universal navigation: a concise and practical comprehensive navigation website
- 78页数字孪生+智慧楼宇解决方案
- BetterScroll源码阅读顺便学习TypeScript
- [binary search - simple] 367 Effective complete square
- Solution de transformation de mot de données de 111 pages Fine Chemical Co., Ltd.
- Acwing 1775 Lost cattle (simulated)
猜你喜欢

Solution de transformation de mot de données de 111 pages Fine Chemical Co., Ltd.

How to apply for corporate email? How to register email with mobile phone number?

AcWing 1854. 晋升计数(模拟)

红队技术-父进程伪装(MITRE ATT&CK框架:T1134)

AcWing 1854. Promotion count (Analog)

终极套娃 2.0|云原生 PaaS 平台的可观测性实践分享

【时序】Reformer:局部敏感哈希(LSH)实现高效 Transformer 论文笔记

【unity笔记】L3Unity Shader学习开始
![[Unity] error CS0433: The type ‘Task‘ exists in both Unity. Tasks,....](/img/17/3a0b2e52336bc702ff3b38420e9e6a.png)
[Unity] error CS0433: The type ‘Task‘ exists in both Unity. Tasks,....

Introduction to openlayers (II)
随机推荐
JUC并发学习笔记
【unity笔记】L3Unity Shader学习开始
Login refactoring notes
What mailbox do foreign trade companies usually use and how to send e-mail in groups?
Web.xml文件详解
AcWing1800. Do not do the last (enumeration)
GLASS:用于子图表示学习的 GNN 标签技巧
Huawei power PON distribution network solution
AcWing 1812. Square pasture (enumeration)
[binary search - medium] 1498 Number of subsequences satisfying the condition
SMTP协议解读以及如何使用SMTP协议发送电子邮件
返璞归真,多方安全计算要回归到“安全”的本源考虑
全国降雨侵蚀力因子R值
Mysql
JDBC和数据库连接池
Take it easy, just talk about the soft test
MySQL
AcWing 1812. 方形牧场(枚举)
Mysql
What is an email address? Easy to use email registration application