1、与用户交互
分为:输入、输出
输入: 人向计算机输入数据
输出:计算机向人输出数据
在python2 中,input(输入)时一定要声明你输入的数据类型
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
>>> input(">>:") >>:sean Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'sean' is not defined >>> input(">>:") >>:"sean" 'sean' >>> input(">>:") >>:1 1 >>> input(">>:") >>:[1,2] [1, 2] >>> ----------------------------------------------------- >>> raw_input(">>:") >>:sean 'sean' >>> raw_input(">>:") >>:12 '12'
在python3中,input #接收用户信息时,无论用户输入的是什么类型,最终返回的一定是字符串
name = input("请输入您的名字:") # 接收用户的输入,无论用户输入的是什么类型,最终返回的一定是字符串 # print(name) # print(type(name)) # print(name)
总结:python2中的raw_input与python3中input作用相同
数据:描述、衡量数据的状态
类型:不同的事物需要不同的类型存储
int(整型):用来记录人的年龄、出生年份、学生人数等整数相关的状态
age = 22
birthday = 1997
float (浮点型) : 用来记录人的身高、体重、薪资等小数相关的状态
height = 175.5 weight = 102.5 salary = 15000.8
str(字符串类型):用来记录人的名字、家庭住址、性别等描述性质的状态
name = "Tony" address = '上海浦东新区' sex = '男'
用单引号、多引号、双引号都可以定义字符串,本质上没有区别 但是要考虑嵌套的问题
字符串只能进行相加和相乘
>>> name = 'sean' >>> age = '18' >>> name + age #相加就是简单的字符串拼接 'sean18' >>> >>> name*5 # 相乘就相当于字符串相加5次 'seanseanseanseansean' >>>
list(列表) :可以存放多个同种属性的值
#列表类型是用索引来对应值,索引代表的是数据的位置,从0开始计数 >>> stu_name = ['张三','李四','王五'] >>> stu_name [0] '张三' >>> stu_name [1] '李四' # 2 列表可以嵌套, >>> student_info=[['tony',18,['jack']],['jason',18,['play','sleep']]] >>> student_info[0][2][0]# 取出第一个学生的第一个爱好 jack
dict(字典) : 可以存放多个不同属性的值
>>> person_info={'name':'tony','age':18,'height':185.5}
字典类型使用key来对应值(value) ,key可以对值有描述性的功能,通常为字符串
>>> person_info={'name':'tony','age':18,'height':185.5} >>> person_info['name'] 'tony' >>> person_info['age'] 18 >>> person_info['height'] 185.5
定义:判断事物的对错
值相等的情况下,id不一定相等
id相等的情况下,值一定相等
a = True # a = bool(True) b = False # a = bool(False) a = 1 b = 2 print(a > b) # 比较大小 False # 返回结果,不正确
is和==的区别
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
is和==都是对对象进行比较判断作用的 ==是python标准操作符中的比较操作符,用来比较判断两个对象的value(值)是否相等 ```python >>> a = 'cheesezh' >>> b = 'cheesezh' >>> a == b True ``` is也被叫做同一性运算符,这个运算符比较判断的是对象间的唯一身份标识,也就是id是否相同 ```python >>> x = y = [4,5,6] >>> z = [4,5,6] >>> x == y True >>> x == z True >>> x is y True >>> x is z False >>> >>> print id(x) >>> print id(y) >>> print id(z) ``` 同一类型下的a和b的(a==b)都是为True,而(a is b)则不然 ```python >>> a = 1 #a和b为数值类型 >>> b = 1 >>> a is b True >>> id(a) >>> id(b) >>> a = 'cheesezh' #a和b为字符串类型 >>> b = 'cheesezh' >>> a is b True >>> id(a) >>> id(b) >>> a = (1,2,3) #a和b为元组类型 >>> b = (1,2,3) >>> a is b False >>> id(a) >>> id(b) >>> a = [1,2,3] #a和b为list类型 >>> b = [1,2,3] >>> a is b False >>> id(a) >>> id(b) >>> a = {'cheese':1,'zh':2} #a和b为dict类型 >>> b = {'cheese':1,'zh':2} >>> a is b False >>> id(a) >>> id(b) >>> a = set([1,2,3])#a和b为set类型 >>> b = set([1,2,3]) >>> a is b False >>> id(a) >>> id(b) ``` 通过例子可看出,只有数值型和字符串型的情况下,a is b才为True,当a和b是tuple,list,dict或set型时,a is b为False
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = 'sean' age = 80 print('my name is %s,my age is %s.' % (name, age)) # 利用占位符格式化输出 my name is sean,my age is 80. # 尝试调换括号里面的数值 print('my name is %s,my age is %s.' % (age, name)) my name is 80,my age is sean. # 发现结果也更改了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 尝试接受字符串类型的变量 name = input('请输入你的名字:') age = input('请输入你的年龄:') print('my name is %d, my age is %d.' % (name, age)) 请输入你的名字:tony 请输入你的年龄:23 Traceback (most recent call last): File "G:/python项目/test/test.py", line 5, in <module> print('my name is %d, my age is %d.' % (name, age)) TypeError: %d format: a number is required, not str # 发现上面的接受字符串失败,再次尝试接受数字 name = 101 age = 23 print('my name is %d, my age is %d.' % (name, age)) my name is 101, my age is 23. # 没有报错
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
name = input('请输入你的名字:') age = input('请输入你的年龄:') print('my name is {},my age is {}.'.format(name, age)) my name is tony,my age is 23. # 尝试更换后面的name和age位置,看前面传入的值是否会调换位置 print('my name is {},my age is {}.'.format(age, name)) my name is 23,my age is tony. # 值得位置确实调换了 # 思考,如何让值得位置不受name和age接收的限制? print('my name is {names},my age is {ages}.'.format(ages=age, names=name)) 请输入你的名字:tony 请输入你的年龄:23 my name is tony,my age is 23
name = input('请输入你的名字:')
age = input('请输入你的年龄:')
age = input('请输入你的年龄:')
print(f'my name is {name}, my age is {age}.')
请输入你的名字:tony
请输入你的年龄:23
my name is tony, my age is 23.
请输入你的名字:tony
请输入你的年龄:23
my name is tony, my age is 23.
1、算术运算符
// 取整 %取模(取余数) ** 次幂
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#取整 a = 5 b = 2 a // b print(a // b ) #取模 a % b print(a % b ) #次幂 a ** b print(a ** b)
2、赋值运算符
增量赋值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#+= a = 1 a += 4 # a = a+3 print(a) 5 #-= a = 5 a -= 1 # a = a-1 print(a) 4 #*= a = 2 a *= 3 # a = a*3 print(a) 6 #/= a = 3 a /= 2 a = a / 2 print(a) 1.5
链式赋值
x = y = z = 5 print(x, y, z) 5 5 5
逻辑运算符
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
与 或 非 and or not a = 1 b = 2 c = 3 print(a < b and b > c) # and:如果有一个式子不符合条件,整条式子都为False print(a > b and b < c) print(a < b or b < c) # or:只要有一个式子符合条件,整条式子都为True print(a > b or b < c) print(not a < b) # 取反 print(a < b and b < c or a > c) # True print(a > b or b < c and a > c) # False