Choose exam board for KS4 Computer Science (GCSE)
Choose exam board for KS4 English
Choose exam board for KS4 French
Choose exam board for KS4 Geography
Choose exam board for KS4 German
Choose exam board for KS4 History
Choose tier for KS4 Maths
Choose exam board for KS4 Music
Choose exam board for KS4 Physical education (GCSE)
Choose exam board for KS4 Religious education (GCSE)
Choose exam board for KS4 Spanish

      Using for loops to iterate data structures

      Lesson details

      Learning outcome

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

      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 - an iterative statement that will repeat for the length of a given sequence

      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.

      Teacher tip

      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.

      Equipment

      Pupils will need access to a device that can edit and run Python programs. Examples in this lesson use the RPF Code Editor https://oak.link/python-editor

      Licence

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

      Lesson video

      Loading...

      Prior knowledge starter quiz

      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
      Code colour

      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]
      Code colour

      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)
      Code colour

      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)
      Code colour

      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)
      Code colour

      1
      Correct answer: 3
      2
      0

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