🐍 Python for kids · Class 5 · Lesson 8 of 18 · Free
Loops – Repeating Code
⏱ About 25 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Repeat code with a for loop and range().
- Use a while loop that stops when a condition becomes False.
- Add up numbers and stop a loop early with break.
Your teacher says, "Write 'I will not talk in class' 100 times." Your hand would hurt! With a loop, Python can do it in two lines, and it never gets tired.
Why do we need loops in Python?
Imagine printing "Jai Hind!" ten times. You could copy the line ten times, but that is slow and boring.
A loop tells Python: repeat these lines again and again. You write the work once, and Python repeats it.
Like if, a loop line ends with a colon. The lines to repeat are indented under it.
▶for i in range(3):2 print("Jai Hind!")3print("The assembly is over.")
No variables yet.
▌✏️ Change this code and run it yourself
Jai Hind! Jai Hind! Jai Hind! The assembly is over.
💡 Change the 3 to 10 and run it again. That is the power of a loop!
How does range() work in a for loop?
range() makes a row of numbers for the loop. Each time round, the loop variable takes the next number.
range(5) gives 0, 1, 2, 3, 4. It starts at 0 and stops before 5. range(1, 6) starts at 1 and stops before 6.
A third number is the step, or jump size. range(2, 11, 2) gives 2, 4, 6, 8, 10.
Number 1 Number 2 Number 3 Number 4 Number 5 Even: 2 Even: 4 Even: 6 Even: 8 Even: 10
💡 The stop number is never included. To count to 10, write range(1, 11).
How do I make a multiplication table with a for loop?
Here is a loop that does real work. The user types a number, and the loop prints its full times table.
Each time round, i becomes 1, then 2, up to 10. The f-string puts the current values into the sentence.
▶num = int(input("Which table? "))2for i in range(1, 11):3 print(f"{num} x {i} = {num * i}")
No variables yet.
▌✏️ Change this code and run it yourself
Which table? 7 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
What is a while loop in Python?
A while loop keeps repeating as long as its condition is True. It is like an if that keeps checking again.
Use while when you do not know how many repeats you need. For example, "keep going until the countdown reaches zero".
Inside the loop, something must change. Otherwise the condition stays True forever.
▶count = 52while count > 0:3 print(count)4 count = count - 15print("Blast off! The rocket is flying.")
No variables yet.
▌✏️ Change this code and run it yourself
5 4 3 2 1 Blast off! The rocket is flying.
How do I add up numbers with a loop?
Think of a piggy bank. It starts empty, and you drop in a coin each day.
In code, the piggy bank is a variable called total. It starts at 0, and the loop adds to it each time round.
After the loop ends, total holds the full sum.
Savings after 7 days: 70 rupees 1 + 2 + ... + 10 = 55
How do I stop a loop early with break?
Sometimes you want to leave a loop before it finishes. The word break jumps straight out of the loop.
Below, the player gets three tries to guess the secret word. If they get it right, break ends the loop at once.
Its partner, continue, skips the rest of one round and moves to the next.
Guess the secret snack: dosa Wrong, try again. Guess the secret snack: samosa Correct in 2 tries! Game over.
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
for i in range(3):
print(i)Question 2. What does this print?
for n in range(1, 10, 3):
print(n)Question 3. What does this print?
n = 1
while n < 20:
n = n * 2
print(n)🧩 Your challenge
Be the cricket scorer! An over has 6 balls. Ask for the runs scored on each ball, add them up, and print the total runs in the over.
Runs on ball 1: 4 Runs on ball 2: 1 Runs on ball 3: 0 Runs on ball 4: 6 Runs on ball 5: 2 Runs on ball 6: 1 Total runs in the over: 14
⚠️ Common mistakes (and how to fix them)
❌ Wrong
count = 1
while count <= 3:
print(count)No error message – the program prints 1 forever and never stops (an infinite loop).
✅ Right
count = 1
while count <= 3:
print(count)
count = count + 1If nothing inside a while loop changes the variable, the condition stays True forever.
❌ Wrong
for i in range(1, 10):
print(i)No error message – but it prints only 1 to 9, not 1 to 10.
✅ Right
for i in range(1, 11):
print(i)range() stops just before the end number, so use one more than the last number you want.
❌ Wrong
for i in range(3)
print("Hello")SyntaxError: expected ':'
✅ Right
for i in range(3):
print("Hello")Just like if, every for and while line must end with a colon.
📌 Remember
- A loop repeats a block of indented code, so you write the work only once.
- for with range() repeats a fixed number of times; range(a, b) stops before b.
- range(start, stop, step) lets you count in jumps, like 2, 4, 6.
- A while loop repeats as long as its condition is True – make sure something changes.
- Use a total variable that starts at 0 to add things up inside a loop.
- break leaves a loop early; continue skips to the next round.
Finished the quiz and the challenge?
Questions students ask
What is the difference between for and while loop in Python?
A for loop is best when you know how many times to repeat, like 10 times. A while loop is best when you repeat until something happens, like a correct answer.
Why does range(5) start at 0 in Python?
Python counts from 0 by default, and range(5) gives five numbers: 0, 1, 2, 3 and 4. To start at 1, write range(1, 6).
What is an infinite loop and how do I stop it?
An infinite loop never ends because its condition never becomes False. Stop it with the Stop button or by reloading the page, then make sure the loop variable changes inside the loop.
What does break do in a Python loop?
break stops the loop immediately, even if more rounds were left. The program then continues with the first line after the loop.