-2

Can someone help to explain the following:

1 and 5
5 and 1

The above gives 5 and 1 respectively. Q1: Why is the and operator not commutative?

Next:

1==True # gives True

However:

5==True
5==False

both give False. Q2: Why?

https://docs.python.org/2/library/stdtypes.html section 5.1 says "... All other values are considered True", so 5 should be True.

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • 1
    In answer to Q2: `bool(5)` is True, this does not mean that `5 == True` – Ffisegydd Oct 14 '14 at 08:59
  • As shown above, 5 is **`neither`** True nor False. I am fine with that if that is so defined. But this clearly contradicts section 5.1. – Old Geezer Oct 14 '14 at 09:03
  • @OldGeezer `true != True`, true here means truthy value. – Ashwini Chaudhary Oct 14 '14 at 09:04
  • 1
    Dups: [Why do 'and' & 'or' return operands in Python?](http://stackoverflow.com/questions/22598547/why-do-and-or-return-operands-in-python) and [Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language?](http://stackoverflow.com/questions/2764017/is-false-0-and-true-1-in-python-an-implementation-detail-or-is-it-guarante) – Ashwini Chaudhary Oct 14 '14 at 09:05
  • 1
    Please keep your posts limited to **one** question; you asked two, and both are duplicates. I've closed this as a duplicate of one of those, but Ashwini named the other as well. – Martijn Pieters Oct 14 '14 at 09:07
  • @MartijnPieters I couldn't find in the text any mention of the `bool()` function. Can you point me to it in case I am missing something. – Old Geezer Oct 14 '14 at 09:45
  • See [*Truth value testing*](https://docs.python.org/2/library/stdtypes.html#truth-value-testing) and the [`bool()` function](https://docs.python.org/2/library/functions.html#bool). – Martijn Pieters Oct 14 '14 at 09:49

3 Answers3

2

From the documentation:

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.

Note that here, if x is false means if bool(x) == False.

Vincent
  • 12,919
  • 1
  • 42
  • 64
  • I wasn't aware that the English text `if x is false` means `if bool(x)==False`. However, if I treat 5 as neither True nor False, then the definition of `and`: if x is false then x else y also explains why `5 and 1` gives 1 and `1 and 5` gives 5. – Old Geezer Oct 14 '14 at 09:36
  • 1
    The `bool(x)` distinction is important however, especially if you consider examples like `[] and 5` (that returns `[]`) or the fact that you can define a `__bool__` method (or `__nonzero__` in python 2) to customize boolean evaluation for a class. – Vincent Oct 14 '14 at 09:46
1

The answer to your first question about and is that the result of the and is the value of the first expression if it is False-like (0, False, None, etc.), otherwise it is the value of the second expression. A bunch of programming languages share this sort of construct; in some languages like Bash and Perl it's idiomatic to rely on it extensively. In Python we mostly use and for regular boolean logic and rely on it less often for short-circuiting.

As for 1==True being true but 5==True being false, well, frankly I wish 1==True also returned false but obviously it's too late to change it now. :)

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

In answer to your second part:

It's true that all non-zero integers are "Truthy" but this does not mean that 5 == True will return True. The == operator is comparing value and, when comparing values, 5 is not equal to True. If you compare bool(5) == True then that will return True. When you do if 5: what you actually are doing is if bool(5):.

The reason that 1 == True returns True is that in Python the boolean class is a subclass of int, with True being represented by 1 and False being represented by 0. So 1 == True returns True because they are equal.

Note that, as discussed in this question, this is NOT GUARANTEED in Python 2.x as True and False can be re-defined. In Python 3.x they are keywords which will always be equal to 1 and 0, respectively.

Community
  • 1
  • 1
Ffisegydd
  • 51,807
  • 15
  • 147
  • 125