Imagine telling a robot to stamp 30 envelopes. You would not write 30 separate instructions — you would say "repeat this 30 times." A for loop in Python works that way: it repeats a block of code once for each item in a sequence, then stops automatically. It is one of the most important tools any programmer learns.
What is the basic structure of a for loop?
A Python for loop has three parts: the keyword for, a loop variable, the keyword in, a sequence, and a colon. The indented block below runs once per item.
for item in sequence:
# this indented block runs once for each item
do_something_with(item)
Python uses indentation (four spaces or one tab) to show which lines are inside the loop. There are no curly braces as in some other languages — the indentation is the structure.
How do you loop a fixed number of times using range()?
range() generates a sequence of integers. It is the most common tool when you want to repeat something a set number of times.
for i in range(5):
print(i)
Output:
0
1
2
3
4
Notice that range(5) produces the numbers 0, 1, 2, 3, 4 — five numbers starting at zero. The upper bound (5) is not included.
You can control the start, stop, and step:
| Code | Sequence produced | Use case |
|---|---|---|
range(5) |
0, 1, 2, 3, 4 | Count from 0 |
range(1, 6) |
1, 2, 3, 4, 5 | Count from 1 |
range(0, 10, 2) |
0, 2, 4, 6, 8 | Even numbers |
range(10, 0, -1) |
10, 9, 8 … 1 | Count down |
Worked example — sum of numbers 1 to 10:
total = 0
for number in range(1, 11):
total = total + number
print("Total:", total) # Total: 55
How do you loop over a list?
When you have a list of items and want to process each one, loop directly over the list:
subjects = ["Maths", "English", "Computing", "Science"]
for subject in subjects:
print("I enjoy", subject)
Output:
I enjoy Maths
I enjoy English
I enjoy Computing
I enjoy Science
The loop variable subject takes the value of each list item in turn. You can name it anything, but a meaningful name makes your code far easier to read.
Worked example — find the highest score:
scores = [72, 88, 61, 95, 83]
highest = 0
for score in scores:
if score > highest:
highest = score
print("Highest score:", highest) # Highest score: 95
How do you loop over a string?
Strings are sequences of characters, so for loops work on them directly:
word = "Python"
for letter in word:
print(letter)
Output:
P
y
t
h
o
n
This is useful for tasks like counting vowels, reversing a word character by character, or checking whether a string contains a particular letter.
Worked example — count vowels in a word:
word = input("Enter a word: ")
vowels = "aeiouAEIOU"
count = 0
for letter in word:
if letter in vowels:
count = count + 1
print("Number of vowels:", count)
How do you use the loop variable's index?
Sometimes you need both the item and its position. Use enumerate():
names = ["Alice", "Bob", "Carol"]
for index, name in enumerate(names):
print(index, name)
Output:
0 Alice
1 Bob
2 Carol
enumerate() pairs each item with its position number, starting from 0 (you can pass a second argument to start from 1 instead: enumerate(names, 1)).
What are the most common for-loop mistakes at KS3?
| Mistake | Example | Fix |
|---|---|---|
| Forgetting the colon | for i in range(5) |
Add : at the end |
| Wrong indentation | Code after loop runs every iteration | Ensure loop body is indented consistently |
range upper bound is exclusive |
Expect 1–10, write range(1,10) |
Use range(1, 11) |
| Modifying the list mid-loop | Can produce unexpected behaviour | Loop over a copy: for x in my_list[:] |
Frequently asked questions
What is the difference between a for loop and a while loop in Python?
A for loop repeats a fixed number of times (once per item in a sequence). A while loop repeats as long as a condition remains True, which could be forever if the condition never becomes False. Use for when you know in advance how many repetitions are needed; use while when the stopping point depends on user input or a changing value.
Can I stop a for loop early?
Yes. Use the break statement to exit the loop immediately:
for number in range(10):
if number == 5:
break
print(number)
# Prints 0, 1, 2, 3, 4 then stops
What does continue do inside a for loop?
continue skips the rest of the current iteration and moves to the next item:
for number in range(6):
if number == 3:
continue
print(number)
# Prints 0, 1, 2, 4, 5 (skips 3)
Do I need for loops for the GCSE computer science exam?
Yes. Iteration (including for loops) is explicitly required by all major GCSE specifications. You will need to write, trace, and modify for loop code in both the written exam and any programming project or controlled assessment.
Ready to practise writing for loops with instant feedback on your code? Visit aitutors.me — Professor Turing sets bite-sized Python challenges pitched exactly at your level.