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
- Count-controlled iteration is implemented using for loops in Python.
- The range() function can be used with a for loop to specify the number of times that a for loop should iterate.
- 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
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?
Q2. are organised collections of data.
Q3.What is the term for a variable that is used to signal a change in a program's state?
Q4.A while loop will continue to iterate until ...
Q5.What would be displayed if this code was run?
1234count = 1
while True:
print(count)
count = count + 1Code colour
Q6.What item is held in rolls[3] of this list?
1rolls = [1, 4, 3, 6]Code colour
Assessment exit quiz
6 Questions
Q1.Which type of loop is used in Python for count-controlled iteration?
Q2.What will the following code display?
12for count in range (1,8):
print(count)Code colour
Q3.A loop is used for condition-controlled iteration.
Q4.What does the len() function do?
Q5.What would be the output of this code when it is run?
12345rolls = [1, 4, 3, 6]
count = 0
for dice in rolls:
count = count + 1
print(count)Code colour
Q6.What would be the output of this code when it is run?
123456rolls = [1, 4, 3, 6]
count = 0
for dice in rolls:
if dice >= 3:
count = count + 1
print(count)Code colour
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...
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.
The starter quiz will activate and check your pupils' prior knowledge, with versions available both with and without answers in PDF format.
We use learning cycles to break down learning into key concepts or ideas linked to the learning outcome. Each learning cycle features explanations with checks for understanding and practice tasks with feedback. All of this is found in our slide decks, ready for you to download and edit. The practice tasks are also available as printable worksheets and some lessons have additional materials with extra material you might need for teaching the lesson.
The assessment exit quiz will test your pupils' understanding of the key learning points.
Our video is a tool for planning, showing how other teachers might teach the lesson, offering helpful tips, modelled explanations and inspiration for your own delivery in the classroom. Plus, you can set it as homework or revision for pupils and keep their learning on track by sharing an online pupil version of this lesson.
Explore more key stage 3 computing lessons from the Python programming with sequences of data unit, dive into the full secondary computing curriculum, or learn more about lesson planning.