Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Repeating with loops

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

Thirty friends, one message

You want to send "Happy birthday" to 30 friends. Would you type it 30 times?

No. You would find a way to repeat the job. A program can repeat steps too. It can do it 30 times, or a million times, without getting tired.

Key words

WordWhat it meansExample
LoopA part of a program that repeats.for, while
IterationOne run through a loop. It also means repeating steps.the loop runs 5 times
Count-controlled loopA loop that runs a set number of times.for i in range(5)
Condition-controlled loopA loop that runs while a condition is True.while saved < 50
forThe Python word for a count-controlled loop.
whileThe Python word for a condition-controlled loop.
range()Makes the numbers a for loop counts through.range(5) gives 0 to 4
Infinite loopA loop that never stops, because its condition never becomes False.
VariableA name that holds a value.saved = 0
FunctionA named piece of code that does one job.print()
Trace tableA table where you write the value of each variable after each step.

The cells students fill in are left blank in this preview.

Count-controlled: the for loop

You know how many times to repeat. Use a for loop.

This is a count-controlled loop. Each run through the loop is one iteration. The function range(5) gives the numbers 0, 1, 2, 3 and 4. That is five iterations. In for i in range(5), the name i holds the current number.

Try it: a for loop

Code: python
for i in range(5):
    print("Happy birthday, friend number", i)

# Change 5 to 8 and run it again.
# Notice that the counting starts at 0.

What range() gives you

You writeThe loop countsHow many times
range(5)0, 1, 2, 3, 45
range(1, 6)1, 2, 3, 4, 55
range(3)0, 1, 23

The cells students fill in are left blank in this preview.

range() with two numbers

data-callouts.note.label

You can give range() two numbers: where to start and where to stop. The loop counts up by 1 and stops before the stop number.

Extra: range() with three numbers

CodeNumbers it givesHow it works
range(0, 10, 2)0, 2, 4, 6, 8Start at 0. Go up by 2. Stop before 10
range(3, 0, -1)3, 2, 1Start at 3. Go down by 1. Stop before 0

The cells students fill in are left blank in this preview.

Quick check: for loops

Have a go

Quick check: for loops

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Try it: loop over a list

Code: python
names = ["Sam", "Aisha", "Tom"]
for name in names:
    print("Happy birthday,", name)

# A loop can also build a list. Start with an empty list. Then use append to add to the end.
# i * i means i times i.
squares = []
for i in range(1, 6):
    squares.append(i * i)
print(squares)

The lesson goes on

11 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.