运算符
1、算术运算符
加+、减-、乘*、除/(结果永远是小数)、地板除 //、取余%、幂运算**(优先级最高)
地板除// :如果除数或者被除数存在小数,那结果上加上 .0
取余% :
81 % 11 = 4
-81 % 11 = -4 + 11 =7
81 % -11 = 4 + ( -11 ) = -7
-81 % -11 = -4 (如果被除数和除数都是负数,在正常得到的结果上加上负号)
var1 = 2
var2 = 4
加+
ret1 = var1 + var2
减-
ret2 = var1 - var2
乘*
ret3 = var1 * var2
除/
ret4 = var1 / var2
地板除 //
ret5 = var1 // var2
取余%
ret6 = var1 % var2
幂运算**
ret7 = var1 ** var2
print(ret1, ret2, ret3, ret4, ret5, ret6, ret7)
2、比较运算符
> < >= <= == != 只会产生两个结果:True和False
= :一个等号是赋值,把等号右边的值赋给左边
==:两个等号是在做比较,判断左右值是否相同
!=:判断两个值是否不等
var7 = 20
var8 = 20
ret8 = var7 > var8
ret9 = var7 < var8
ret10 = var7 >= var8
ret11 = var7 <= var8
ret12 = var7 == var8
ret13 = var7 != var8
print(ret8, ret9, ret10, ret11, ret12, ret13)
3、赋值运算符
= 等号、+=加等、-=减等、*=乘等、/=除等、//=地板除等、%=取余等、**=幂等
= 等号
rew1 = 12
rew2 = 2
+=加等
rew2 += rew1
-=减等
rew2 -= rew1
*=乘等
rew2 *= rew1
/=除等
rew2 /= rew1
//=地板除等
rew2 //= rew1
%=取余等
rew2 %= rew1
**=幂等
rew2 **= rew1
print(rew2)
4、成员运算符
in 和 not in (针对于容器类型数据)
注:字符串必须是一个连续的片段
list列表 tuple元组 set集合
dict字典(in 和 not in 在字典中只判断键不能判断值)
rew3 = "asdpwdzxc"
rew4 = ['asd', 'pwd', 'zxc']
in 和 not in 字符串
rew5 = "asd" in rew3
rew6 = "dsa" in rew3
rew5 = "asd" not in rew3
rew6 = "dsa" not in rew3
in 和 not in 列表(元组,集合)
rew7 = 'ass' in rew4
rew8 = 'asd' in rew4
rew7 = 'ass' not in rew4
rew8 = 'asd' not in rew4
print(rew5, rew6)
print(rew7, rew8)
in 和 not in 字典
rew9 = {'as': '阿斯蒂', 'zs': '安中学'}
rew10 = 'as' in rew9
rew11 = 'bs' in rew9
rew10 = 'as' not in rew9
rew11 = 'bs' not in rew9
print(rew10, rew11)
5、身份运算符
is 和 is not (检测两个数据在内存中是否是相同的地址)
整型,浮点型,布尔型
qwe1 = 90
qwe2 = 90
qwe1 = -90
qwe2 = -90
qwe1 = -90.9
qwe2 = -90.9
qwe1 = True
qwe2 = False
复数类型
qwe1 = 0j
qwe2 = 0j
qwe1 = 1 + 2j
qwe2 = 1 + 2j
容器类型
qwe1 = ()
qwe2 = ()
qaq1 = qwe1 is qwe2
qaq2 = qwe1 is not qwe2
print(qaq1, qaq2)
6、逻辑运算符
and逻辑与(&&)、or逻辑或(||)、not逻辑非(!)
and逻辑与(全真即真,一假即假)
or逻辑或(一真即真,全假即假)
!逻辑非(真变假,假变真)
逻辑短路:如果出现短路效果代码就不知执行了
True or 表达式 真
False and 表达式 假
注:print是内置函数,函数内部返回的是None,功能是打印
and逻辑与(&&)
qwe3 = True and False
qwe4 = False and False
qwe5 = True and True
qwe6 = False and False
or逻辑或(||)
qwe3 = True or False
qwe4 = False or False
qwe5 = True or True
qwe6 = False or False
print(qwe3, qwe4, qwe5, qwe6)
not逻辑非(!)
qwe7 = not True
qwe8 = not False
特殊的情况 短路
qwe7 = True or True
qwe8 = True or False
qwe7 = 5 or 6
qwe8 = 0 or 6
qwe7 = 5 and 6
qwe8 = 0 and 6
print(qwe7, qwe8)
逻辑运算的优先级
() > not > and > or
示例
qwe9 = 1 > 2 or 3 < 4 and 5 > 10 or 11 < 12 and 13 > 16
qwe9 = not(5 or 6) and 7
qwe9 = (5 or 6) and 7
qwe9 = 5 or 6 and 7
print(qwe9)
7、位运算符
按位与 &、按位或|、按位非~、按位异或^、左移<<(乘法操作)、右移>>(除法操作)
按位异或^:两者之间不一样返回真,一样返回假
左移<<:相当于做乘法:a << b = a*2的b次幂
右移>>:相当于做除法:a >> b = a/2的b次幂
按位非!:针对补码进行按位取反,包括符号位。公式:-(n+1)
运算级:
(1)个别
优先级最高 ** 幂运算
优先级最低 = 赋值运算
()括号可以提升优先级
(2)整体
一元运算符 > 二元运算符
一元运算符:同一时间,只操作一个值
二元运算符:同一时间,操作两个值
(3)同一层级
逻辑:() > not > and >or
算数:乘除 > 加减
位运算:(<< >>) > & > ^ > |
(4)其它情况
算术运算符 > 位运算符 > 比较运算符 > 身份运算符 > 成员运算符 > 逻辑运算符
赋值运算符用来将算好的值赋给等号左边的变量
asd1 = 19
asd2 = 15
print(asd1 & asd2) # 与
print(asd1 | asd2) # 或
print(asd1 ^ asd2) # 异或
asd3 = 5 << 4 # 左移
asd4 = 5 >> 2 # 右移
print(asd3, asd4)
asd5 = ~19 # 非
asd6 = ~(-19) # 非
print(asd5, asd6)
示例
asd7 = 5 + 5 << 6 // 3 is 40 and True
asd8 = (5 + 5) << (6 // 3) is 40 and True
print(asd7, asd8)