1. 程式人生 > 實用技巧 >Python基礎之運算子

Python基礎之運算子

# ### 運算子

# (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) # ### (2) 比較運算子 > < >= <= == !=
"""比較運算子只會差生兩種結果: 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)
# ### (3)賦值運算子:  = += -= *= /= //= %= **=
# = 從右向左,將右側的值賦值給左側變數
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) # (4)成員運算子: in 和 not in (針對於容器型資料) """ 字串必須是一個連續的片段 """ strvar = "1234,5678" res = "1" in strvar res = "2" not in strvar res = "3" in strvar res = "4" in strvar res = ',' not in strvar print(res) # list tuple set lst = ["1","2","3","4","5"] res = "6" in lst print(res) tup = ("1","2","3","4","5") res = "1" not in tup print(res) setvar = {"1","2","3"} res = "1" 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)
# ### (5)身份運算子:  is 和 is not (檢測兩個資料在記憶體當中是否是同一個值)  

# python3.6
# 整型 -5 ~ 正無窮
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)

# ### (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)

# 邏輯短路:  如果出現了短路效果,後面的程式碼不執行
"""
短路:
(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)
# ### (7)位運算子:    & |  ^ << >> ~

# 按位與 &
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
"""

# 運算子總結:
"""
(1) 個別運算子:
    優先順序最高 ** 冪運算
    優先順序最低 =  賦值運算子
    () 括號可以提升運算優先順序
    
(2) 整體 一元運算子 > 二元運算子
    一元運算子: 同一時間,只操作一個值 - ~
    二元運算子: 同一時間,操作兩個值 + - * / ...
    
(3) 同一層級
    邏輯: () > not > and > or
    算數: 乘除 > 加減
    位運算子: ( << >> ) > & > ^ > |

(4) 其他運算子
    算數運算子 > 位運算子 > 比較運算子 > 身份運算子> 成員運算子 > 邏輯運算子
    賦值運算子是最後算完進行賦值,做收尾工作的.
"""    
res = 5+5 << 6//3 is 40 and True
print(res)

"""
res = 10 << 2 is 40 and True
res = 40 is 40 and True
res = True and True
res = True
"""
# 加上括號包起來,讓別人能看懂程式碼;
res = (5+5) << (6//3) is 40 and True