python 全棧開發:邏輯運算
阿新 • • 發佈:2019-01-04
基礎運算子
邏輯運算:
優先順序:()> not > and >or
數字轉bool值,0為False,非零的數字為True。
1、
print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2) True print(3 > 4 or 4 < 3 and 1 == 1) False print(1 < 2 and 3 < 4 or 1 > 2) True print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) True print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) False print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) False print( not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) False
2、
x or y ,x為Ture,則輸出值為x,x為False,則輸出值為y。
x and y ,x為True,則輸出值為y,x為False,則輸出值為x。
print(0 or 2) 2 print(2 or 0) 2
print(2 and 1) 1 print(1 and 2 ) 2 print(0 and 2) 0
print(1 or 2) 1