16 Conditional Operators
- 可以製作出True/False結果的operations有很多,這邊整理一下:
16.0.1 ==
, !=
, >=
, <=
這些operator,我們稱為comparison operators,適用於數值型資料:
比大小:
a == b
,a != b
,a < b
,a <= b
,a > b
,a >=b
餘數類:
a % 3 == 0
只想看商:
a // 3 == 1
= 33
a = 200
b if b > a:
print("b is greater than a")
#> b is greater than a
16.0.2 and
, or
, not
- 這種operator,我們稱為logical operators,適用於連接多個condition
- logical operators就是
and
,or
,not
,對應到R的&
,|
,!
= 200
a = 33
b = 500
c if a > b and c > a:
print("Both conditions are True")
#> Both conditions are True
if a > b or a > c:
print("At least one of the conditions is True")
#> At least one of the conditions is True
if not(b > a):
print("a is greater or equal to b")
#> a is greater or equal to b