最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

速看:05-python运算符

来源:博客园

运算符

算术运算符

算数运算符: + - * / // % **


(资料图片仅供参考)

# +var1 = 7var2 = 90res = var1 + var2print(res) # 97# -var1 = 7var2 = 90res = var1 - var2print(res) # 83 # *var1 = 7var2 = 10res = var1 * var2print(res) # 630# / 结果永远为小数var1 = 10var2 = 5res = var1 / var2     # 5.0 print(res , type(res))# // 地板除"""被除数 ➗ 除数 = 商""""""注意点:如果被除数或者除数是小数,那么得到正常结果之后,数值后面带上.0变成小数"""var1 = 10.0var2 = 3.0# var2 = 3.0res = var1 // var2  # 3.0print(res)# %  取余var1 = 7var2 = 4res = var1 % var2       # 3res = -7 % 4  # -3 + 4 = 1       # res = 7 % -4  # 3 + (-4) = -1res = -7 % -4 # -3 (被除数和除数都是负的,正常结果加负号)res = 81 % 7   # 4res = 81 % -7  # -3res = -81 % 7  # 3res = -81 % -7 # -4print(res)# ** 幂运算res = 2 ** 3print(res)   # 2^3=8

取余数运算

-7 % 3,先算7%3=4,然后看被除数前符号==>-4,再加上除数,-4+3=-17 % -3,7%3=4,被除数正,4+-3=1

比较运算符

比较运算符: > < >= <= == !=比较运算符的结果要么是True , 要么是False 只有两个值

res = 10 > 5res = 10 >= 10# ==这个符号是在做比较,比较==两边的数值是否一样res = 5 == 9res = 5 != 9 print(res)

赋值运算符

赋值运算符: = += -= *= /= //= %= **=

赋值运算符 将右侧的值赋值给左侧变量;一般右侧先运算再赋值给左边

# = 赋值运算符 将右侧的值赋值给左侧变量a = 5 <= 3print(a)var1 = 10var2 = 5# += """var1 = var1 + var2"""# var1 += var2# print(var1)# -="""var1 = var1 - var2"""# var1 -= var2# print(var1)# %="""var1 = var1 % var2"""var1 %= var2print(var1)

成员运算符(in/not in)

成员运算符: in 和 not in (针对于容器型数据)

xxx in/not in 容器类型,判断是不是再字符串中

"""字符串判断时,必须是连续的片段"""strvar = "今天天气要下雨,赶紧回家收衣服"res = "今" in strvar    # Trueres = "天气" in strvar  # Trueres = "赶回" in strvar  # Falseprint(res)# 针对于列表,元组,集合container = ["赵沈阳","赵万里","赵世超"]container = ("赵沈阳","赵万里","赵世超")container = {"赵沈阳","赵万里","赵世超"}# res = "赵沈阳" in container       # True# res = "赵万里" not in container   # Falseres = "赵世超1223232" not in container # Trueprint(res) # 针对于字典 (判断的是字典的键,不是值)container = {"zsy":"赵沈阳","zwl":"赵万里","zsc":"赵世超"}res = "赵沈阳" in container # Falseres = "zsy" in containerprint(res)

身份运算符(is/not is)

身份运算符 is 和 is not (检测两个数据在内存当中是否是同一个值),判断地址

# 整型 -5~正无穷 (驻留小数据池)var1 = 100var2 = 100print(var1 is var2)   # True# 浮点型 非负数  var1 = -9.1var2 = -9.1print(var1 is var2)  # False# bool 相同即可var1 = Truevar2 = Trueprint(var1 is var2)  # True# complex 在实数+虚数不相同 (只有虚数的情况下例外)var1 = 6-8jvar2 = 6-8j   # Falsevar1 = -10jvar2 = -10j   # Falsevar1 = 10jvar2 = 10j    # Trueprint(var1 is var2)# 容器: 相同字符串 , 空元组相同即可  剩下的所有容器都不相同container1 = ()container2 = ()print(container1 is not container2)  # Falsecontainer1 = "你"container2 = "你"print(container1 is not container2)   # Falsecontainer1 = [1,23,3]container2 = [1,23,3]print(container1 is not container2)   # True

