40

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.

Georgy
  • 12,464
  • 7
  • 65
  • 73
atp
  • 30,132
  • 47
  • 125
  • 187
  • What may be tripping you up is that you can use any datatype in a context that would seem to want a boolean. 'if 17: blah' is legal. – Russell Borogove Dec 18 '10 at 15:07

4 Answers4

63

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
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 2
    Do you know what the order of operands returned is? First to last? – atp Dec 18 '10 at 11:38
  • 11
    The first value (left to right) that determines the outcome (i.e. truish or falsish) is the result. – Martin v. Löwis Dec 18 '10 at 11:40
  • Comparison operators in Python also return a pure boolean value (examples: `x < 5` or chained like `0 < x <= 1` or even `5 > x < y`; chained comparisons finish before any boolean gets returned). – Attila the Fun Apr 27 '23 at 15:03
40

See this table from the standard library reference in the Python docs:

Boolean Operations

mgalgs
  • 15,671
  • 11
  • 61
  • 74
  • 3
    The 'is false' part is a little confusing. It's more like equals to False or evaluates in an `if` to False – Adam Sep 08 '16 at 14:56
  • 3
    @Adam No, it's definitely not like "equals to False". For example, `0` and `[]` are both false, but only the first is equal to `False`. The actual rule is explained in detail in the previous section, but it's still a bit confusing to keep straight in your head. Which is why in real life, everyone (including the core devs) says "truthy" and "falsey" instead, but the reference docs avoid that. – abarnert Apr 11 '18 at 00:54
  • As far as how "is false" is actually evaluated by the interpreter, I believe 2.7 never quite defines it anywhere, although you can guess pretty well from the `__nonzero__` docs that it means `x.__nonzero__() == 0 except AttributeError: x.__len__() == 0 except AttributeError: False`. – abarnert Apr 11 '18 at 00:58
24

from Python docs:

The operator not yields True if its argument is false, False otherwise.

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Python's or operator returns the first Truth-y value, or the last value, and stops. This is very useful for common programming assignments that need fallback values.

Like this simple one:

print my_list or "no values"

This will print my_list, if it has anything in it. Otherwise, it will print no values (if list is empty, or it is None...).

A simple example:

>>> my_list = []
>>> print my_list or 'no values'
no values
>>> my_list.append(1)
>>> print my_list or 'no values'
[1]

The compliment by using and, which returns the first False-y value, or the last value, and stops, is used when you want a guard rather than a fallback.

Like this one:

my_list and my_list.pop()

This is useful since you can't use list.pop on None, or [], which are common prior values to lists.

A simple example:

>>> my_list = None
>>> print my_list and my_list.pop()
None
>>> my_list = [1]
>>> print my_list and my_list.pop()
1

In both cases non-boolean values were returned and no exceptions were raised.

Community
  • 1
  • 1
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
0

Need to add some points to the @Frédéric 's answer.

do return one of their operands???

It is True but that is not the logic behind this. In python a number except 0 is considered as True and number 0 is considered as False.

(0 and 42 -> False and True) = False.

That's why it returns 0.

(0 or 42-> false or True) = 42

In that case the statement will be True because of the operand 42. so python returns the operand which causes the statement to be true, in that case.

ic3b3rg
  • 14,629
  • 4
  • 30
  • 53
dulaj sanjaya
  • 1,290
  • 13
  • 25