# (5)身份运算符 is 和 is not (检测两个数据在内存当中是否是同一个值) # 整型 -5~正无穷 var1 = 100 var2 = 100 print(var1 is var2) # 浮点型 非负数 var1 = -9.1 var2 = -9.1 print(var1 is var2) # bool 相同即可 var1 = True var2 = True print(var1 is var2) # complex 在实数+虚数不相同 (只有虚数的情况下例外) var1 = 6-8j var2 = 6-8j var1 = -10j var2 = -10j print(var1 is var2) # 容器: 相同字符串 , 空元组相同即可 剩下的所有容器都不相同 container1 = () container2 = () print(container1 is not container2) container1 = "你" container2 = "你" print(container1 is not container2) container1 = [1,23,3] container2 = [1,23,3] print(container1 is not container2) # (6)逻辑运算符: and or not # and 逻辑与 """全真则真,一假则假""" res = True and True # True res = True and False # False res = False and True # False res = False and False # False print(res) # or 逻辑或 """一真则真,全假则假""" res = True or True # True res = False or True # True res = True or False # True res = False or False # False print(res) # not 逻辑非 res = not True res = not False print(res) # 逻辑短路 """ 无论后面的表达式是True 还是False 都已经无法改变最后的结果,那么直接短路,后面的代码不执行; (1) True or print("程序执行了 ~ 1111") (2) False and print("程序执行了 ~ 2222") True or print("程序执行了 ~ 1111") True or True => True True or False => True False and print("程序执行了 ~ 2222") False and True => False False and False => False """ """ 计算规律: 先脑补计算当前表达式的布尔值是True还是False 如果出现了 True or 表达式 或者 False and 表达式的情况,直接返回前者,后面代码不执行 如果没有出现短路效果,直接返回后者 """ res = 5 and 6 # 6 """ True and True =>True True and False => False """ res = 5 or 6 # 5 res = 0 and 999 res = 0 or "abc" print(res) # 逻辑运算符的优先级 """ 优先级从高到低: () > not > and > or """ res = 5 or 6 and 7 # 5 or 7 => 5 res = (5 or 6) and 7 # 5 and 7 res = not (5 or 6) and 7 # not 5 and 7 => False and 7 => False res = 1<2 or 3>4 and 5<100 or 100<200 and not (700>800 or 1<-1) """ not (False or False) => True res = 1<2 or 3>4 and 5<100 or 100<200 and not (700>800 or 1<-1) res = True or False and True or True and True res = True or False or True res = True or True => True """ print(res)
位运算符
# ### (7)位运算符: & | ^ << >> ~ var1 = 19 var2 = 15 # & 按位与 res = var1 & var2 """ 000 ... 10011 000 ... 01111 000 ... 00011 => 3 """ print(res) # | 按位或 res = var1 | var2 """ 000 ... 10011 000 ... 01111 000 ... 11111 """ print(res) # ^ 按位异或 """两个值不相同=>True 反之返回False""" res = var1 ^ var2 """ 000 ... 10011 000 ... 01111 000 ... 11100 """ print(res) # << 左移 (相当于乘法) """5乘以2的n次幂""" res = 5 << 1 # 10 res = 5 << 2 # 20 res = 5 << 3 # 40 print(res) """ 000 ... 101 => 5 000 .. 1010 => 10 000 ..10100 => 20 000 .101000 => 40 """ # >> 右移 (相当于除法) """5地板除2的n次幂""" res = 5 >> 1 # 2 res = 5 >> 2 # 1 res = 5 >> 3 # 0 """ 000 ... 101 000 ... 010 => 2 000 ... 001 => 1 000 ... 000 => 0 """ print(res) # ~ 按位非 (针对于补码进行操作,按位取反,包含每一位) """ -(n+1) """ # res = ~22 res = ~19 print(res) """ 原码:000 ... 10011 反码:000 ... 10011 补码:000 ... 10011 补码: 000 ... 10011 按位非: 111 ... 01100 给你补码->原码 补码:111 ... 01100 反码:100 ... 10011 原码:100 ... 10100 => -20 """ res = ~-19 print(res) """ 原码:100 ... 10011 反码:111 ... 01100 补码:111 ... 01101 补码: 111 ... 01101 按位非: 000 ... 10010 给你补码->原码 (因为是整数 ,原反补相同) 000 ... 10010 => 19 """ """ 总结: 个别运算符: 运算符优先级最高的: ** 运算符优先级最低的: = ()可以提升优先级 一元运算符 > 二元运算符 (优先级) 一元运算符 : 同一时间,操作一个值 ~ - 二元运算符 : 同一时间,操作一个值 + - * / .... 同一种类运算符: 算数运算符 : 乘除 > 加减 逻辑运算符 : () > not > and > or 位运算符 : ( << >> ) > & > ^ > | 整体排序: 算数运算符 > 位运算符 > 比较运算符 > 身份运算符 > 成员运算符 > 逻辑运算符 赋值运算符用来做收尾 """ res = 5+5 << 6 // 3 is 40 and False """ res = 10 << 2 is 40 and False res = 40 is 40 and False res = True and False res = False """ print(res) # 用括号提升下优先级 res = (5+5) << (6//3) is 40 and False