Read a for loop
Leans on
Quick review (2 minutes)
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.
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.
Find the sequence after
inand write its items in order. A string gives one item for each character.Find the body: the lines indented under the
forline.For each item, write the loop name's value, then run the body's lines in order with that value.
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
forline.Count the passes: one for each item, unless a
breakends the loop sooner. Check that the output holds what the body displays once for each pass.
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
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.
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
- Find the sequence after
inand write its items in order. A string gives one item for each character. - Find the body: the lines indented under the
forline. - For each item, write the loop name's value, then run the body's lines in order with that value.
- 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
forline. - Count the passes: one for each item, unless a
breakends 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
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
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
| Step | letter | Output | Explanation |
|---|---|---|---|
| Pass 1 | M | M | The loop name gets the first character, and the body displays it. |
| Pass 2 | A | A | The loop name gets the next character, and the body runs again. |
| Pass 3 | P | P | The last character. After this pass there is no next item. |
print("done") | P | done | The 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.
for c in "hi":
print(c)
print(c)Show the steps and the 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.
word = ""
for ch in word:
print("x")
print("after")Show the steps and the 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
- Python 3.13 Tutorial, 4.2 for Statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Language Reference, 8.3 The for statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
- Python 3.13 Tutorial, 3.2 First Steps Towards Programming. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.