運算符 算術運算符 算數運算符: + - * / // % ** # + var1 = 7 var2 = 90 res = var1 + var2 print(res) # 97 # - var1 = 7 var2 = 90 res = var1 - var2 print(res) # 83 # * ...
運算符
算術運算符
算數運算符: + - * / // % **
# +
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