Boolean Expressions
The Python "bool" type (short for "boolean") has only two values - True
and False
. The bool values typically show up in the tests of if-statements and while-loops.
Boolean Comparisons
Usually a bool is created by comparing two values — e.g. x == y
returns True
if the values are equal, like this:
1 == 2 → False
Equal
1 != 2 → True
Not equal
1 < 2 → True
Strict less than
2 < 2 → False
Strict less than
2 <= 2 → True
Less or equal
Unlike many languages, the ==
and !=
operators work for any data type — int, string, list, and so on.
The operation of the if-statement and while-loop is based on bool values, but works in a slightly more flexible way. See Truthy Logic for details.
Boolean Operators And Or Not
Boolean values can be combined with the operators and or not, like following (printing 'yay' if x is in the range 0..100 inclusive):
if x >= 0 and x <= 100: print('yay')
Python is unique in using the plain words like "and" for this. many languages use "&&
" for and, "||
" for or.
A and B
→ True
if both are True
A or B
→ True
if one or the other or both are True
not A
→ Inverts True
/False
Boolean Precedence
There is "precedence" between and/or/not, analogous to arithmetic precedence, where "*" has higher precedence than "+".
Precedence order: not
is highest precedence, followed by and
, followed by or
. Mnemonic: not
is like unary minus e.g. '-6', and
is like * multiplication, or
is like + addition.
Q: what does the following do?
if a < 6 and b < 6 or c < 6:
The and
goes first (higher precedence), so the above is equivalent to the following form with parenthesis added to show the order the comparisons are evaluated:
if (a < 6 and b < 6) or c < 6:
To force the or
to go first, put in parenthesis like this:
if a < 6 and (b < 6 or c < 6):
If you are unsure what order an expression will follow, you can always add parenthesis to force the order you want.
Boolean Short-Circuit
Suppose you have a string s and an index number i, and you want to check if the char at that index is alphabetic .. but only if i is valid. You can write that this way...
if i < len(s) and s[i].isalpha():...
This works because the boolean evaluation goes left to right, stopping ("short-circuiting") as soon as it can. So in the example above, if i < len(s)
is False (i.e. i is large), the whole expression evaluates to False. In particular the s[i].isalpha()
is not evaluated. Attempting to evaluate it would be an error, since i is too large, creating an IndexError.
Writing the i<len(s)
test to the left of the s[i]
in effect protects it, and this is a common programming pattern. Most computer languages use short circuiting in the their boolean expressions like this.
History: George Boole
For numbers we have the familiar rules of algebra. George Boole realized there were analogous Boolean algebra rules for working with True
/False
expressions. The boolean type is named after him.
In particular De Morgan's law, is a neat rule from Boolean algebra, similar to multiplying a sum by -1
:
1. not (A or B) = (not A) and (not B) 2. not (A and B) = (not A) or (not B)
Copyright 2020 Nick Parlante