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 1
2 for count in range(1, 6):
3 print(count)
4
5 # Part 2: count the cities that start with L
6 cities = ["London", "Paris", "Leeds", "Rome"]
7 l_count = 0
8 for city in cities:
9 if city[0] == "L":
10 l_count = l_count + 1
11 print(l_count)
countcityl_count
1

Output

 

Step 1: The loop starts. range(1, 6) starts at 1, so count is 1 for the first pass.

1 / 16

Press Next to move one step at a time. Part 1 fills the count column; Part 2 fills city and l_count.

Step 1 of 16: The loop starts. range(1, 6) starts at 1, so count is 1 for the first pass..

Watch out: Before you press Next, guess what the next row will say. Then check the code and the table agree.

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 line has the bug?

This program should count the cities that start with L. The list has three of them (London, Lima and Lyon), so it should print 3. It prints 0. Pick the line where it goes wrong.

A student's program — which line goes wrong?

Computer Science · Python

Write the missing lines

This program should display the highest monthly rainfall in the list. The figures are all above 0, so highest can start at 0. Choose each missing line.

  1. rainfall = [12, 7, 31, 18]
  2. highest = 0highest holds the biggest value seen so far.
  3. for amount in rainfall:One pass for each item in the list, so 4 passes. amount is the current item.
  4. missing step
Which line is step 4?

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.

5 of 5 still to sort.

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

1A for loop knows how many times it will run before it starts: once per value range() gives, or once per item in the list.
2range(start, stop) counts up from start and stops just before stop. range(stop) starts at 0.
3A counter starts at 0 and goes up by 1 only when the if test is true.
4Use a for loop for a known number of repeats and a while loop when the number of repeats is not known in advance.

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?
Repeating a sequence of instructions within a program loop.
What are the two main types of iteration?
Count-controlled iteration, which repeats a set number of times, and condition-controlled iteration, which repeats until a condition is met.
Which Python loop is count-controlled, and which is condition-controlled?
A for loop is count-controlled. A while loop is condition-controlled.
What does a for loop know before it starts that a while loop does not?
How many times it will repeat: a set number of times when the loop begins.
In "for count in range(1, 6):", what do the 1 and the 6 mean?
1 is the starting value and 6 is the stopping value.
Is the stopping value of range() included in the loop?
No, it is exclusive. The loop stops just before it, so range(6) displays values up to 5.
range() is given only one number. Where does counting start?
At 0 by default. That one number is the stopping value, and the count goes up by 1.
What does "for city in cities:" do?
Runs the indented block once for each item in the list, inspecting each item in turn.
How do you count the items in a list that meet a condition?
Start a count variable at 0 and increase it by 1 each time an item passes the if test inside the loop.
What does city[0] refer to?
The first character of the string held in the loop variable city.
Are comparisons in Python case sensitive?
Yes. A capital letter in the list needs a capital letter in the test.
How do you find the lowest value in a list with a for loop?
Keep a variable for the minimum so far and replace it with the current item when the item is lower.
When is a while loop the better choice?
When the number of repeats is not known in advance, such as repeating until the user answers "no".

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 posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

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.

Helpful guides for parents and students