Lesson

Iteration with for loops

A PRIMM lesson on repeating code, counting patterns, and tracking a running total.

Open a shared Python workspace in seconds.

Lesson overview

Topic
Iteration
Level
GCSE
Learning objective
Use a for loop to repeat code a fixed number of times and explain how a running total changes.

1.Predict

Example code

total = 0
for i in range(1, 6):
    total = total + i
    print(i, total)
  • What do you think the program will print on each line?
  • Which value changes every time the loop repeats, and which value keeps growing?

2.Run

Open the code in Paired, run it, and compare the output with your prediction.

3.Investigate

  • This program starts with total set to 0 before the loop begins.
  • range(1, 6) controls how many times the loop runs and which values i takes.
  • Why does the loop stop after 5?
  • What job does total have inside the loop?

4.Modify

  • Change the loop so it adds numbers from 1 to 10.
  • Change the print line so it also labels each value clearly.

5.Make

Create your own loop that works out and prints the running total of the first 8 numbers.

Extension

Adapt the program so it only adds even numbers and explain how you changed the loop.

Need extra support?

Reece offers GCSE & A-Level Computer Science tutoring: reece-tutoring.co.uk