๐ฐ๏ธ Python, Grade 8 ยท session 3 of 16 ยท 60 minutes ยท ๐ free with an account
Loops: for, range and while
Computers never get bored. NOVA repeats things with loops: a for loop for a known number of times, a while loop until something changes. You will add up, count, draw shapes and play a guessing game.

A complete lesson ยท 60 minutes
Teach it, see it, practise it, all on this page.
Open this page with your learner and work down it, or press Teach this lesson to go through it one step at a time on a big screen. Every section has something to do.
- Warm-up3 min
- Warm-up games5 min
- for and range9 min
- Totals and counters9 min
- while loops9 min
- Times table maker7 min
- Hollow square7 min
- Prime checker5 min
- Quiz5 min
- Set the after-class project1 min
By the end you can
- Repeat with for and range(start, stop, step)
- Keep a running total or a count inside a loop
- Loop with while until a condition becomes False
- Use if inside a loop
Before you start
- Variables and maths (lesson 1)
- if, elif and else (lesson 2)
Have ready
- A computer or tablet with this page open
Grade 8 ยท ages 13 and 14 ยท no experience needed
Warm-up ยท think first
Add up all the numbers from 1 to 100 in your head. Is there a fast way?
Show the answer
5050. Pair 1 with 100, 2 with 99 and so on: 50 pairs of 101.
Teacher tip: Then show that a loop does it without the trick: total = total + n, a hundred times.
๐ฎ Warm-up games ยท 5 quick games
Play with Bolt the robot
Five minutes with NOVA, the station computer. Vote, say why, then show the answer.
- Which list doesn't belong: range(0, 6, 2), range(0, 5, 2), range(1, 6, 2), range(0, 4, 2)? (range(0, 6, 2) ยท range(0, 5, 2) ยท range(1, 6, 2) ยท range(0, 4, 2))
Answer: range(1, 6, 2) is the odd one: it gives the odd numbers 1, 3, 5, and the others give even numbers. - Would you rather get 1 rupee doubled every day for 20 days, or 10,000 rupees now? (1 rupee doubled for 20 days ยท 10,000 rupees now)
Answer: Doubling: on day 20 you get 2 ** 19 = 524,288 rupees in that day alone. - range(n) gives n numbers. (Always ยท Sometimes ยท Never)
Answer: Always โ Always. range(5) is 0, 1, 2, 3, 4: five numbers, starting from 0. - How many stars are in a triangle with 10 rows: 1 star, then 2, then 3, โฆ?
Answer: 55: 1 + 2 + โฆ + 10 = 55. - A snail climbs 3 m up a 10 m wall by day and slips 2 m by night. On which day does it reach the top?
Answer: Day 8: after 7 days and nights it is at 7 m, and on day 8 it climbs to 10 m.
Three levels, like a real engineer
Use it. Tweak it. Build it.
First use what the lesson shows exactly as it is, then change one key thing and see what happens, then make your own.
- ๐
Level 1 ยท Use it
Watch the Loop Run
Run the example that prints the numbers 1 through 5 using the for loop. Carefully watch how the code repeats the print statement for every single number in the sequence.
- ๐ง
Level 2 ยท Tweak it
Adjust the Loop Limit
Take the example that counts to 5 and change the stopping number. Change the range to stop at 15. Predict how many times the loop will execute before running the code.
- ๐ ๏ธ
Level 3 ยท Build it
Build Your Own Counter
Write a while loop that keeps a running total. Start the total at zero, and make the loop count up to 20, adding the count to your total each time.
Part 1 of 3 ยท learn, see, practise
for and range
Need to do something 10 times? Don't copy the line 10 times. A for loop repeats the indented lines once for every number range gives me.
for i in range(5): runs the indented lines 5 times, with i = 0, 1, 2, 3, 4. range stops before the stop number.
range(start, stop, step): range(2, 11, 2) is 2, 4, 6, 8, 10, and range(10, 0, -1) counts down from 10 to 1.
๐งโ๐ซ Teacher: type this live with the class
Counting machine
for i in range(5):Read it: for each i in 0 to 4.
print(i)Indented: this line repeats. Run it: 0 to 4, not 5.
for n in range(2, 11, 2):Start 2, stop before 11, jump 2.
print(n, end=" ")end=" " keeps them on one line.
โถ Output
(nothing is printed)
๐ฆ Memory boxes
โถ Output
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
๐ฆ Memory boxes
5intChange 6 to 13 for the whole 7 times table, then change the 7.
Practise it
Start with the warm-up and go as far as you can. The stretch questions are meant to be hard.
1.1 Print the numbers 1 to 10, one per line.
โถ Output
(nothing is printed)
๐ฆ Memory boxes
๐งโ๐ซ Model answer (for the teacher)
Teacher eyes only: hide the answers before you share your screen.
The answer green = the 2 lines the student writes
for n in range(1, 11): print(n)
What it prints
1 2 3 4 5 6 7 8 9 10
What โ Check looks for
- it prints exactly:
1 2 3 4 5 6 7 8 9 10
- In the code: Use a for loop.
1.2 Print 3, 6, 9, โฆ 30, one per line, using range with a step.
โถ Output
(nothing is printed)
๐ฆ Memory boxes
๐งโ๐ซ Model answer (for the teacher)
Teacher eyes only: hide the answers before you share your screen.
The answer green = the 2 lines the student writes
for n in range(3, 31, 3): print(n)
What it prints
3 6 9 12 15 18 21 24 27 30
What โ Check looks for
- it prints exactly:
3 6 9 12 15 18 21 24 27 30
- In the code: Use range(start, stop, step).
1.3 Print a countdown from 10 to 1, then Liftoff!
โถ Output
(nothing is printed)
๐ฆ Memory boxes
๐งโ๐ซ Model answer (for the teacher)
Teacher eyes only: hide the answers before you share your screen.
The answer green = the 3 lines the student writes
for n in range(10, 0, -1): print(n)print("Liftoff!")
What it prints
10 9 8 7 6 5 4 3 2 1 Liftoff!
What โ Check looks for
- it prints exactly:
10 9 8 7 6 5 4 3 2 1 Liftoff!
- In the code: Count down with a step of -1.
1.4 Print the 9 times table from 9 x 1 = 9 to 9 x 10 = 90.
โถ Output
(nothing is printed)
๐ฆ Memory boxes
๐งโ๐ซ Model answer (for the teacher)
Teacher eyes only: hide the answers before you share your screen.
The answer green = the 2 lines the student writes
for n in range(1, 11): print(f"9 x {n} = {9 * n}")
What it prints
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 9 x 10 = 90
What โ Check looks for
- it prints exactly:
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 9 x 10 = 90
- In the code: Use a loop.
1.5 Print a triangle of stars: 1 star, then 2, up to 5.
โถ Output
(nothing is printed)
๐ฆ Memory boxes
๐งโ๐ซ Model answer (for the teacher)
Teacher eyes only: hide the answers before you share your screen.
The answer green = the 2 lines the student writes
for n in range(1, 6): print("*" * n)
What it prints
* ** *** **** *****
What โ Check looks for
- it prints exactly:
* ** *** **** *****
- In the code: Use a loop.
1.6 Mix with lesson 2: print the numbers from 1 to 20 that divide by 3 or by 4, one per line.
โถ Output
(nothing is printed)
๐ฆ Memory boxes
๐งโ๐ซ Model answer (for the teacher)
Teacher eyes only: hide the answers before you share your screen.
The answer green = the 3 lines the student writes
for n in range(1, 21): if n % 3 == 0 or n % 4 == 0: print(n)
What it prints
3 4 6 8 9 12 15 16 18 20
What โ Check looks for
- it prints exactly:
3 4 6 8 9 12 15 16 18 20
- In the code: Test with %.
- In the code: Join the tests with or.
๐ Free for a year
Make a free account to open the rest of this lesson
Every new account opens every lesson of every course free for a whole year: no payment, no card, nothing to cancel.
Unlocking the rest of this lesson gives you:
- Part 2: Totals and counters, with practice at three levels
- Part 3: while loops, with practice at three levels
- Class activity 1: Times table maker
- Class activity 2: Hollow square
- Class activity 3: Prime checker
- A 8-question quiz
- After-class project: Times-table trainer
- Homework and a printable practice sheet with answers
- Teach mode and the minute-by-minute teacher's plan
It takes a minute. Anyone under 13 signs up with a parent. The first 2 lessons of every course, every game and all maths practice stay free.
FAQ
Loops: for, range and while: questions
What is the difference between for and while?
Use for when you know how many times to repeat (or have a list to walk through). Use while when you repeat until something changes.
Start a conversation
Want a teacher to take this class?
Tell us the learner's age and level. A teacher from our network teaches with these lessons; we confirm timing and price first.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian