What's the Truth About 'and', 'or', and 'not' Operators?
A Python expert claims that in the past, the 'and' and 'or' operators returned one of their operands rather than a boolean. But when was this the case?
The Truth Unveiled
Contrary to the claim, 'and' and 'or' operators have always returned boolean values in Python. The example provided by the speaker:
0 or 42 == 42
is returning 42 because 42 is the first true value encountered in the logical expression. Similarly,
0 and 42 == 0
returns 0 because 0 is the first false value encountered.
In contrast, the 'not' operator consistently returns a pure boolean value:
not 0 == True
not 42 == False
Therefore, the claim that 'and' and 'or' operators used to return operands instead of booleans is incorrect.
The above is the detailed content of Did Python's 'and' and 'or' Operators Ever Return Values Other Than Booleans?. For more information, please follow other related articles on the PHP Chinese website!