As a premise, in Python, the bool value is True for anything that is not 0 or empty.
bool(0) # False
bool(0.1) # True
bool("") # False
bool([""]) # True
On the contrary, True and False can be operated in the same way as 1 and 0.
True*1 # 1
False*1 # 0
True+0 # 1
True+True # 2
Although not limited to Python, and and or are short-circuit evaluated on both sides.
True and False # True
print("a") or print("b")
# a
# b
1 and print("b") # b
And the logical operation of Python ** The result is not limited to bool. ** **
100 and 200 # 200
200 and 100 # 100
100 or 200 # 100
200 or 100 # 200
If you use &, | instead of and, or, it becomes a bit operation.
True & False # False
True & 1 # 1
True & 2 # 0
123 & 125 # 121
Recommended Posts