标签:内存 算数运算 原码 情况 str 一个 必须 赋值运算符 二进制
运算符包括算数运算符、比较运算符、赋值运算符、成员运算符、身份运算符、逻辑运算符、位运算符
注意点 | |
---|---|
算数运算符 | % 取余 , //地板除 , ** 幂运算 |
比较运算符 | == 比较两个值是否相等 != 比较两个值是否不同 |
赋值运算符 | a += 1 => a = a+1 |
成员运算符 | in 或 not in 判断某个值是否包含在(或不在)一个容器类型数据当中 |
身份运算符 | is 和 is not 用来判断内存地址是否相同 |
逻辑运算符 | 优先级 () > not > and > or |
位运算符 | 优先级 (<<或 >> ) > & > ^ > | |
# (1) 算数运算符 + - * / // % ** var1 = 6 var2 = 4 # + res = var1 + var2 print(res) # - res = var1 - var2 print(res) # * res = var1 * var2 print(res) # / 除法 (结果为小数) res = var1 / var2 res = 10 / 5 print(res) # // 地板除 (返回整数) res = 10 // 5 res = 11 // 4 print(res) # 如果被除数或者除数是一个小数,结果加上.0 res = 11.0 // 4 print(res)
res = 11 % 4 res = -11 % 4 # -3 + 4 = 1 res = 11 % -4 # 3 + (-4) = -1 res = -11 % -4 # -3(如果被除数和除数都是负数,在正常得到的结果前面填上负号即可.) print(res) res = -17 % 7 # -3 + 7 = 4 res = 17 % -7 # 3 + (-7) = -4 res = -17 % -7 #-3 print(res)
res = 3 ** 2 res = 3 ** 4 print(res)
比较运算符只会差生两种结果: True 真的 ,False 假的
var1 = 100 var2 = 100 res = var1 > var2 res = var1 < var2 res = var1 >= var2 res = var1 <= var2 print(res) # 注意点 res = var1 == var2 print(res) res = var1 != var2 print(res)
# = 从右向左,将右侧的值赋值给左侧变量 var1 = 9 var2 = 2 # += """var1 = var1 + var2""" # var1 += var2 # print(var1) # -= """var1 = var1 - var2""" # var1 -= var2 # print(var1) # *= """var1 = var1 * var2""" # var1 *= var2 # print(var1) # /= """var1 = var1 / var2""" # var1 /= var2 # print(var1)
# //= """var1 = var1 // var2""" # var1 //= var2 # print(var1) # %= """var1 = var1 % var2""" # var1 %= var2 # print(var1) # **= """var1 = var1 ** var2""" var1 **= var2 print(var1)
字符串必须是一个连续的片段
strvar = "雷霆嘎巴,无情哈拉少" res = "雷" in strvar res = "文" not in strvar res = "无情" in strvar res = "霆哈少" in strvar res = ‘,‘ not in strvar print(res) # list tuple set lst = ["余锐","高学峰","治理追","温子月","草神光"] res = "高学峰" in lst print(res) tup = ("余锐","高学峰","治理追","温子月","草神光") res = "李博伦" not in tup print(res) setvar = {"金角大王","银角大王","神秘男孩"} res = "神秘男孩" in setvar print(res) # dict """ in 和 not in 在字典中只判断键,不判断值 """ dic = {"yr":"英俊帅气","bx":"抠脚大汉","mcp":"高大威猛"} res = "英俊帅气" in dic # False res = "yr" in dic res = "mcp" not in dic print(res)
var1 = 1000 var2 = 1000 res = var1 is var2 print(res)
var1 = -5.78 var2 = -5.78 res = var1 is not var2 print(res)
var1 = True var2 = True res = var1 is var2 print(res)
var1 = 4-90j var2 = 4-90j res = var1 is var2 print(res)
var1 = (1,2,3) var2 = (1,2,3) res = var1 is var2 print(res)
"""全真则真,一假则假""" res = True and True # True res = True and False # False res = False and True # False res = False and False # False print(res)
"""全假则假,一真则真""" res = True or True # True res = True or False # True res = False or True # True res = False or False # False print(res)
"""真变假,假变真""" res = not True res = not False print(res)
如果出现了短路效果,后面的代码不执行
""" 短路: (1) True or 表达式 (2) False and 表达式 """ """ True or print("紫月真胖呀") True or True => True True or False => True False and print("紫月真猛呀") False and False => False False and True => False """ """ bool 类型为假的十种情况 0 0.0 0j False ‘‘ [] () {} set() None 先用大脑算一下布尔值真假,是否出现短路情况 如果出现了直接返回当前值,没出现,继续向右执行; """ res = 5 or 6 res = 0 or 6 res = 0 and 9 print(res)
() > not > and > or
res = 5 or 6 and 7 # 5 or 7 => 5 res = (5 or 6) and 7 # 5 and 7 => 7 res = not(5 or 6) and 7 # not 5 and 7 => False and 7 =>False print(res) res = 1>2 or 3<4 and 5>10 or 11<12 and 13>15 or 100<1000 and 1<2 # res = False or True and False or True and False or True and True # res = False or False or False or True # res = False or False or True # res = False or True # res = True print(res)
var1 = 19 var2 = 15 print(var1 & var2) """ 19的二进制 0000 ... 10011 15的二进制 0000 ... 01111 0000 ... 10011 0000 ... 01111 0000 ... 00011 => 3 """
var1 = 19 var2 = 15 print(var1 | var2) """ 19的二进制 0000 ... 10011 15的二进制 0000 ... 01111 0000 ... 10011 0000 ... 01111 0000 ... 11111 => 31 """
""" 1 0 => 1 1 1 => 0 0 0 => 0 0 1 => 1 """ var1 = 19 var2 = 15 print(var1 ^ var2) """ 19的二进制 0000 ... 10011 15的二进制 0000 ... 01111 0000 ... 10011 0000 ... 01111 0000 ... 11100 => 28 """
""" 左移相当于做乘法 : 5 * 2的n次幂 """ res = 5 << 1 # 10 res = 5 << 2 # 20 res = 5 << 3 # 40 print(res) """ 0000 ... 101 0000 ... 1010 移动一位 10 0000 10100 移动二位 20 """
""" 右移相当于做除法 : 5 // 2的n次幂 """ res = 5 >> 1 # 2 res = 5 >> 2 # 1 res = 5 >> 3 # 0 print(res) """ 0000 ... 101 0000 ... 010 移动一位 2 0000 ... 001 移动二位 1 0000 ... 000 移动三位 0 """
"""公式: -(n+1)""" var1 = ~19 print(var1) """ ~ 按位非每一位都取反,包括符号位,求反码每一位除了符号位,都取反. 原码:0000 ... 10011 反码:0000 ... 10011 补码:0000 ... 10011 补码: 0000 ... 10011 按位非: 1111 ... 01100 给你补码求原码: 补码: 1111 ... 01100 反码: 1000 ... 10011 原码: 1000 ... 10100 => -20 """ var1 = ~(-22) print(var1) """ 原码: 1000 ... 10011 反码: 1111 ... 01100 补码: 1111 ... 01101 补码: 1111 ... 01101 按位非: 0000 ... 10010 给你补码求原码: 正数 : 补码 = 反码 = 原码 原码: 0000 ... 10010 => 18 """
7、运算符总结
1 """ 2 (1) 个别运算符: 3 优先级最高 ** 幂运算 4 优先级最低 = 赋值运算符 5 () 括号可以提升运算优先级 6 7 (2) 整体 一元运算符 > 二元运算符 8 一元运算符: 同一时间,只操作一个值 - ~ 9 二元运算符: 同一时间,操作两个值 + - * / ... 10 11 (3) 同一层级 12 逻辑: () > not > and > or 13 算数: 乘除 > 加减 14 位运算符: ( << >> ) > & > ^ > | 15 16 (4) 其他运算符 17 算数运算符 > 位运算符 > 比较运算符 > 身份运算符> 成员运算符 > 逻辑运算符 18 赋值运算符是最后算完进行赋值,做收尾工作的. 19 """ 20 res = 5+5 << 6//3 is 40 and True 21 print(res) 22 23 """ 24 res = 10 << 2 is 40 and True 25 res = 40 is 40 and True 26 res = True and True 27 res = True 28 """ 29 # 加上括号包起来,让别人能看懂代码; 30 res = (5+5) << (6//3) is 40 and True
标签:内存 算数运算 原码 情况 str 一个 必须 赋值运算符 二进制
原文地址:https://www.cnblogs.com/kongxiangqun/p/13225699.html