Sub topic 7
Combining conditions with and, or, and not
This chapter assumes you can write a basic if/else statement and use comparison operators such as >= and ==. Here you will join several tests into one condition.
The three operators
Python has three logical operators. Each one takes conditions and produces a new condition.
| Operator | Meaning | True when |
|---|---|---|
| `and` | both | every part is true |
| `or` | either | at least one part is true |
| `not` | opposite | the part is false |
age = 25
has_ticket = True
if age >= 18 and has_ticket:
print('entry allowed')
if age < 13 or age >= 65:
print('discount')
if not has_ticket:
print('buy a ticket first')The first block prints entry allowed. The second and third print nothing, because their conditions are false.
The diagram shows how and combines two tests.
Precedence: not, then and, then or
When one condition mixes operators, Python applies not first, then and, then or. This is similar to multiplication coming before addition.
a, b, c = True, False, False
print(a or b and c)
print((a or b) and c)The first line prints True, because b and c is evaluated first and gives False, and True or False is True. The second line prints False, because the parentheses force a or b to run first.
They return operands, not only True or False
and and or return one of the values you gave them. Only not always returns a real bool. This matters when you use the result outside an if.
name = ''
display = name or 'guest'
print(display)The empty string is falsy, so or moves on and returns 'guest'. The program prints guest. The or operator returns the first truthy value, or the last value if none are truthy. The and operator returns the first falsy value, or the last value if all are truthy.
The most common mistake
Beginners often write the condition the way they would say it in English.
color = 'green'
if color == 'red' or 'blue':
print('primary color')This prints primary color even though color is 'green'. Python reads the condition as (color == 'red') or 'blue'. The string 'blue' is non-empty, so it is always truthy. Each side of or must be a full condition, as in this version.
color = 'green'
if color == 'red' or color == 'blue':
print('primary color')
if color in ('red', 'blue'):
print('primary color')Neither block prints anything now. The second form, using in, stays short when the list of allowed values grows.
Simplifying with not
Use not when it makes the intent clearer, and avoid stacking it. Compare these two lines.
is_closed = False
if not is_closed:
print('open')For comparisons, prefer the direct operator. Write x != 3 instead of not x == 3. De Morgan's laws help when you need to flip a compound condition. The expression not (a and b) is equivalent to not a or not b.
Exercise
What does this code print, and why? x = 5 print(x > 3 or x / 0 > 1) print(not x > 3 and x < 10)
Show answer
The first line prints True. Python finds x > 3 is true, so or does not need the right side, and the division by zero never runs. The second line prints False. not applies first to x > 3, giving False, and False and ... is False.