Boolean
python有別的寫法
Python裡的Boolean不叫boolean,是「bool」。
a = True
b = False
print(a and b) #False
print(a or b) #True
print(not a) #False
在做if判斷時,「非」的寫法可以直接寫「not」(用「~」反而會無效)
if not result.__contains__(value):
&
python中沒有「&&, ||」這種「兩個&|」的符號(其他語言中視為and, or),寫為「and, or」
a = None
b = "S"
print(a is None) # >True
print(b is None) # >False
print(a is None and b is None) # >False
print(a & b) # >TypeError: unsupported operand type(s) for &: 'NoneType' and 'stry
a = 7
b = 3
print(a is None) # >False
print(b is None) # >False
print(a is None and b is None) # >False
print(a & b) # >3
print(a is None & b is None)
# >TypeError: unsupported operand type(s) for &: 'NoneType' and 'int' =>「X is None」的回傳是個NoneType的物件
Transfer type
string to boolean
bool(str)
python視任何object為「truthy」,其值非空時布林為True,空值時為False;所以只有空字串為False。
print(bool('True')) # >True
print(bool('')) # >False
print(bool('X')) # >True
print(bool(0)) # >False
print(bool(3)) # >True
Last updated