Python 中 OR 和 AND 運算子有什麼區別?


在 Python 中,and 和 or(以及 not)被定義為邏輯運算子。它們都需要兩個可以求出真或假的運算元。

只有當兩個運算元都為真時,and 運算子才返回真。

>>> a=50
>>> b=25
>>> a>40 and b>40
False
>>> a>100 and b<50
False
>>> a==0 and b==0
False
>>> a>0 and b>0
True

如果任一運算元為真,or 運算子將返回真。

>>> a=50
>>> b=25
>>> a>40 or b>40
True
>>> a>100 or b<50
True
>>> a==0 or b==0
False
>>> a>0 or b>0
True

更新時間: 26-Feb-2020

5K+ 次瀏覽

開啟你的 職業生涯

完成本課程,獲取認證

開始
廣告
© . All rights reserved.