and / or operators return value

atp picture atp · Dec 18, 2010 · Viewed 23.6k times · Source

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

Frédéric Hamidi picture Frédéric Hamidi · Dec 18, 2010

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