Quarry School

Read a for loop

Subject: Computing
Level 1 · FoundationsAbout 6 min read3 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.

    Leans on

    Quick review (2 minutes)

    In plain words

    A ticket checker at a gate takes the tickets of the people in line one at a time, from the front, and does the same check on each.

    Rule

    A for loop over a sequence runs its indented body once for each item, in order, unless a break ends it sooner. Before each pass, the name after for is given the next item. When the items run out, the program carries on with the first line after the body. An empty sequence gives no passes.

    Step

    1. Find the sequence after in and write its items in order. A string gives one item for each character.

    2. Find the body: the lines indented under the for line.

    3. For each item, write the loop name's value, then run the body's lines in order with that value.

    4. After the last pass, move to the first line after the body. In this unit's programs that is the next line indented no further than the for line.

    5. Count the passes: one for each item, unless a break ends the loop sooner. Check that the output holds what the body displays once for each pass.

    Trap

    A line meant to run once can slip into the body. If print("done") were indented under the for line, it would run on every pass and the output would hold done three times. Check the indentation of each line below a for line.

    The loop name keeps its last value after the loop ends. After the loop over "MAP", letter still holds P. Use the loop name inside the body, where it holds the current item.

    1 In plain words

    A ticket checker at a gate takes the tickets of the people in line one at a time, from the front, and does the same check on each. When the line is empty, the checker stops.

    A for loop works through the items of a sequence in the same way. It runs its indented body once for each item, in the order the items appear. A string is a sequence of characters, so a for loop over a string runs its body once for each character.

    Before each pass, the name after for, the loop name, is given the next item. The body can use that name to work with the current item.

    A break, taught in the last lesson of this unit, can end a loop sooner.

    Remember one sentence: a for loop gives its name each item in turn and runs the body once for each, unless a break ends it sooner. To look it up, see section 4.2 of the Python 3.13 tutorial, on for statements.

    2 Picture it

    Passletter holdsDisplayed1MM2AA3PP

    A table of the three passes of a for loop over the string MAP. On pass 1 the name letter holds M and the body displays M. On pass 2 it holds A and the body displays A. On pass 3 it holds P and the body displays P. These are the passes of the first worked example. The first row is marked.

    The three passes of the loop over the string MAP

    3 Why

    Python works out the sequence once, at the for line. It then gives the loop name the first item and runs the body. When the body reaches its end, the loop name gets the next item and the body runs again. When there is no next item, the loop ends and Python carries on with the first line after the body.

    The body is the block of lines indented under the for line. The lines directly in it share one indentation; a block inside it, such as an if block, is indented further. The first line after the body, indented no further than the for line, is not part of the loop. In the programs of this unit it runs once, when the loop has ended.

    A sequence with no items gives no passes. A for loop over the empty string "" never runs its body, and the loop name is not given a value by the loop.

    4 The rule

    A for loop over a sequence runs its indented body once for each item, in order, unless a break ends it sooner.

    Before each pass, the name after for is given the next item. When the items run out, the program carries on with the first line after the body. An empty sequence gives no passes.

    5 How

    1. Find the sequence after in and write its items in order. A string gives one item for each character.
    2. Find the body: the lines indented under the for line.
    3. For each item, write the loop name's value, then run the body's lines in order with that value.
    4. After the last pass, move to the first line after the body. In this unit's programs that is the next line indented no further than the for line.
    5. Count the passes: one for each item, unless a break ends the loop sooner. Check that the output holds what the body displays once for each pass.

    6 Worked example

    Display each character of a string

    Python code
    for letter in "MAP":
        print(letter)
    print("done")
    

    Standard input

    Empty

    Standard output

    M
    A
    P
    done
    

    Predict the four output lines first. Then save the code as letters.py and run the file, or follow the trace below. The body runs once for each of the three characters, and done is displayed once, after the loop.

    A body of two lines

    Python code
    for letter in "ab":
        print(letter)
        print("-")
    print("end")
    

    Standard input

    Empty

    Standard output

    a
    -
    b
    -
    end
    

    Both print lines are indented, so both belong to the body and both run on each pass. The last line is at the margin, so it runs once.

    Trace

    Trace the loop over the string MAP
    StepletterOutputExplanation
    Pass 1MMThe loop name gets the first character, and the body displays it.
    Pass 2AAThe loop name gets the next character, and the body runs again.
    Pass 3PPThe last character. After this pass there is no next item.
    print("done")PdoneThe loop has ended. The line at the margin runs once.

    7 The trap

    A line meant to run once can slip into the body. If print("done") were indented under the for line, it would run on every pass and the output would hold done three times. Check the indentation of each line below a for line.

    The loop name keeps its last value after the loop ends. After the loop over "MAP", letter still holds P. Use the loop name inside the body, where it holds the current item.

    Quick checks

    Quick check 1 of 2

    Predict what this program displays. The program reads no input.

    Python code
    for c in "hi":
        print(c)
        print(c)
    Show the steps and the answer
    Answer

    It displays h, h, i and i, each on its own line.

    Why: The string has two characters, so the body runs twice. Both print lines are in the body, so each pass displays its character twice.

    Source: Python Software Foundation, Python 3.13 Tutorial, 4.2 for Statements

    Quick check 2 of 2

    Predict what this program displays. The program reads no input.

    Python code
    word = ""
    for ch in word:
        print("x")
    print("after")
    Show the steps and the answer
    Answer

    It displays only after.

    Why: The empty string has no characters, so the loop makes no passes and its body never runs. The last line is at the margin and runs once.

    Source: Python Software Foundation, Python 3.13 Language Reference, 8.3 The for statement

    Sources

    1. Python 3.13 Tutorial, 4.2 for Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Language Reference, 8.3 The for statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Tutorial, 3.2 First Steps Towards Programming. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.