天天看點

python 操作符 or Operator 的傳回值

or operater

  1. BOOLEAN:
x y x or y
True True True
True False True
False True True
False False False
True or True => True
False or True => True
True or False => True
False or False => False
           
x y x or y
x y x, if x is true, otherwise y

隻看or左側的,如果非空,則傳回左側數值;若空,則直接傳回右側

會傳回False的Objects:

  • constants defined to be false: None and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: ‘’, (), [], {}, set(), range(0)
0 or 1 => 1
1 or 2 => 1
2 or 1 => 2
[] or 1 => 1
[] or 0 => 0
2 < 4 or [] => True
5 > 10 or [] => []
5 > 10 or 0 => 0
True or 4 < 3 => True
False or 0 < 3 => True
           

繼續閱讀