Paul's Python CoursePPC

for loops

Not met yet

A for loop repeats the indented lines under it, once for each value it is given: for i in range(3): runs its body three times. The colon starts the body. The first line back at the left edge is after the loop, and runs once.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks a line after a loop's indented body runs on every pass.
  • Thinks only the first indented line repeats, or that a later indented line runs once after the loop.
  • Thinks the body of a loop runs only once.

Where it comes up

A Python for loop repeats a block of code once for each item in a sequence: each number in a range(), each character in a string, each item in a list. You use it whenever the same steps should happen several times, so you write them once instead of copying lines. The loop is made of two parts: a header line, for name in something:, ending in a colon, and the indented lines under it, called the body. Python decides what belongs to the body purely from the indentation.

How it works

The thing after in can be anything Python can step through. A string gives its characters one at a time; a list gives its items in order:

for letter in "sun":
    print(letter)
for name in ["Ivo", "Rosa"]:
    print("Checking", name)
    print("Done with", name)
print("All names checked")
s
u
n
Checking Ivo
Done with Ivo
Checking Rosa
Done with Rosa
All names checked

Each pass runs the whole body, top to bottom, before the next pass starts: both print lines for Ivo, then both for Rosa. The last line is back at the left edge, so it is not part of either loop. It runs once, after the second loop has finished. The body can be as many lines as you like, as long as they share the same indentation (four spaces is the convention).

A header with no indented line under it isn't a loop at all, and Python refuses to run the file:

for tick in range(3):
print("Tick")
  File "main.py", line 2
    print("Tick")
    ^^^^^
IndentationError: expected an indented block after 'for' statement on line 1

A missing colon at the end of the header is a similar early stop: SyntaxError: expected ':'.

Common mistakes

  • Expecting a line after the loop to repeat. A line back at the left edge comes after the loop and runs once, when the loop is done. That is what you want for a final total; if the line should happen on every pass, indent it to match the body.
  • Thinking only the first indented line repeats. Every line indented under the header is in the body, and all of them run on every pass, in order. A later indented line does not wait for the loop to end.
  • Thinking the body runs only once. It runs once per value: three times for range(3), once per item for a list, and not at all for an empty list. If something happens once, it is outside the loop.

Related

range supplies the numbers for counting loops, and the loop variable explains the name after for. To check what a loop really does, trace it pass by pass. When you don't know in advance how many passes you need, use a while loop.