逻辑运算符

逻辑运算符: and or not

# and 逻辑与   """全真则真,一假则假"""res = True and True    # Trueres = True and False   # Falseres = False and True   # Falseres = False and False  # Falseprint(res)# or  逻辑或  """一真则真,全假则假"""res = True or True    # Trueres = False or True   # Trueres = True or False   # True res = False or False  # Falseprint(res)# not 逻辑非  res = not Trueres = not Falseprint(res)# 逻辑短路计算规律:先脑补计算当前表达式的布尔值是True还是False如果出现了 True or 表达式  或者 False and 表达式的情况,直接返回前者,后面代码不执行如果没有出现短路效果,直接返回后者"""无论后面的表达式是True 还是False 都已经无法改变最后的结果,那么直接短路,后面的代码不执行;(1) True or print("程序执行了 ~ 1111")(2) False and print("程序执行了 ~ 2222")True or print("程序执行了 ~ 1111")True or True => TrueTrue or False => TrueFalse and print("程序执行了 ~ 2222")False and True  => FalseFalse and False => False"""res = 5 and 6 # 6"""True and True =>TrueTrue and False => False"""res = 5 or 6  # 5res = 0 and 999res = 0 or "abc"print(res)# 逻辑运算符的优先级""" 优先级从高到低: () > not > and > or   """res = 5 or 6 and 7 # 5 or 7 => 5res = (5 or 6) and 7 # 5 and 7res = not (5 or 6) and 7 # not 5 and 7 => False and 7 => Falseres = 1<2 or 3>4 and 5<100 or 100<200 and not (700>800 or 1<-1)"""not (False or False) => Trueres = 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 Trueres = True or False or Trueres = True or True => True"""print(res) 

位运算符

位运算符: & | ^ << >> ~

var1 = 19   // 10011var2 = 15   // 1111# & 按位与res = var1 & var2    # 3"""000 ... 10011000 ... 01111000 ... 00011 => 3"""print(res)# | 按位或res = var1 | var2"""000 ... 10011000 ... 01111000 ... 11111"""print(res)   # # ^ 按位异或"""两个值不相同=>True 反之返回False"""res = var1 ^ var2"""000 ... 10011000 ... 01111000 ... 11100"""print(res)# << 左移 (相当于乘法)"""5乘以2的n次幂"""res = 5 << 1 # 10res = 5 << 2 # 20res = 5 << 3 # 40print(res)"""000 ... 101  => 5000 .. 1010  => 10000 ..10100  => 20000 .101000  => 40"""# >> 右移 (相当于除法)"""5地板除2的n次幂"""res = 5 >> 1 # 2res = 5 >> 2 # 1res = 5 >> 3 # 0"""000 ... 101000 ... 010 => 2000 ... 001 => 1000 ... 000 => 0"""print(res)# ~ 按位非 (针对于补码进行操作,按位取反,包含每一位)""" -(n+1) """# res = ~22res = ~19print(res)"""原码:000 ... 10011反码:000 ... 10011补码:000 ... 10011补码:   000 ... 10011按位非: 111 ... 01100给你补码->原码补码:111 ... 01100反码:100 ... 10011原码:100 ... 10100 => -20"""res = ~-19print(res)  # 20

运算符优先级

"""总结:个别运算符:运算符优先级最高的: **运算符优先级最低的: =()可以提升优先级一元运算符 > 二元运算符 (优先级)一元运算符 : 同一时间,操作一个值 ~ - 二元运算符 : 同一时间,操作一个值 + - * / ....同一种类运算符:算数运算符 : 乘除 > 加减逻辑运算符 : () > not > and > or 位运算符   : ( << >> ) > & > ^ > |整体排序:算数运算符 > 位运算符 > 比较运算符 > 身份运算符 > 成员运算符 > 逻辑运算符赋值运算符用来做收尾"""res = 5+5 << 6 // 3 is 40 and False"""res = 10 << 2 is 40 and Falseres = 40 is 40 and Falseres = True and Falseres = False"""print(res)# 用括号提升下优先级res = (5+5) << (6//3) is 40 and False

关键词: 赋值运算符 逻辑运算符 算术运算符