Python For Loops Explained (GCSE Examples)
June 19, 2026
A for loop repeats a block of code a set number of times. Loops are one of the most heavily tested ideas in GCSE programming, so it is worth getting comfortable with them. This guide explains them with examples you can run right away.
The basic for loop
This prints the numbers 0 to 4:
for i in range(5):
print(i)
range(5) produces the numbers 0, 1, 2, 3, 4 — it starts at 0 and stops before 5. The variable i takes each value in turn.
Counting from a different number
range(start, stop) lets you choose where to begin:
for n in range(1, 6):
print(n) # prints 1 to 5
Counting in steps
for n in range(0, 21, 5):
print(n) # 0, 5, 10, 15, 20
Looping over a list
You can loop directly over the items in a list:
colours = ["red", "green", "blue"]
for c in colours:
print(c)
A worked example: total of a list
scores = [12, 8, 15, 6]
total = 0
for s in scores:
total = total + s
print(total) # 41
Common mistakes
- Forgetting that
range(n)stops atn-1. - Indenting the loop body incorrectly — Python uses indentation to know what is inside the loop.
- Using a for loop when you do not know how many times to repeat (use a
whileloop instead).
The best way to learn loops is to run them. Open the free online Python IDE, paste in these examples, and change the numbers to see what happens.
Try these loops yourself.
Paste the examples into our free Python IDE and run them.
Open the Python IDE →