I am currently learning Python3, but I don't understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that it is supposed to return "True" or "False" and not one of the input numbers. Second, I don't see why the output (one of the input numbers) is so. Please help, thanks.
3 Answers
The operator and returns the last element if no element is False
(or an equivalent value, such as 0
).
For example,
>>> 1 and 4
4 # Given that 4 is the last element
>>> False and 4
False # Given that there is a False element
>>> 1 and 2 and 3
3 # 3 is the last element and there are no False elements
>>> 0 and 4
False # Given that 0 is interpreted as a False element
The operator or returns the first element that is not False
. If there are no such value, it returns False
.
For example,
>>> 1 or 2
1 # Given that 1 is the first element that is not False
>>> 0 or 2
2 # Given that 2 is the first element not False/0
>>> 0 or False or None or 10
10 # 0 and None are also treated as False
>>> 0 or False
False # When all elements are False or equivalent

- 19,111
- 14
- 69
- 90
-
Thanks. This is really confusing... Why complicate things so much and sacrifice readability for the sake of brevity? I am startled by these 2 examples: `(1 and not 1) or (1 and 1)` returns `1`, but `(1 and not 0) or (1 and 0)` returns `True`??? – ACV Feb 17 '21 at 20:48
This can be confusing - you're not the first to be tripped up by it.
Python considers 0 (zero), False, None or empty values (like [] or '') as false, and anything else as true.
The "and" and "or" operators return one of the operands according to these rules:
- "x and y" means: if x is false then x, else y
- "x or y" means: if x is false then y, else x
The page you reference doesn't explain this as clearly as it could, but their examples are correct.

- 602
- 5
- 19
I don't know if this helps, but to extend @JCOC611's answer, I think of it as returning the first element that determines the value of the logical statement. So, for a string of 'and's, the first False value or the last True value (if all values are True) determine the ultimate result. Similarly, for a string of 'or's, the first True value or last False value (if all values are False) determine the ultimate result.
>>> 1 or 4 and 2
1 #First element of main or that is True
>>> (1 or 4) and 2
2 #Last element of main and that is True
>>> 1 or 0 and 2
1
>>> (0 or 0) and 2
0
>>> (0 or 7) and False
False #Since (0 or 7) is True, the final False determines the value of this statement
>>> (False or 7) and 0
0 #Since (False or 7) is True, the final 0(i.e. False) determines the value of this statement)
The first line is read as 1 or (4 and 2), so since 1 makes the ultimate statement True, its value is returned. The second line is governed by the 'and' statement, so the final 2 is the value returned. Using the 0 as False in the next two lines can also show this.
Ultimately, I'm generally happier using Boolean values in boolean statements. Depending on non-booleans being associated with boolean values always makes me uneasy. Also, if you thibk of constructing a boolean staement with boolean values, this idea of returning the value that determines the value of the entire statement makes more sense (to me, anyway)

- 6,968
- 8
- 50
- 77