# ### 变量的缓存机制 (仅对python3.6版本负责)
# -->Number 部分(int,float.bool,complex)
# 1.对于整数而言,-5~正无穷范围内的相同值,id一致
var1 = 10
var2 = 10
var1 = -100
var2 = -100
print(id(var1),id(var2))
# 2.对于浮点数而言,非负数范围内的相同值 id一致
var1 = 0.15
var2 = 0.15
var1 = - 1.6
var2 = - 1.6
print(id(var1),id(var2))
# 3. 布尔值而言,值相同情况下,id一致
var1 = True
var2 = True
print(id(var1),id(var2))
# 4.复数在 实数+虚数 这样的结构中永不相同(只有虚数的情况下例外)
var1 = 3+4j
var2 = 3+4j
var1 = 8j
var2 = 8j
var1 = -60j
var2 = -60j
print(id(var1),id(var2))
# -->容器类型部分(str,list,tuple,set,dict)
# 5.字符串 和 空元组 相同的情况下,地址相同
var1 = "您好"
var2 = "您好"
print(id(var1),id(var2))
var1 = ()
var2 = ()
print(id(var1),id(var2))
# 6.列表,元组,字典,集合无论什么情况 id标识都不同 [空元组例外]
var1 = (1,2,3)
var2 = (1,2,3)
print(id(var1) , id(var2))
# ### 运算符
var1 = 10
var2 = 5
# 1.算数运算符 + - * / // % **
# +
res = var1 + var2
print(res)
# -
res = var1 - var2
print(res)
# *
res = var1 * var2
print(res)
# / (结果一定是小数)
res = var1 / var2 # 2.0
print(res)
# // (地板处,取整数)
# 商 = 被除数 // 除数
res = var1 // var2 # 2
# 如果被除数或者除数都是小数,那么在正常取得这个结果之后加上.0
res = 10.0 // 5 # 2.0
print(res)
# % (取余)
# 被除数和除数如是负数的求法:
# 如果被除数是负数,先全部看做正整数求出余数,然后给余数加负号,最后加上除数
# 如果除数是负数,先全部看做正整数求出余数,然后给除数加负号,最后余数加上带负号的除数
res = var1 % var2
res = 7 % 5 # 2
res = -7 % 5 # -2 + 5 = 3
res = 7 % -5 # 2 + (-5) = -3
res = -7 % -5 #-2 # 被除数和除数都是负数,余数头上直接加负号 => -3
print(res)
# **
res = 5 ** 2
print(res)
# 2. 比较运算符: > < >= =< == !=
res = 10 > 8
res = 10 <= 8
res = 10 >= 10
print(res)
# == 是在比较,比较两个值是否相同, = 是在赋值,将右侧的值赋值给左侧
res = 10 == 10 # True
res = 10 != 10 # False
print(res)
出处:https://www.cnblogs.com/liucx/
# 3.赋值运算符: = += -= *= /= //= %= **=
# 赋值运算(从右往左看)
# +=
'''var1 = var1 + var2'''
var1 = 8
var2 = 3
var1 += var2
print(var1)
# -=
'''var1 = var1 - var2'''
var1 = 8
var2 = 3
var1 -= var2
print(var1)
# *=
'''var1 = var1 * var2'''
var1 = 8
var2 = 3
var1 *= var2
print(var1)
# /=
'''var1 = var1 / var2'''
var1 = 8
var2 = 3
var1 /= var2
print(var1)
# //=
'''var1 = var1 // var2'''
var1 = 8
var2 = 3
var1 //= var2
print(var1)
# %=
'''var1 = var1 % var2'''
var1 = 10
var2 = 3
var1 %= var2
print(var1)
# **=
'''var1 = var1 **= var2'''
var1 = 10
var2 = 2
var1 **= var2
print(var1)
# 4.成员运算符: in 和 not in (针对容器型数据)
# 字符串
"""需要字符串是一个连续的片段"""
strvar = "天天向上,好好学习"
res = "上" in strvar
res = "天天" in strvar
res = "上学" in strvar # False
res = "上学" not in strvar # True
print(res)
# list tuple set
listvar = ["小刘","小李","小陈","小张"]
tupvar = ("小刘","小李","小陈","小张")
setvar = {"小刘","小李","小陈","小张"}
res = "小刘" in listvar
res = "小李" not in tupvar
res = "aa" in setvar
print(res)
# dict
"""in 或 not in 在字典当中,判断的是键,不是值"""
dic = {"xk":"星空","lt":"蓝天","by":"白云"}
res = "星空" in dic # False
res = "xk" in dic
print(res)
# 5.身份运算符: is 和 is not (检测两个数据在内存当中是否是同一个值)
# 整型 -5 ~ 正无穷
var1 = 100
var2 = 100
print(var1 is var2)
# 浮点型 非负数
var1 = 4.8
var2 = 4.8
print(var1 is not var2)
# 相同的布尔值
var1 = True
var2 = True
print(var1 is var2)
# 实数+虚数 这样结构里,永远不同
var1 = 5-90j
var2 = 5-90j
var1 = 90j
var2 = 90j
print(var1 is var2)
# 容器(除了相同的字符串和相同的空元组之外,剩下的所有容器都不一样)
var1 = ()
var2 = ()
var1 = (1,2)
var2 = (1,2)
print(var1 is var2)
# 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 = True or False # True
res = False or True # True
res = False or False # False
print(res)
# 逻辑非 not
"""真变假,假变真"""
res = not True
res = not False
print(res)
print("====")
# 逻辑短路
"""
短路场景:
(1)True or 表达式
(2)False and 表达式
后面的代码由于逻辑问题,没有触发执行
"""
print(True or print("1111")) # 打印 True
print(False and print("2222")) # 打印 False
res = True or True # True
res = True or False # True
res = False and True # False
res = False and False # False
print(res)
"""
1.先脑补当前这个值的布尔型是True还是False
2.依据真假用来判断是否短路
3.短路返回前者,否则返回后者
"""
res = 8 or 6 # 8 短路,返回前者
res = 5 and 6 # 6 不短路,返回后者
res = False and 6 # False 短路
res = 6 and True
print(res)
# 逻辑符号的优先级
# () > not > and > or
res = 5 or 6 and 7 # 5 or 7 => 5
res = not 5 or 7 and 8 # False or 7 and 8 =>False or 8 => 8
res = not(5 or 6) and 7 # not 6 and 7 => False and 7 => False
res = 0 or 6 and 7 # 0 or 7 => 7
res = 0 or 6 and 0 # 0 or 0 =>0
res = 1>2 and 3<4 or 7>10 and 10<11 or 11> 12 and 34<1
'''
False and True or False and True or False and False
False or False or False
False or False
False
'''
print(res)
# 7.位运算符: & | ^ << >> ~
# & 按位与
var1 = 19
var2 = 15
res = var1 & var2
print(res) #3
"""
10011 => 19
01111 => 15
00011
"""
# | 按位或
var1 = 19
var2 = 15
res = var1 | var2
print(res) # 31
"""
10011 => 19
01111 => 15
11111
"""
# ^ 按位异或 (相同的两个值是False,不同的两个值是True)
var1 = 19
var2 = 15
res = var1 ^ var2
print(res) # 28
"""
10011 => 19
01111 => 15
11100
"""
# << >>
'''<< 左移是做乘法 乘以2的n次幂'''
res = 5 << 2 # 5 * 2 的2次幂 => 20
res = 2 << 3 # 2 * 2 的3次幂 => 16
res = 3 << 1 # 3 * 2 的1次幂 => 6
print(res)
"""
5 => 0000 0101 => 左移2位 => 0001 0100 => 20
2 => 0000 0010 => 左移3位 => 0001 0000 => 16
2 => 0000 0011 => 左移1位 => 0000 0110 => 6
"""
""">> 右移是做除法,除以2的n次幂"""
res = 5 >> 1 # 5 // 2的1次幂 => 2
res = 5 >> 2 # 5 // 2的2次幂 => 1
res = 5 >> 3 # 5 // 2的3次幂 => 0
print(res)
"""
0000 0101 => 右移1位 => 0000 0010 => 2
0000 0101 => 右移2位 => 0000 0001 => 1
0000 0101 => 右移3位 => 0000 0000 => 0
"""
# ~ 按位非
"""公式:-(n+1)"""
res = ~ -19 # 18
res = ~ -10 # 9
res = ~ 10 # -11
print(res)
# 总结
"""
优先级最高的运算符: **
优先级最低的运算符: =
一元运算符优先级 > 二元运算符
一元运算符 : 同一时间只操作一个数字的 - ~
二元运算符 : 同一时间操作两个数字的 + - * /...
同级运算符比较:
乘除 > 加减
() > not > and > or
(<< >>) > & ^ > |
算术运算符 > 位运算符 > 比较运算符 > 身份运算符 > 成员运算符 > 逻辑运算符 >
赋值运算符做收尾,等所有值运算结束之后,赋值给左侧变量.
"""# 用括号包起来,更加方便大家阅读;
res = (5 + 5) << (6 // 3) is 40 or False
# res = 10 << 2 is 40 or False
# res = 40 is 40 or False
# res = True or False
# res = True
print(res)