and 和 or 的用法
and 和 or 是python的兩個邏輯運算符,可以使用and和or進行多個條件内容的判斷.
and是當and連接配接的所有的表達式的值為真時,才為真
or是當or連接配接的所有表達式的值一個為真時,這個表達式就為真
and和or表達式傳回的本質
and 的傳回結果問題:
從左到右計算表達式,若所有的都為真,則傳回最後一個值,若存在假,傳回第一個假值.
>>> 0 and False and 1
0
>>> 1 and False and 5
False
>>> 1 and 3 and False and 5
False
>>> 1 and 2 and 0 and False and 5
0
or 的傳回結果問題:
從左到右計算表達式,隻要遇到真值就傳回那個真值,如果表達式結束依舊沒有遇到真值,就傳回最後一個假值.
>>> 1 or 2 or False
1
>>> False or 2 or False
2
>>> False or 1 and 2
2
邏輯運算中:
再沒有()得情況下,not優先級高于and, and優先級高于or
優先級關系()> and > or 同一優先級從左往右計算。
x or y ,if x is false,then y,else x
x and y,if x is false,then x,else y
not x, if x is false,then True ,else False