Python基本运算符
Python基本运算符
一、算术运算符
算术运算符不仅有加'+'减'-'乘'*'除'/',还有整除'//',取余'%',等于'='。
print(9 // 6)
print(9 % 6)
print(9 + 6)
print(9 - 6)
print(9 * 6)
print(9 / 6)
print(9 == 6)
二、赋值运算符
- 增量赋值
x = 100
x += 50 # :x = x + 50
print(x)
y = 100
y -= 50 # :y = y - 50
print(y)
z = 100
z *= 50 # :z = z * 50
print(z)
q = 100
q /= 50 # :q = q / 50
print(q)
- 链式赋值
a = 999
b = a
c = a
print(a, b, c) # :打印变量'a''b'''c'的值
a = b = c # :简化写法
print(a, b, c) # :打印变量'a''b'''c'的值
- 交叉赋值
v = 3.141592653
w = 1234567890
tmp = v # :创建一个临时变量名'tmp'
v = w
w = tmp
print(v, w)
v, w = w, v # :简化写法
print(v, w)
- 解压赋值
分步解压
student_height_list = ['175cm', '180cm', '177cm', '187cm', '190cm', '188cm', '185cm'] # :创建学生身高列表
student_height1 = student_height_list[0]
print(student_height1)
student_height2 = student_height_list[1]
print(student_height2)
student_height3 = student_height_list[2]
print(student_height3)
student_height4 = student_height_list[3]
print(student_height4)
student_height5 = student_height_list[4]
print(student_height5)
student_height6 = student_height_list[5]
print(student_height6)
student_height7 = student_height_list[6]
print(student_height7)
一步到位
student_height_list = ['175cm', '180cm', '177cm', '187cm', '190cm', '188cm', '185cm'] # :创建学生身高列表
height1, height2, height3, height4, height5, height6, height7 = student_height_list
print(height1, height2, height3, height4, height5, height6, height7)
一般情况下,左右两边的变量名和变量值的个数要相等,但可以使用'*_'打破限制
student_height_list = ['175cm', '180cm', '177cm', '187cm', '190cm', '188cm', '185cm'] # :创建学生身高列表
student_height1, student_height2, *_ = student_height_list # :用‘*_'代替剩余的变量值
print(student_height1, student_height2, _) # 打印前二个变量值
*_, student_height6, student_height7 = student_height_list # :用‘*_'代替前面的变量值
print(_, student_height6, student_height7) # :打印后两个变量值
三、逻辑运算符
'and'与
所有条件全都满足,运行才结果显示'True'
print(777 == 999 and 666 < 8888 and 888 > 4444 and 222 != 222) # :打印逻辑判断结果
'or'或
其中一个条件满足,运行结果就显示'True'
print(777 == 999 or 666 < 8888 or 888 > 4444 or 222 != 222) # :打印逻辑判断结果
'not'否
将条件结果否定,即'True'变为'False','False'变为'True'
print(not True) # :打印'not'逻辑判断结果
print(not False) # :打印'not'逻辑判断结果
四、成员运算
判断数据值(真实数据)在不在某个列表中
legend_name_list = ['pat', 'ezreal', 'jax', 'yi', 'janna', 'annie', 'taric', 'ahri', 'ekko', 'yasuo', 'jeyce']
print('yasuo' in legend_name_list) # :判断'yasuo'是否在列表集合中
print('victor' in legend_name_list) # :判断'vitor'是否在列表集合中
五、身份运算
'is'判断
判断两个数据值的内存地址是否一致
a = ['abc', 'bcd', 'cdf']
b = ['abc', 'bcd', 'cdf']
print(a is b) # :判断内存地址是否一致
'=='判断
判断两个数据的值是否一致
a = ['abc', 'bcd', 'cdf']
b = ['abc', 'bcd', 'cdf']
print(a == b) # :判断数值是否一致