GCSE · Computer Science · Edexcel · Spec 1CP2
Programming – iteration over data structures
Four animals. One loop. A string that builds itself: cat, then catdog, then catdogmouse, then catdogmousegoat. Step through it below and call out each value before it appears.
Computer Science · Algorithms
Trace table — dry run the code
Before each Next step, say out loud what result will hold. Then check if you were right.
Ready when you are — step through one line at a time.
Predict, then check
Strings can be looped over just like lists. Work out which positions get visited first.
my_word = "sheep" for i in range(0, 5, 2): print(my_word[i]) What does this print?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- An array has a fixed size and holds one data type. A list can grow or shrink and can mix data types.
- In Python, a list can be used as an array if every element has the same type and nothing is added or deleted.
- Every item sits at an index, its position. Counting starts at 0, so planets[0] is Mercury and planets[1] is Venus.
Have a goplanets = ["Mercury", "Venus", …]. Before you look: what does planets[1] give you?
Venus
Counting starts at 0, so planets[1] is the second item. Answering Mercury means you counted from 1.
- for planet in planets hands the loop each item itself, one after another, until the list runs out.
- for i in range(len(planets)) hands the loop each index instead: 0, 1, 2 … and you print planets[i].
Have a goIn for i in range(len(planets)), what is i on the very first pass: "Mercury", 0 or 1?
0
i holds an index, not a planet, and indexes start at 0. The planet itself is planets[i].
- To build a string, start with an empty one and join each item on: cat, catdog, catdogmouse, catdogmousegoat.
- For numbers, start a total at 0 and add each value. Total divided by the number of values gives the average.
Have a goSam starts total = 1 "so it isn't empty", then loops over [2, 3] adding each value. What does Sam get, and what should it be?
Sam gets 6. It should be 5.
The loop adds onto whatever total starts as, so starting at 1 sneaks an extra 1 into the answer. A total starts at 0.
- Strings loop like lists: for letter in my_word gives each character, and range(len(my_word)) fits any length.
- A two-dimensional list is a list of lists, counted from 0 both ways: scores[0] is a whole row, scores[0][2] one item.
Have a goscores = [[4, 7, 9], [6, 2, 8]]. What is scores[1][0]?
6
scores[1] is the second row, [6, 2, 8], and index 0 picks its first item. Row first, then column, both from 0.
- Lists are dynamic, so a loop can add items as it runs with the append method, which puts each one on the end.
The big picture
A for loop can visit every item in a list, or every character in a string, either by handing you the item itself or by handing you its index. Each pass can add to a result. Lists and arrays count from 0, and a two-dimensional list needs a row index and then a column index.
Key points
Worked example
Problem
scores = [6, 9, 4, 5]. Use a loop to find the total of the scores, then their average.
⚠ Watch out
Two slips catch loads of people. One: thinking the first item is at index 1. It's at index 0. Two: reading scores[0] in a two-dimensional list as a single value. It's a whole row; scores[0][2] is one item.
Memory hook
Picture a row of shelves numbered from 0. A loop can grab each thing as it walks past, or read each shelf number and then look. Same shelves, same things, two ways of walking.
Check yourself
Cover the page. numbers = [3, 8, 5], total starts at 0 and a loop adds each number on. What is total after each pass, and why does it start at 0?
Flashcards
(13)What is an array?
What is a list?
When can a Python list be used as an array?
What is an index, and where does it start?
What does the loop variable hold in for planet in planets?
What does the loop variable hold in for i in range(len(planets))?
Why start result as an empty string before a concatenating loop?
How do you total and average a list of numbers with a loop?
How do you loop over the characters of a string?
Why use range(len(my_word)) in an index loop?
What is a two-dimensional list?
In a two-dimensional list, what is the difference between scores[0] and scores[0][2]?
What does append do?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
- Functions vs procedures
- Global vs local variables
- Identifying and correcting program errors
- Primitive data types
- Programming – condition-controlled repetition
- Programming – count-controlled repetition
- Programming – selection
- Programming – sequencing
- Programming arithmetic operators
- Programming logical operators
- Programming relational operators
- Reading, writing, analysing and refining programs
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 26 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.