# 運算子 # 算數運算子 a = 10 b = 21 print(a + b) # 31 print(a - b) # -11 print(a * b) # 210 a = 10 b = 5 # / (求得的結果永遠為小數) print(a / b) # 2.0 print(b % a) # 5 # // 被除數或者除數如果是小數,那麼求得的結果後要加 .0 a = 11.0 b = 5 print(a // b) # 2.0 # 比較運算子 print(a == b) # False print(a >= b) # False print(a <= b) # True print(a != b) # True print(a > b) # False print(a < b) # True # 賦值運算子 # c = a + b # print(c) # 31 # a += b # print(a) # 31 # a -= b # print(a) # -11 # a *= b # print(a) # 210 # a /= b # print(a) # 0.47619047619047616 # b %= a # print(b) # 1 # b //= a # print(b) # 2 a = 2 b = 3 a **= b print(a) # 8 # 身份運算子(is 和 is not 檢測兩個資料在記憶體當中是否是同一個值) # 整型 var1 = 100 var2 = 100 print(id(var1), id(var2)) # 4307965392 4307965392 print(var1 is var2) # True var1 = -5 var2 = -5 print(var1 is var2) # True var1 = -6 var2 = -6 print(var1 is var2) # True var1 = -100 var2 = -100 print(var1 is var2) # 浮點型 var1 = 5.3 var2 = 5.3 print(var1 is var2) # True var1 = -5.3 var2 = -5.3 print(var1 is var2) print(var1 is not var2) # False # 複數 var1 = 3 - 90j var2 = 3 - 90j print(var1 is var2) # 布林值 var1 = True var2 = True print(var1 is var2) # 相同的字串或元組,地址相同 var1 = () var2 = () print(var1 is var2) var1 = (20,) var2 = (20,) print(var1 is var2) var1 = [1, 2] var2 = [1, 2] print(var1 is var2) # False var1 = '123' var2 = '123' print(var1 is var2) # True # 邏輯運算子 and or not # and 邏輯與 """全真則真,一假則假""" res = True and True print(res) # True res = True and False print(res) # False res = False and True print(res) # False res = False and False print(res) # False # or 邏輯或 """一真則真,全假則假""" res = True or True print(res) # True res = True or False print(res) # True res = False or True print(res) # True res = False or False print(res) # False # not 邏輯非 res = not True print(res) # False res = not False print(res) # True # 邏輯短路 """後面的程式碼不執行""" """ 現象有兩種: 1.True or 表示式 2.False and 表示式 計算順序: 首先腦補當前這個布林值是真還是假 然後看一下這個式子是否短路 如果短路返回前者 反之,返回後者 """ res = 5 or 6 print(res) # 5 res = 5 and 6 print(res) # 6 res = 0 and 6 print(res) # 0 res = 0 or 6 print(res) # 6 # 邏輯運算子優先順序 """() > not > and > or""" # 成員運算子 : in 和 not in(針對容器型別資料) """要求字串是連續的片段""" strvar = "沙塵暴的天氣終於過了" res = "沙" in strvar print(res) # True res = "沙塵暴" in strvar print(res) # True res = "沙了" in strvar print(res) # False # 針對於字典dict """針對於字典,判斷的是鍵不是值""" dictvar = {"123": 456, "789": 0} res = "123" in dictvar print(res) # True res = 456 in dictvar print(res) # False # 位運算 # 運算子優先順序 # 算數運算子>位運算>比較運算子>身份運算子>成員運算子>邏輯運算子>賦值運算子