文、意如
一、數學運算子 + - * / // ** %
二、指定運算子 = += -= *= /=
三、比較運算子 == >= <= !=
四、邏輯運算子 and or not
五、特定運算子 is、 isnot
一、數學運算子 + - * /
x = 3
y = 6
print(x+y)
print(x-y)
print(x*y)
print(x/y)
/、// 、**
c1=6/3
print(c1)#2.0
c2=8/5
print(c2)#1.6
a1=6//3
print(a1) #2
b2=8//5
print(b2) #1
D1=7**3 #7*7*7
print(D1) #343
D2=8**5 #8*8*8*8*8
print(D2) #32768
a = 6%3
print(a) #0
b = 8%5
print(b) #3
二、指定運算子 = += -= *= /=
#指定運算子
x = 5
print(x) #5
x -= 3
print(x) #5-3 = 2
x += 3
print(x) #2+3 =5
x *= 3
print(x) #5*3 = 15
x /=3
print(x) #15/3 =5.0
三、比較運算子 == >= <= !=
#比較運算子
#布林只有兩種值(true) 或者(false)
#true 代表為真、成立、數字代表為 1
#false 代表為假、不成立、數字代表為 0
a = 5
b = 7
print(a == b) #false
print(a > b) #false
print(a <= b) #true
print(a != b) #true
四、邏輯運算子 and or not
試著做做看
運算符的優先級,由高到低:1. not 2.and 3.or
#邏輯運算子
"""
and (&&) 兩者都成立 才是True
or (||) 兩者其一成立 就是True
not (!) 相反 如果是True 就回傳False 、如果是False 就回傳True
"""
a = 5
b = 10
c = 8
print(a<b and b<c) #T and F = F
print(a<b and b>c and c==a ) #T and T and F = F
print(a>=b and b>c)
print(a<b or b>=c and c!=a )
print(a<b and b>c or c==a )
print(a<b or b>c or c==a)
五、特定運算子 is、isnot
"""
特定運算子
is a is b 兩個變數都一樣 回傳True
is not a is not b 兩個變數不一樣 回傳True
"""
a = 5
b = 7
c = 8
d = 5
print(a is c) #f
print(a is d) #t
print(a is not c) #t
print(a is not d) #f
Yiru@Studio - 關於我 - 意如