1. Concept library
  2. Python Foundations
  3. Module 2: Control flow

Concept

for loops and range

A for loop visits each item of a sequence in turn. range(n) produces the numbers 0 to n-1, which is how you loop a fixed number of times or over indexes.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what range(n) produces and why it stops at n - 1
  2. Write a for loop over a list directly, and one over indexes with enumerate
  3. Predict the output of a loop over range with a start, stop and step

In practice

A few lines that show for loops and range doing its job.

Python
names = ["Ada", "Grace", "Linus"]

for name in names:
    print(name.upper())

for i in range(3):
    print(i)          # 0, 1, 2

for i, name in enumerate(names, start=1):
    print(i, name)

The common mistake

Watch for

Using range(len(names)) and indexing when you only need the items. Loop over the list directly, and reach for enumerate() when you also want the position.

What to know first

Your roadmap puts these before for loops and range, unless you already know them.

How it stays learned

Once a session shows you understand for loops and range, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on for loops and range.

Previous in the module← while loops