Lesson 4 – For Loops (Iteration)
In this lesson you will:
- Explain what a for loop does (repetition / iteration).
- Use
range()to control how many times a loop runs. - Write for loops with correct indentation.
- Predict the output of simple loops.
- Use a loop to repeat input (e.g. "ask 10 times") and do simple processing (sum / max).
By the end you should be able to:
- Write a for loop to repeat code a fixed number of times.
- Use
range(start, stop)andrange(start, stop, step)correctly. - Avoid common
range()mistakes (off-by-one errors). - Use a loop to build a result (e.g. total, average, highest).
1. Quick recap
From the previous lessons, remember:
- Variables store values.
- We can display output using
print(). - We can read input using
input()and convert toint()orfloat(). - Indentation matters: Python uses indentation to show which lines belong together.
ifstatements run code only when a condition isTrue.
Mini-check (predict):
What will this print?
x = 3
x = x + 1
print(x)
2. What is a for loop?
A for loop lets Python repeat a block of code a set number of times.
Why this matters: instead of writing lots of similar lines…
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")
…you can write one loop:
for i in range(5):
print("Hello")
How it works:
- Python creates a sequence of values using
range(). - The loop variable (here
i) takes each value in turn. - The indented block runs once for each value.
3. Example 1 – Loop from 1 to 10
Predict first: What will the first number be? What will the last number be?
for i in range(1, 11):
print(i)
What happens:
- When
iis 1 → prints 1 - When
iis 2 → prints 2 - …
- When
iis 10 → prints 10
Then it stops because range(1, 11) stops before 11.
Try it yourself
Run it, then:
- Change it to print 1 to 5
- Change it to print 1 to 20
Output
Example Solution
# Print 1 to 5
for i in range(1, 6):
print(i)
# Print 1 to 20
for i in range(1, 21):
print(i)
4. Understanding range()
The range() function controls how many times your loop runs and what values the loop variable takes. There are three ways to use it:
Example A: range(stop) – Start at 0
When you use just one number, the loop starts at 0 and stops before that number.
for i in range(5):
print(i)
This prints: 0, 1, 2, 3, 4 (5 numbers total, starting from 0)
Try it yourself
Run the code below. Then change it to print numbers going up in steps of 3 (hint: you'll need to add a third number to range).
Output
Example Solution
# To go up in steps of 3, use range(0, 15, 3)
for i in range(0, 15, 3):
print(i) # Prints: 0, 3, 6, 9, 12
Example B: range(start, stop) – Choose your start
When you use two numbers, the first is where to start, and the second is where to stop (but not include).
for i in range(3, 8):
print(i)
This prints: 3, 4, 5, 6, 7 (starts at 3, stops before 8)
Try it yourself
Run the code below. Then change it to print numbers going up in steps of 3.
Output
Example Solution
# To go up in steps of 3, use range(3, 15, 3)
for i in range(3, 15, 3):
print(i) # Prints: 3, 6, 9, 12
Example C: range(start, stop, step) – Control the step size
When you use three numbers, you control where to start, where to stop, and how big each step is.
for i in range(0, 10, 2):
print(i)
This prints: 0, 2, 4, 6, 8 (starts at 0, stops before 10, steps by 2 each time)
Try it yourself
Run the code below. Then change it to go up in steps of 3 instead of 2.
Output
Example Solution
# Change step from 2 to 3
for i in range(0, 10, 3):
print(i) # Prints: 0, 3, 6, 9
Key points to remember:
range(5)→ starts at 0, goes up to (but not including) 5range(3, 8)→ starts at 3, goes up to (but not including) 8range(0, 10, 2)→ starts at 0, goes up to (but not including) 10, steps by 2- The stop value is never included in the range!
5. Example 2 – Loop from 0 to 9
Predict first: Does it include 10?
for i in range(10):
print(i)
What happens:
range(10) starts at 0 and stops before 10. So it prints 0 to 9.
Try it yourself
Change it to print 0 to 4
Change it to print 0 to 19
Output
Example Solution
# Print 0 to 4
for i in range(5):
print(i)
# Print 0 to 19
for i in range(20):
print(i)
6. Example 3 – A different step value
Predict first: How many numbers will print?
for i in range(0, 11, 2):
print(i)
What happens:
- Start 0
- Step 2 each time
- Stop before 11
Prints: 0, 2, 4, 6, 8, 10
Try it yourself
Change the step to 3
Change the start to 1 (keep step 2) and predict what happens
Output
Example Solution
# Step 3
for i in range(0, 11, 3):
print(i) # Prints: 0, 3, 6, 9
# Start 1, step 2
for i in range(1, 11, 2):
print(i) # Prints: 1, 3, 5, 7, 9
7. Example 4 – Printing even numbers
Predict first: Will any odd numbers appear?
for i in range(2, 21, 2):
print(i)
Try it yourself
Make it print even numbers from 2 to 50
Make it print even numbers from 0 to 20
Output
Example Solution
# Even numbers from 2 to 50
for i in range(2, 51, 2):
print(i)
# Even numbers from 0 to 20
for i in range(0, 21, 2):
print(i)
8. Example 5 – Printing odd numbers
Predict first: What is the last number printed?
for i in range(1, 20, 2):
print(i)
Try it yourself
Make it print odd numbers from 1 to 99
Make it print odd numbers from 5 to 25
Output
Example Solution
# Odd numbers from 1 to 99
for i in range(1, 100, 2):
print(i)
# Odd numbers from 5 to 25
for i in range(5, 26, 2):
print(i)
9. Repeating input with a loop (this is where loops become powerful)
A very common pattern is: "Ask the user something 10 times."
Example – ask for 3 numbers and print them
Predict first: How many times will it ask?
for i in range(3):
num = int(input("Enter a number: "))
print("You entered:", num)
Try it yourself
Change it to ask 5 times
Change the message to include the attempt number (extension): Attempt 1, Attempt 2, …
Output
Example Solution
# Ask 5 times
for i in range(5):
num = int(input("Enter a number: "))
print("You entered:", num)
# With attempt number
for i in range(3):
num = int(input("Attempt " + str(i+1) + ": Enter a number: "))
print("You entered:", num)
Video – For loops with calculations
Watch this video to see how to use for loops for common patterns: calculating sums and averages.
Video: Using for loops to calculate sums and averages
10. Indentation and loop blocks (important)
A for loop line ends with a colon :. The code inside the loop must be indented.
Example – one line inside the loop:
for i in range(5):
print(i)
Example – two lines inside the loop:
for i in range(5):
print("Number:", i)
print("-----")
print("Done")
What happens:
- The indented lines repeat.
print("Done")runs once at the end.
Try it yourself
Move one of the indented lines back to the left. Predict what changes, then run it.
Output
Example Solution
# If you move print("-----") to the left (no indentation):
for i in range(5):
print("Number:", i)
print("-----") # This runs once, not 5 times!
print("Done")
11. Printing star patterns (triangle)
You can use loops to create patterns with stars or other characters. A common pattern is a triangle that grows from 1 star to many stars.
Example – Print a triangle of stars
Predict first: How many lines will print? How many stars on the first line? How many on the last line?
for i in range(1, 6):
print("*" * i)
What happens:
- When
iis 1 → prints "*" (1 star) - When
iis 2 → prints "**" (2 stars) - When
iis 3 → prints "***" (3 stars) - When
iis 4 → prints "****" (4 stars) - When
iis 5 → prints "*****" (5 stars)
Key idea: The expression "*" * i repeats the star character i times. This is called string multiplication.
Try it yourself
Change it to print a triangle with 10 lines
Change it to print a triangle starting from 3 stars (so the first line has 3 stars, second has 4, etc.)
Output
Example Solution
# Triangle with 10 lines
for i in range(1, 11):
print("*" * i)
# Triangle starting from 3 stars
for i in range(3, 8):
print("*" * i) # First line: 3 stars, second: 4, etc.
12. Common misconceptions
Read these carefully — they are very common in tests.
Mistake 1: Expecting the stop value to be included
for i in range(1, 10):
print(i)
This prints 1 to 9, not 10.
Fix:
for i in range(1, 11):
print(i)
Mistake 2: Forgetting indentation
Wrong:
for i in range(5):
print(i)
Correct:
for i in range(5):
print(i)
Mistake 3: Thinking range(10) starts at 1
It starts at 0.
range(10) → 0 to 9
Mistake 4: Counting backwards but forgetting the step is negative
To go down, you need a negative step:
for i in range(10, 0, -1):
print(i)
(Notice it stops before 0, so 1 is the last value.)
14. Understanding Quiz
Answer all questions below, then click "Check Answers" at the end to see your score.
for i in range(4):
print(i)
for i in range(5):
print(i)
for i in range(10, 0, -1):
print(i)
for i in range(1, 10):
print(i)
They wanted 1 to 10. What should they change?
for i in range(1, 6):
print(i * 2)
total = 0
for i in range(5):
total = total + i
print(total)
13. Coding tasks – For loops practice
Complete all the tasks below. Do one task at a time, run your code, and check the output.
-
Task 1 – Count up
Print the numbers 1 to 10. -
Task 2 – Count down
Print the numbers 10 down to 1. -
Task 3 – Evens
Print the even numbers from 2 to 20. -
Task 4 – Step challenge
Print 5, 10, 15, 20, 25, 30 usingrange()with a step. -
Task 5 – Squares
Print the square of each number from 1 to 8 (e.g., 1² = 1, 2² = 4, …). -
Task 6 – Times table (formatting)
Print the 7 times table from 7 × 1 to 7 × 12 like:7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 ... -
Task 7 – Running total
A shop counts customers each hour for 5 hours. Ask for 5 numbers (customers each hour) and print the total customers. -
Task 8 – Highest score
A class has 10 quiz scores. Ask for 10 scores and print the highest score. -
Task 9 – Pass counter
A teacher enters 10 test scores (0–100). Count how many are 50 or more and print:Passes: ___ -
Task 10 – Summary stats
A sports coach enters 8 lap times (seconds). Ask for 8 times and then print the average (total ÷ 8).
Answer all the above tasks here
Use the editor below to complete the coding tasks above.