标签:创建 除法 数字 test 代码执行 mic png 运算优先级 code
[root@node10 python]# cat test.py
var1 = 10 var2 = 7 res = var1 + var2 print(res)
[root@node10 python]# python3 test.py
[root@node10 python]# cat test.py
var1 = 10 var2 = 7 res = var1 - var2 print(res)
var1 = 10 var2 = 7 res = var1 * var2 print(res)
var1 = 10 var2 = 7 res = var1 / var2 print(res)
#/ 最后的结果一定是小数
[root@node10 python]# cat test.py
res = 14 / 7
print(res)
整除之后的数
[root@node10 python]# cat test.py
res = 14 // 7 print(res) # 如果被除数 或者 除数是一个小数,那么结果后面加一个.0 res = 14.3 // 7 print(res)
[root@node10 python]# cat test.py
res = 8 % 3 # 正常取余数 print(res) res = -8 % 3 #-2 + 3 = 1 print(res) res = 8 % -3 #2 + (-3) = -1 print(res) res = -8 % -5 #-3果两者符号都是符号 那么直接取余数跟上一个负号 print(res)
[root@node10 python]# cat test.py
res = 3 ** 2 print(res) res = 2 ** 4 print(res)
> < >= <= == !=
返回一个布尔类型的值 True False
[root@node10 python]# cat test.py
# > var1 = 5 var2 = 3 res = var1 > var2 print(res) # < res = var1 < var2 print(res) # >= res = var1 >= var2 print(res) res = 5>=5 #满足一个条件即可 方返回真 print(res) # <= res = var1 <= var2 print(res) res = 5<=5 #满足一个条件即可 方返回真 print(res) # == res = var1 == var2 print(res) # != res = var1 != var2 print(res)
= += -= *= /= //= %= **=
[root@node10 python]# cat test.py
var1 = 8 var2 = 3 # = var1 = var2 print(var1) # += var1 += var2 ‘‘‘var1 = var1 + var2‘‘‘ print(var1) # -= var1 -= var2 ‘‘‘var1 = var1 - var2‘‘‘ print(var1) # *= var1 *= var2 var1 = var1 *var2 print(var1) # /= var1 /= var2 ‘‘‘var1 = var1 / var2‘‘‘ print(var1) # //= 地板除运算 var1 //= var2 var1 = var1 // var2 print(var1) # %= 余运算 var1 %= var2 var1 = var1 % var2 print(var1) # **= 幂运算 var1 = 4 var1 **= 3 print(var1)
in 和 not in (针对于容器型数据)
[root@node10 python]# cat test.py
strvar = "吾尝终日而思已,不如须臾之所学也" res = "吾" in strvar print(res) res = "终日" in strvar print(res) res = "不如" in strvar print(res) # list tuple set listvar = ["老子","孔子","孟子","荀子"] res = "孙子" not in listvar print(res) setvar = {"庄子","子路","冉有","颜回"} print(setvar) res = "颜回" in setvar print(res) tuplevar = ("子渊","冉求","公孙龙") res = "冉求" not in tuplevar print(res) # dict 成员运算符 判断字典时 判断的是键 不是那个所对应的值 dictvar = {"top":"程咬金","bottom":"甄姬","middle":"嫦娥"} res = "程咬金" in dictvar print(res) res = "top" not in dictvar print(res)
is | is not 检测两个数据在内存中是否是同一个地址
仅仅限定在3.6版本之前 是变量的缓存 , 判断一个值在某个范围内存在,就不会再另外创建一个相同的值,为了节省内存空间 3.7之后不再划定范围,只要有一份就缓存 提出小数据池 或者变量缓存是为了提升代码执行的效率,减少内存占用空间
[root@node10 python]# cat test.py
# 整型 -5 ~ 正无穷 var1 = 19 var2 = 19 res = var1 is var2 print(res) # 浮点型 非负数 var1 = -5.52 var2 = -5.52 res = var1 is not var2 print(res) # 复数 在实数 + 虚数这样的结构中永远不一样 但如果只有虚数 在值相同情况下一样 var1 = 3+4j var2 = 3+4j res = var1 is var2 print(res) var1 = 6j var2 = 6j res = var1 is not var2 print(res) # bool 在布尔值相同的情况下 id一样 var1 = True var2 = True res = var1 is var2 print(res) # 容器类型数据地址判断 # str 字符串而言,字符串值相同情况下,id一致 var1 = "你" var2 = "你" print(var1 is var2) var1 = (1,23) var2 = (1,23) print(var1 is var2) # 空元组例外 var1 = () var2 = () print(var1 is var2) # 剩下的容器类型数据无论什么值都不一样 var1 = [1,2] var2 = [1,2] print(var1 is not var2)
and or not
[root@node10 python]# cat test.py
# and 逻辑与 res = True and True print(res) res = True and False print(res) res = False and True print(res) res = False and False print(res)
[root@node10 python]# python3 test.py
# or 逻辑或 res = True or True print(res) res = True or False print(res) res = False or True print(res) res = False or False print(res)
# not 逻辑非(相当于取反) res = not True print(res) res = not False print(res)
‘‘‘ 一个or或者一个and 情况下 (1)True or something (2)False and something ‘‘‘ print("<===>") False or print(123) True and print(456)
# 逻辑优先级 # () > not > and > or res = 5 or 6 and 7 print(res) res = (5 or 6) and 7 # 5 and 7 print(res) res = not (5 or 6) and 7 # not 5 and 7 => False and 7 => False print(res) # 5 or 7 res = 1<2 and 3>4 or 5<6 # True and False or True => False or True print(res) res = 1<2 and 3>4 or 5>6 and 7<8 or 9>10 # True and False or False and True or False =>False or False or False => False or False => False print(res) # 这种情况特殊,上来直接短路,后面的不用再按照and or 优先级算了 (例外) res = 5 or 6 and 7 or 8 and 9 or 10 print(res) # 如果上来直接是假,不一定结果也是假,要依次向后计算 # False and True or True #数据类型判断 isinstance ‘‘‘ int float bool complex str list tuple set dict isinstance(要判断类型的数据,(类型1,类型2,类型3......)) 如果在类型元组当中,返回真 如果不在类型元组当中,返回假 ‘‘‘ intvar = 15 print(isinstance(intvar,int)) strvar = "789" print(isinstance(strvar,(int,str,list)))
& | ~ ^ << >>
var1 = 19 var2 = 15 # & 按位与 res = var1 & var2 print(res)
10011 01111 00011
var1 = 19 var2 = 15 # & 按位或 res = var1 | var2 print(res)
10011 01111 11111
""" n<<m n乘以2的m次幂 """ res = 5 << 2 # 相当于5 * 2的2次幂 print (res)
00000101 00010100 4+16 = 20
n>>m n除以2的m次幂
res = 5 >> 1 """ 00000101 00000010 """ print(res)
两个值[不相同]方为真,两个值[相同]方为假
var1 = 19 var2 = 15 res = var1 ^ var2 ‘‘‘ 10011 01111 11100 ‘‘‘ print(res)
res = ~19 res = ~-21 print(res)
res = 5+5 << 6 // 3 is 40 and True #规范写法 res = (5+5) << (6 // 3) is 40 and True print (res)
标签:创建 除法 数字 test 代码执行 mic png 运算优先级 code
原文地址:https://www.cnblogs.com/zyxnhr/p/12257599.html