All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Count-controlled iteration: for loops and range()

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

A count-controlled loop repeats a fixed number of times, or once for every item in a structure.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

for with range() (spec 7.1.1)

range(stop) gives 0 up to stop - 1. range(start, stop) starts at start. range(start, stop, step) counts in steps. The stop value is never included.

for i in range(3):
    print(i, end=" ")
print()
for i in range(2, 11, 4):
    print(i, end=" ")
print()
for i in range(5, 0, -1):
    print(i, end=" ")

Output:

0 1 2 
2 6 10 
5 4 3 2 1 

Looping over a structure

A for loop can visit every item of a list, string, tuple, set or dictionary directly. To get the index as well, loop over range(len(items)).

names = ["Ana", "Ben", "Cai"]
for name in names:
    print(name.upper())
for i in range(len(names)):
    print(i, names[i])

Output:

ANA
BEN
CAI
0 Ana
1 Ben
2 Cai

Running totals and counts

Initialise before the loop, update inside it, use the result after it:

marks = [12, 18, 9, 15]
total = 0
count = 0
for m in marks:
    total = total + m
    if m >= 12:
        count = count + 1
print(total, count)

Output:

54 3

Nested loops

A loop inside a loop runs the inner loop completely for each pass of the outer loop. Two nested loops over n items do n x n passes, which matters for efficiency.

for row in range(1, 4):
    line = ""
    for col in range(1, 4):
        line = line + str(row * col) + " "
    print(line)

Output:

1 2 3 
2 4 6 
3 6 9 

Worked example

How many times does the body of for i in range(3, 20, 4) run, and what values does i take?

  1. Start at 3 and add 4 each time while i < 20.
  2. Values: 3, 7, 11, 15, 19
  3. Count them: 5

Answer: 5 times

Key idea: range(start, stop, step) never includes stop; loop over items directly or over range(len(items)) for indexes; initialise totals before the loop.

Check yourself

  1. What is displayed when this code runs? ```python for i in range(1, 6, 2): print(i, end=" ") ```
    Show answer

    1 3 5 . range(1, 6, 2) gives 1, 3, 5 (6 is excluded), so 1 3 5.

  2. What is the value of total after this code runs? ```python total = 0 for k in range(1, 5): total = total + k * k ```
    Show answer

    30. 1 + 4 + 9 + 16 = 30. range(1, 5) stops at 4.

  3. How many times is print called by this code? ```python for a in range(4): for b in range(3): print(a, b) ```
    Show answer

    12. The inner loop runs 3 times for each of the 4 outer passes: 4 x 3 = 12.

  4. What is displayed when this code runs? ```python for i in range(10, 0, -3): print(i, end=" ") ```
    Show answer

    10 7 4 1 . Counting down in steps of 3 from 10, stopping before 0: 10 7 4 1.

  5. What is displayed when this code runs? ```python count = 0 for ch in "programming": if ch == "m": count = count + 1 print(count) ```
    Show answer

    2. A for loop over a string visits each character; 'm' appears 2 times.

  6. This code should print the numbers 1 to 10 inclusive. Which line completes it? ```python __________ print(n, end=" ") ```
    • `for n in range(1, 11):`
    • `for n in range(1, 10):`
    • `for n in range(10):`
    • `for n in range(0, 11):`
    Show answer

    `for n in range(1, 11):`. The stop value is excluded, so range(1, 11) gives 1 to 10.

  7. This code should display 12 but it does not. Which line number is in the wrong place? ``` 1 nums = [4, 7, 1] 2 for n in nums: 3 total = 0 4 total = total + n 5 print(total) ```
    Show answer

    3. total = 0 is inside the loop (line 3), so it is reset on every pass and only the last value survives. Initialise the total once, before the loop.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.