標籤:pytho 布爾值 python 返回 優先順序 布爾 bool als 邏輯
#and or not
#優先順序 ()>not>and>or
#and or notprint(2>1 and 1<4 or 2<3 and 9>6 or 2<4 and 3<2)# True or True or False#True
print(3>4 or 4<3 and 1==1) #Falseprint(1<2 and 3<4 or 1>2) #Trueprint(2>1 and 3<4 or 4>5 and 2<1) #Trueprint(1>2 and 3<4 or 4>5 and 2>1 or 9<8) #Falseprint(1>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) #Falseprint(not 2>1 and 3<4 or 4>5 and 2>1 and 9>8 or 7<6) #False
數字與布爾值轉換
#int-->boolprint(bool(2)) #Trueprint(bool(-2)) #Trueprint(bool(0)) #False#bool-->intprint(int(True)) #1print(int(False)) #0
x or y,若 x 為非0,則返回 x
print(1 or 2) #1print(3 or 2) #3print(0 or 2) #2print(0 or 100) #100
x and y,若 x 為真,則返回 y
print(1 and 2) #2print(0 and 2) #0
#and or 組合
print(2 or 100 or 3 or 4) #2print(0 or 4 and 3 or 2) #3
#綜合 第一個為布爾型則返回布爾型,第一個為數值型,則返回數值型
print(1>2 and 3 or 4 and 3<2) #Falseprint(2 or 1<3 and 2) #2
print(0 or 5<4) #Falseprint(0 and 3>1) #0print(3>1 and 0) #0print(3>1 and 2 or 2<3 and 3 and 4 or 3>2) #2
01 Python 邏輯去運算