The operator and returns the last element if no element is False
(or an equivalent value, such as 0
).
For example,
>>> 1 and 4
4 # Given that 4 is the last element
>>> False and 4
False # Given that there is a False element
>>> 1 and 2 and 3
3 # 3 is the last element and there are no False elements
>>> 0 and 4
False # Given that 0 is interpreted as a False element
The operator or returns the first element that is not False
. If there are no such value, it returns False
.
For example,
>>> 1 or 2
1 # Given that 1 is the first element that is not False
>>> 0 or 2
2 # Given that 2 is the first element not False/0
>>> 0 or False or None or 10
10 # 0 and None are also treated as False
>>> 0 or False
False # When all elements are False or equivalent
1
solved Python Logical Operators