icon-background-square
New
New
Year 9

Using for loops to iterate data structures

I can use a for loop to inspect every element of a list.

icon-background-square
New
New
Year 9

Using for loops to iterate data structures

I can use a for loop to inspect every element of a list.

warning

These resources will be removed by end of Summer Term 2025.

Switch to our new teaching resources now - designed by teachers and leading subject experts, and tested in classrooms.

Lesson details

Key learning points

  1. Count-controlled iteration is implemented using for loops in Python.
  2. The range() function can be used with a for loop to specify the number of times that a for loop should iterate.
  3. For loops can be used to iterate through each item held in a list to inspect each item in turn.

Keywords

  • Iteration - the process of repeating a sequence of instructions within a program loop

  • For loop - a count-controlled loop used to repeat a specific block of code a known number of times

Common misconception

A for loop is the same as a while loop as they are both designed to repeat instructions. You should pick one and stick to it for all your programs.

Unlike a while loop, a for loop is count-controlled, meaning it will repeat a set number of times when the loop begins. In contrast, a while loop repeats instructions for as long as its condition is true. This can vary each time the loop runs.


To help you plan your year 9 computing lesson on: Using for loops to iterate data structures, download all teaching resources for free and adapt to suit your pupils' needs...

Unplugged activites help to visualise tasks away from the computer screen. If you think pupils will struggle to understand the tasks, try rehersing them in groups with printed versions of the lists so that the effect of instructions can be visualised.
speech-bubble
Teacher tip
equipment-required

Equipment

All pupils require access to devices that can edit and run Python programs. Starter code files are available to copy or use directly via the Raspberry Pi Foundation's Code Editor.

copyright

Licence

This content is © Oak National Academy Limited (2025), licensed on Open Government Licence version 3.0 except where otherwise stated. See Oak's terms & conditions (Collection 2).

Lesson video

Loading...

6 Questions

Q1.
The process of repeating a sequence of instructions within a program loop is the definition of which term?
Correct answer: iteration
sequence
selection
a while loop
Q2.
are organised collections of data.
Correct Answer: Data structures, ata tructures, ata structures
Q3.
What is the term for a variable that is used to signal a change in a program's state?
Correct Answer: flag, a flag
Q4.
A while loop will continue to iterate until ...
a counter reaches a certain value.
Correct answer: a condition is met.
the program crashes.
Q5.
What would be displayed if this code was run?
1234
count = 1 while True: print(count) count = count + 1
1 2 3 4 5
The code will not run.
Correct answer: The code will print 1, 2, 3 ... and so on until the program is stopped manually.
Q6.
What item is held in rolls[3] of this list?
1
rolls = [1, 4, 3, 6]
1
4
3
Correct answer: 6

6 Questions

Q1.
Which type of loop is used in Python for count-controlled iteration?
Correct Answer: for, for loop
Q2.
What will the following code display?
12
for count in range (1,8): print(count)
01234567
Correct answer: 1234567
12345678
012345678
Q3.
A loop is used for condition-controlled iteration.
Correct Answer: while
Q4.
What does the
len()
function do?
prints a list or string
sorts a list or string
iterates through a list or string
Correct answer: returns the length of a list or string
Q5.
What would be the output of this code when it is run?
12345
rolls = [1, 4, 3, 6] count = 0 for dice in rolls: count = count + 1 print(count)
0
1
Correct answer: 4
3
Q6.
What would be the output of this code when it is run?
123456
rolls = [1, 4, 3, 6] count = 0 for dice in rolls: if dice >= 3: count = count + 1 print(count)
1
Correct answer: 3
2
0