运算符

算术运算符

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

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

取余数运算

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

比较运算符

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

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

赋值运算符

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

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

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

成员运算符(in/not in)

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

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

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

身份运算符(is/not is)

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

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

逻辑运算符

逻辑运算符: 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
	如果出现了 True or 表达式  或者 False and 表达式的情况,直接返回前者,后面代码不执行
	如果没有出现短路效果,直接返回后者
"""
无论后面的表达式是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
"""
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)

位运算符

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

var1 = 19   // 10011
var2 = 15   // 1111
# & 按位与
res = var1 & var2    # 3
"""
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)  # 20

运算符优先级

"""
总结:
	个别运算符:
		运算符优先级最高的: **
		运算符优先级最低的: =
		()可以提升优先级
	一元运算符 > 二元运算符 (优先级)
		一元运算符 : 同一时间,操作一个值 ~ - 
		二元运算符 : 同一时间,操作一个值 + - * / ....
	同一种类运算符:
		算数运算符 : 乘除 > 加减
		逻辑运算符 : () > 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
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。