Did Python's 'and' and 'or' Operators Ever Return Values Other Than Booleans?

Susan Sarandon
Release: 2024-11-07 15:24:03
Original
372 people have browsed it

Did Python's 'and' and 'or' Operators Ever Return Values Other Than Booleans?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template