KS3 · Computer Science
Iteration: FOR loops in Python
range(6) never prints 6: it counts 0 to 5 and stops. Work out why, and you can predict any for loop before it runs.
Computer Science · Python
Step through two for loops
Part 1 counts with range(). Part 2 loops over a list and counts matches. Press Next and watch the variables change, one pass at a time.
Where does Part 1 stop, and why does the counter in Part 2 only move on some passes?
1 # Part 1: count from 12 for count in range(1, 6):3 print(count)45 # Part 2: count the cities that start with L6 cities = ["London", "Paris", "Leeds", "Rome"]7 l_count = 08 for city in cities:9 if city[0] == "L":10 l_count = l_count + 111 print(l_count)
| count | city | l_count |
|---|---|---|
| 1 |
Output
Step 1: The loop starts. range(1, 6) starts at 1, so count is 1 for the first pass.
Press Next to move one step at a time. Part 1 fills the count column; Part 2 fills city and l_count.
Predict, then check
One number inside the brackets. What does that number mean, and where does the counting start?
What does "for count in range(6): print(count)" show on the screen?
Computer Science · Python
Which loop does the job?
Pick a task, then pick the loop that suits it. Read the reason before you move on.
Still to sort
for loop (0)
Count-controlled: repeats a set number of times.
Where the line is: The number of repeats is set when the loop begins.
while loop (0)
Condition-controlled: repeats for as long as its condition is true.
Where the line is: The number of repeats is not known in advance and can vary each time the loop runs.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
What you need to know
- Iteration repeats instructions in a loop. A for loop is count-controlled: a set number of times, known when it begins.
- A while loop is condition-controlled instead. It repeats for as long as its condition is true.
- In "for count in range(1, 6):", the 1 is the starting value and the 6 is the stopping value.
- The stopping value is exclusive. The loop stops just before it and never reaches it.
Have a goMaya is very sure that range(1, 6) gives 1, 2, 3, 4, 5 and 6. How many passes does the loop actually make?
5 passes: the values are 1, 2, 3, 4 and 5.
The stopping value is exclusive, so the loop stops just before 6. Maya counted the stopping value as one of the passes.
- Give range() only one number and that is the stopping value; counting starts at 0 and goes up by 1.
Have a goYou want a loop that makes exactly 7 passes, counting up from 0. What goes inside the brackets of range( )?
range(7), which gives 0, 1, 2, 3, 4, 5, 6.
One number is the stopping value, and counting starts at 0 by default. So 7 is left out and you get 7 values from 0 to 6.
- To loop through a list, write "for city in cities:". The indented block runs once for each item.
- city[0] is the first character of the string in the loop variable, so city[0] == "L" tests the first letter.
- An if statement inside the loop tests each item; a counter starting at 0 goes up by 1 when one passes.
- Comparisons are case sensitive, so a capital letter in the list needs a capital letter in the test.
- To find the highest value, keep a variable for the highest so far and replace it whenever an item is higher.
Have a gohighest starts at 0. A loop meets the items 3, then 8, then 5, replacing highest whenever an item is higher. What is highest after each item?
3, then 8, then 8.
The 5 is not higher than the 8 already held, so highest is left alone. The variable only changes when an item beats it.
The big picture
A for loop repeats a block of code a set number of times, and that number is known when the loop begins. It might come from range(), where the stopping value is left out, or from the number of items in a list. Put an if statement and a counter inside the loop to count the items that pass a test, or to track the highest value. When the number of repeats is not known in advance, use a while loop instead.
Key points
Worked example
Problem
A program has a list of ages: ages = [10, 12, 13, 11, 14]. A loop should count how many ages are 12 or over. Work through the loop and say what is displayed.
⚠ Watch out
Expecting range(1, 6) to include 6. It does not: the stopping value is left out, so you get 1, 2, 3, 4, 5. Another trap is treating for and while as interchangeable. A for loop is count-controlled, and a while loop runs until its condition changes.
Memory hook
Think of the stopping value as a stop sign: the loop halts before it and never drives through it. So range(6) stops before 6 and counts 0, 1, 2, 3, 4, 5.
Check yourself
Cover the screen. Which values does n take in "for n in range(3, 7):", how many passes is that, and which number is left out? (3, 4, 5, 6; 4 passes; 7.)
Flashcards
(13)What is iteration?
What are the two main types of iteration?
Which Python loop is count-controlled, and which is condition-controlled?
What does a for loop know before it starts that a while loop does not?
In "for count in range(1, 6):", what do the 1 and the 6 mean?
Is the stopping value of range() included in the loop?
range() is given only one number. Where does counting start?
What does "for city in cities:" do?
How do you count the items in a list that meet a condition?
What does city[0] refer to?
Are comparisons in Python case sensitive?
How do you find the lowest value in a list with a for loop?
When is a while loop the better choice?
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 KS3 Computer Science topics
How this lesson was checked. This KS3 Computer Sciencelesson 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 7 November 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.