07-for-loops.py
python-for-j277 / control-flow  ·  Lesson 07
Lesson 07 — Control flow

Iteration: For Loops

Repeat a block of code once for each item in a sequence.

A for loop repeats a block of code once for each item in a sequence — the characters of a string, the values in a list. That is all a for loop ever does, and there is only one kind of for loop. This page shows you all of it.

The shape of every for loop

Every for loop you will ever write has the same shape: for item in sequence: followed by an indented body. The loop takes each item of the sequence in turn, puts it in the variable, and runs the body once for it. When the items run out, the loop ends.

hello.py
names = ["Ava", "Ben", "Cara"]
for name in names:
    print("Hello", name)
Output
Hello Ava
Hello Ben
Hello Cara
How it works
  • names is a sequence — a list holding three values in order.
  • for name in names means: take each value in turn and call it name.
  • The indented body runs once per item — three items, three passes.
  • name is an ordinary variable. You choose its name; the loop fills it.

Watch what name holds on each pass — the loop simply moves along the sequence:

Passname holdsWhat prints
1"Ava"Hello Ava
2"Ben"Hello Ben
3"Cara"Hello Cara

A string is a sequence too

A string is a sequence of characters, so the very same loop steps through it letter by letter.

letters.py
word = "code"
for letter in word:
    print(letter)
Output
c
o
d
e
How it works
  • Nothing about the loop changed — only the sequence after in.
  • A 4-character string means 4 passes, with letter holding one character each time.
There is only one for loopA for loop does not care what kind of sequence you give it — a list, a string, or (next lesson) numbers made by range(). The loop always does exactly the same job: visit each item in turn and run the body once for it. If you can read for item in sequence:, you can read every for loop ever written.

Doing something with each item

The body can do anything with the current item — like adding each value to a running total.

totals.py
prices = [3, 5, 2]
total = 0
for price in prices:
    total = total + price
print("Total:", total)
Output
Total: 10
How it works
  • total starts at 0 before the loop.
  • Each pass adds the current price to total: first 3, then 5, then 2.
  • After the last item the loop ends, and total holds 10.
Naming the loop variablePick a name that describes one single item: for price in prices, for letter in word, for name in names. It makes the body read like English.
When to use itUse a for loop when you want to do the same thing to every item in a sequence. Use a while loop when you do not know in advance how many repeats you need.

What you have learned

  • A for loop runs its body once for each item in a sequence.
  • Its shape is always the same: for item in sequence: plus an indented body.
  • The loop variable is a normal variable that holds the current item on each pass.
  • Strings and lists are both sequences — the loop treats them exactly the same way.
  • There is only one for loop. What changes is the sequence you give it.