Combine two conditions
Quick review (2 minutes)
Here are two signs invented for this lesson.
When A and B are each True or False: A and B is True only if both are True. A or B is True if at least one is True. not A is True if A is False, and False if A is True.
Without parentheses, comparisons group first, then not, then and, then or. Python works out the left side of and or or first, and works out the right side only when the left side has not settled the answer.
Write the rule in words and underline each joining word: and, or, not.
Write each part as a complete condition. A part is a comparison with its own left side, operator and right side, such as
height >= 120, or a name that already holdsTrueorFalse, such ashas_ticket.Where the rule negates a part, write
notin front of that part, such asnot has_coat.Join the parts with
andwhere both must hold and withorwhere one is enough.If the rule mixes
andwithor, put parentheses around the part the rule treats as one group, as in(member or total > 50) and coupon.Fill in a small table with one row for each case of the parts, True or False, and work out the whole condition for every row.
Each side of and or or must be an expression that stands on its own. English lets you say "older than 12 and younger than 18". In Python, < 18 is not an expression, so age > 12 and < 18 stops with a SyntaxError. Write age > 12 and age < 18.
The slip that raises no error is reply == "y" or "yes". Python reads it as (reply == "y") or "yes". The right side is the text "yes" on its own, and Python counts any text that is not empty as true. The condition therefore passes for every reply. Write reply == "y" or reply == "yes".
When and and or share a line, and groups more tightly. member or total > 50 and coupon means member or (total > 50 and coupon). If you mean the other grouping, write the parentheses.
1 In plain words
Here are two signs invented for this lesson. A sign at a ride says that you may board if you are tall enough and you hold a ticket. Height alone is not enough, and a ticket alone is not enough. A café sign offers a free refill if you are a member or you arrive early. There, one of the two is enough.
Python has three words for joining and flipping conditions. They are the Boolean operators and, or and not. In this unit each side of and or or is a whole condition of its own, such as height >= 120. A value that an operator works on is called an operand.
When each operand is True or False, the operators behave like the two signs. A and B is True only when both are True. A or B is True when at least one is True, and that includes the case where both are. not A flips one value, so True becomes False and False becomes True.
A condition can be given a name. has_ticket = ticket == "yes" looks odd at first. Read it with the rule for =: work out the right side, which is a comparison that gives True or False, then tie the name to that value.
Remember one sentence: and needs both, or needs one, not flips. To look them up, find the Boolean Operations section in the Built-in Types chapter of the Python 3.13 library reference.
2 Picture it
A truth table with one row for each pair of values of A and B. When A and B are both True, A and B is True and A or B is True. When A is True and B is False, A and B is False and A or B is True. When A is False and B is True, A and B is False and A or B is True. When both are False, A and B is False and A or B is False. The first and last rows are marked: A and B is True only in the first, and A or B is False only in the last.
3 Why
Python groups the parts of a condition in a fixed order. Arithmetic binds most tightly, then comparisons, then not, then and, and or last. So height >= 120 and has_ticket is read as (height >= 120) and has_ticket. Parentheses change the grouping, as they do in arithmetic.
Python works out the left side of and or or first, and it stops as soon as the answer is settled. With and, a false left side settles the answer, and Python does not work out the right side. With or, a true left side settles it.
In count != 0 and total / count > 5, Python works out the division only when count is not zero. When count is zero, the left side is False, and Python skips the right side.
4 The rule
When A and B are each True or False: A and B is True only if both are True.
A or B is True if at least one is True. not A is True if A is False, and False if A is True.
Without parentheses, comparisons group first, then not, then and, then or. Python works out the left side of and or or first, and works out the right side only when the left side has not settled the answer.
5 How
- Write the rule in words and underline each joining word: and, or, not.
- Write each part as a complete condition. A part is a comparison with its own left side, operator and right side, such as
height >= 120, or a name that already holdsTrueorFalse, such ashas_ticket. - Where the rule negates a part, write
notin front of that part, such asnot has_coat. - Join the parts with
andwhere both must hold and withorwhere one is enough. - If the rule mixes
andwithor, put parentheses around the part the rule treats as one group, as in(member or total > 50) and coupon. - Fill in a small table with one row for each case of the parts, True or False, and work out the whole condition for every row.
6 Worked example
Tall enough, with a ticket
height = int(input())
ticket = input()
has_ticket = ticket == "yes"
if height >= 120 and has_ticket:
print("Board the ride")
else:
print("Wait here")
Standard input
131
yes
Standard output
Board the ride
Save the code as ride.py, run it, type 131, press Enter, type yes and press Enter. The first line does two steps at once: input() hands back the typed text, and int makes a number from it. Both parts of the condition are True, so the whole condition is True.
Tall enough, without a ticket
height = int(input())
ticket = input()
has_ticket = ticket == "yes"
if height >= 120 and has_ticket:
print("Board the ride")
else:
print("Wait here")
Standard input
131
no
Standard output
Wait here
Run the file again and type 131, then no. The height part is True and the ticket part is False. and needs both, so the condition is False. A False condition sends Python to the else block.
Flip and join two named values
raining = True
has_coat = False
print(not raining)
print(raining and not has_coat)
print(raining or has_coat)
Standard input
Empty
Standard output
False
True
True
Save the five lines as rain.py. Predict the three output lines before you run the file. The middle line groups as raining and (not has_coat).
- Python Software Foundation, Python 3.13 Library Reference, Built-in Functions: input
- Python Software Foundation, Python 3.13 Library Reference, Built-in Functions: int
- Python Software Foundation, Python 3.13 Language Reference, 6.11 Boolean operations
- Python Software Foundation, Python 3.13 Language Reference, 8 Compound statements and 8.1 The if statement
- Python Software Foundation, Python 3.13 Language Reference, 6.17 Operator precedence
Trace
| Instruction | Value | Output | Explanation |
|---|---|---|---|
height = int(input()) | 131 | — | The user types 131, and int turns the text into a number. |
ticket = input() | "no" | — | The user types no. The reply is text. |
has_ticket = ticket == "yes" | False | — | The right side asks whether "no" equals "yes". It does not, so has_ticket names False. |
height >= 120 | True | — | 131 is at least 120. |
height >= 120 and has_ticket | False | — | True and False gives False, because and needs both. |
print("Wait here") | — | Wait here | The condition is False, so the else block runs. |
7 The trap
Each side of and or or must be an expression that stands on its own. English lets you say "older than 12 and younger than 18". In Python, < 18 is not an expression, so age > 12 and < 18 stops with a SyntaxError. Write age > 12 and age < 18.
The slip that raises no error is reply == "y" or "yes". Python reads it as (reply == "y") or "yes". The right side is the text "yes" on its own, and Python counts any text that is not empty as true. The condition therefore passes for every reply. Write reply == "y" or reply == "yes".
When and and or share a line, and groups more tightly. member or total > 50 and coupon means member or (total > 50 and coupon). If you mean the other grouping, write the parentheses.
- Python Software Foundation, Python 3.13 Language Reference, 6.11 Boolean operations
- Python Software Foundation, Python 3.13 Language Reference, 6.10 Comparisons
- Python Software Foundation, Python 3.13 Library Reference, Built-in Types: Truth Value Testing
- Python Software Foundation, Python 3.13 Language Reference, 6.17 Operator precedence
Quick checks
Quick check 1 of 2
Predict the two lines this program displays. The program reads no input.
temp = 31
windy = False
print(temp > 30 and not windy)
print(temp < 0 or windy)Show the steps and the answer
It displays True, then False.
Why: 31 > 30 is True and not False is True, so and gives True. 31 < 0 is False and windy is False, and or needs at least one True.
Source: Python Software Foundation, Python 3.13 Language Reference, 6.11 Boolean operations
Quick check 2 of 2
What does each print line display?
member = True
total = 10
coupon = False
print(member or total > 50 and coupon)
print((member or total > 50) and coupon)Show the steps and the answer
The first line displays True and the second displays False.
Why: Without parentheses, and groups before or, so the first line means member or (total > 50 and coupon). member is True, so or gives True. In the second line the parentheses group member or total > 50, which is True, and True and False is False.
Source: Python Software Foundation, Python 3.13 Language Reference, 6.17 Operator precedence
Sources
- Python 3.13 Library Reference, Built-in Types: Boolean Operations. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 6.11 Boolean operations. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 6.17 Operator precedence. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Library Reference, Built-in Functions: input. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Library Reference, Built-in Functions: int. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 8 Compound statements and 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 6.10 Comparisons. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Library Reference, Built-in Types: Truth Value Testing. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.