Quarry School

Choose a branch

Subject: Computing
Level 1 · FoundationsAbout 8 min read4 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

    A footpath reaches a garden gate with a notice.

    Rule

    Write if, a condition and a colon. Indent the block beneath it, by four spaces in this course. Python runs the block only when the condition is true. An optional else: line, lined up with the if, has its own indented block, which runs only when the condition is false. Lines after the whole statement, back at the left margin, run in both cases.

    Step

    1. Write the decision as two sentences, the first starting with if and the second with otherwise. The baggage rule invented for this lesson reads: if the bag is over the weight limit, charge a fee. Otherwise, charge nothing.

    2. Turn the first sentence into an if line. Write if, then the condition, then a colon: if weight > 23:.

    3. On the next lines, indented by four spaces, write what happens when the condition is True.

    4. If something different happens otherwise, write else: lined up with if, and indent its lines by four spaces too.

    5. Return to the left margin for the lines that run in both cases.

    6. Trace the program twice, once with a value that makes the condition True and once with a value that makes it False.

    Trap

    A line with the wrong indentation can still be a legal program. Take an if statement that has no else, and suppose its block should display two lines. Leave the second at the margin and Python reports no error. It treats that line as the first line after the if statement and runs it whatever the condition gave. Check the left edge of every line when a program does more than you expected.

    When an else: follows, the same slip stops the program. A line at the margin ends the if statement, so the else: below it has no if to belong to, and Python reports a SyntaxError before any line runs.

    A missing colon is caught before the program starts. if weight > 23 with no colon stops with a SyntaxError before any line runs, and the message marks the place in that line where Python found the problem.

    Keep to four spaces for each level, and do not mix tabs with spaces. Start every statement of one block at the same depth. The blocks of statements nested inside it are indented further. A line whose depth fits no block stops the program with an IndentationError.

    1 In plain words

    A footpath reaches a garden gate with a notice. If the gate is open, the notice says, cut through the garden. Otherwise, walk around the wall. You take one route, never both. Past the garden the two routes join and you carry on.

    An if statement is that notice. Its first line holds the word if, then a condition, then a colon. A condition is an expression that Python judges to be true or false. In the worked programs of this unit every condition works out to True or False, like the comparisons from the last lesson. The lines to run when the condition is true go underneath, each one pushed in by four spaces more than the if line. A block inside that block goes four spaces further in, as the last lesson of this unit shows. Pushing a line in is called indenting it, and the indented lines together form a block.

    A line else:, lined up with the if line, opens a second block. Python runs it when the condition is false. The else part is optional. Without it, a false condition makes Python skip the first block and do nothing in its place. Each route is called a branch.

    A block ends at the first line that is indented less, or at the end of the file. When the whole if statement is done, Python carries on with the statement that follows it, whichever branch it took.

    Remember one sentence: the colon opens a block, and the indentation says which lines belong to it. To look it up, see section 4.1 of the Python 3.13 tutorial, on if statements.

    2 Picture it

    Lineweight 30weight 18if lineTrueFalseif blockrunsskippedelse blockskippedrunslast linerunsruns

    A table for the bag program with a column for the run where weight is 30 and a column for the run where weight is 18. On the if line the condition is True when weight is 30 and False when weight is 18. The if block runs in the first run and is skipped in the second. The else block is skipped in the first run and runs in the second. The last line runs in both.

    One block runs and the other is skipped

    3 Why

    Python reads the indentation at the start of each line to decide which lines form a group. Lines indented by the same amount under an if line belong to its block. A line with less indentation is outside the block.

    When Python reaches the if line, it works out the condition. If the condition is true, it runs the first block and passes over the else block. If the condition is false, it passes over the first block and runs the else block. Exactly one of the two runs.

    The colon and the indentation work as a pair. The colon tells Python that a block follows. If the statement after the if line is not indented, Python finds no block and stops with an IndentationError before it runs any line of the file.

    4 The rule

    Write if, a condition and a colon.

    Indent the block beneath it, by four spaces in this course. Python runs the block only when the condition is true. An optional else: line, lined up with the if, has its own indented block, which runs only when the condition is false. Lines after the whole statement, back at the left margin, run in both cases.

    5 How

    1. Write the decision as two sentences, the first starting with if and the second with otherwise. The baggage rule invented for this lesson reads: if the bag is over the weight limit, charge a fee. Otherwise, charge nothing.
    2. Turn the first sentence into an if line. Write if, then the condition, then a colon: if weight > 23:.
    3. On the next lines, indented by four spaces, write what happens when the condition is True.
    4. If something different happens otherwise, write else: lined up with if, and indent its lines by four spaces too.
    5. Return to the left margin for the lines that run in both cases.
    6. Trace the program twice, once with a value that makes the condition True and once with a value that makes it False.

    6 Worked example

    Type 30 for a bag over the limit

    Python code
    reply = input()
    weight = int(reply)
    if weight > 23:
        print("Heavy bag: pay the fee")
    else:
        print("No fee")
    print("Next traveler")
    

    Standard input

    30
    

    Standard output

    Heavy bag: pay the fee
    Next traveler
    

    Save the code as bag.py, run it, type 30 and press Enter. The condition 30 > 23 is True, so the first block runs and the else block is skipped.

    Type 18 for a bag under the limit

    Python code
    reply = input()
    weight = int(reply)
    if weight > 23:
        print("Heavy bag: pay the fee")
    else:
        print("No fee")
    print("Next traveler")
    

    Standard input

    18
    

    Standard output

    No fee
    Next traveler
    

    Run the same file and type 18. The condition 18 > 23 is False, so Python skips the first block and runs the else block. The last line runs in both cases.

    Leave the block at the margin

    Python code
    weight = 30
    if weight > 23:
    print("Heavy bag: pay the fee")
    

    Standard input

    Empty

    Standard output

    Empty

    Exception IndentationError

    The print line is not indented, so the if line has no block. Python reports an IndentationError that names line 3 and displays nothing, because it found the problem before running line 1. Indent line 3 by four spaces to repair it.

    Trace

    Trace the bag program when the user types 30
    InstructionWhat happensOutputExplanation
    reply = input()The user types 30—The name reply holds the text "30".
    weight = int(reply)weight is 30—int turns the text into the number 30.
    if weight > 23:30 > 23 is True—The condition is True, so Python enters the first block.
    print("Heavy bag: pay the fee")RunsHeavy bag: pay the feeThis is the only line of the first block.
    else:Skipped with its block—The first block ran, so the else block does not.
    print("Next traveler")RunsNext travelerThis line is back at the margin. It runs for every bag.

    7 The trap

    A line with the wrong indentation can still be a legal program. Take an if statement that has no else, and suppose its block should display two lines. Leave the second at the margin and Python reports no error. It treats that line as the first line after the if statement and runs it whatever the condition gave. Check the left edge of every line when a program does more than you expected.

    When an else: follows, the same slip stops the program. A line at the margin ends the if statement, so the else: below it has no if to belong to, and Python reports a SyntaxError before any line runs.

    A missing colon is caught before the program starts. if weight > 23 with no colon stops with a SyntaxError before any line runs, and the message marks the place in that line where Python found the problem.

    Keep to four spaces for each level, and do not mix tabs with spaces. Start every statement of one block at the same depth. The blocks of statements nested inside it are indented further. A line whose depth fits no block stops the program with an IndentationError.

    Quick checks

    Quick check 1 of 2

    This program has no else. What does it display? What would it display if the first line were stock = 9?

    Python code
    stock = 3
    if stock < 5:
        print("Order more")
    print("Count done")
    Show the steps and the answer
    Answer

    With stock = 3 it displays Order more, then Count done. With stock = 9 it displays only Count done.

    Why: 3 < 5 is True, so the block runs. 9 < 5 is False, and with no else Python skips the block and does nothing in its place. The last line is at the margin, so it runs both times.

    Source: Python Software Foundation, Python 3.13 Tutorial, 4.1 if Statements

    Quick check 2 of 2

    What does this program display? Then suppose the line print("Keep the change") loses its four spaces and starts at the margin. What does the program display now?

    Python code
    coins = 2
    if coins >= 5:
        print("Buy a ticket")
        print("Keep the change")
    print("Walk home")
    Show the steps and the answer
    Answer

    As written it displays only Walk home. With that line at the margin it displays Keep the change, then Walk home.

    Why: 2 >= 5 is False, so both indented lines are skipped. A line at the margin is outside the block, so it runs whatever the condition gave.

    Source: Python Software Foundation, Python 3.13 Language Reference, 2.1.8 Indentation

    Sources

    1. 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.
    2. Python 3.13 Tutorial, 4.10 Intermezzo: Coding Style. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    3. Python 3.13 Tutorial, 4.1 if Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    4. Python 3.13 Language Reference, 2.1.8 Indentation. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    5. Python 3.13 Library Reference, Built-in Exceptions: IndentationError. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
    6. Python 3.13 Tutorial, 8.1 Syntax Errors. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.