ash ash - 29 days ago 20
Python Question

python and / or operators return value

I was watching a 2007 video on Advanced Python or Understanding Python, and at 18'27" the speaker claims "As some may know in Python

and
and
or
return one of the two values, whereas
not
returns always a boolean." When has this been the case?

As far as I can tell,
and
and
or
return booleans, too.

Answer

The and and or operators do return one of their operands, not a pure boolean value like True or False:

>>> 0 or 42
42
>>> 0 and 42
0

Whereas not always returns a pure boolean value:

>>> not 0
True
>>> not 42
False