Quarry School

Combine two conditions

Subject: Computing
Level 1 · FoundationsAbout 7 min read2 new terms
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    Here are two signs invented for this lesson.

    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.

    Step

    1. Write the rule in words and underline each joining word: and, or, not.

    2. 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 holds True or False, such as has_ticket.

    3. Where the rule negates a part, write not in front of that part, such as not has_coat.

    4. Join the parts with and where both must hold and with or where one is enough.

    5. If the rule mixes and with or, put parentheses around the part the rule treats as one group, as in (member or total > 50) and coupon.

    6. 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.

    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.

    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, BA and BA or BTrue, TrueTrueTrueTrue, FalseFalseTrueFalse, TrueFalseTrueFalse, FalseFalseFalse

    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.

    The four cases of A and B, and of A or B

    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

    1. Write the rule in words and underline each joining word: and, or, not.
    2. 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 holds True or False, such as has_ticket.
    3. Where the rule negates a part, write not in front of that part, such as not has_coat.
    4. Join the parts with and where both must hold and with or where one is enough.
    5. If the rule mixes and with or, put parentheses around the part the rule treats as one group, as in (member or total > 50) and coupon.
    6. 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

    Python code
    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

    Python code
    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

    Python code
    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).

    Trace

    Trace the ride program for a height of 131 and the reply no
    InstructionValueOutputExplanation
    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 >= 120True—131 is at least 120.
    height >= 120 and has_ticketFalse—True and False gives False, because and needs both.
    print("Wait here")—Wait hereThe 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.

    Quick checks

    Quick check 1 of 2

    Predict the two lines this program displays. The program reads no input.

    Python code
    temp = 31
    windy = False
    print(temp > 30 and not windy)
    print(temp < 0 or windy)
    Show the steps and the answer
    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?

    Python code
    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
    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

    1. Python 3.13 Library Reference, Built-in Types: Boolean Operations. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    2. Python 3.13 Language Reference, 6.11 Boolean operations. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    3. Python 3.13 Language Reference, 6.17 Operator precedence. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    4. Python 3.13 Library Reference, Built-in Functions: input. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    5. Python 3.13 Library Reference, Built-in Functions: int. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    6. 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.
    7. Python 3.13 Language Reference, 6.10 Comparisons. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    8. Python 3.13 Library Reference, Built-in Types: Truth Value Testing. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.