๐Ÿ›ฐ๏ธ 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.

๐ŸŽ Unlock free: make an account
Illustration of a cute pastel robot dancing
Illustration: andremsdesign / Pixabay

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.

  1. Warm-up3 min
  2. Warm-up games5 min
  3. for and range9 min
  4. Totals and counters9 min
  5. while loops9 min
  6. Times table maker7 min
  7. Hollow square7 min
  8. Prime checker5 min
  9. Quiz5 min
  10. 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.

More warm-ups for every class โ†’

๐ŸŽฎ 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.

  1. 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.
  2. 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.
  3. range(n) gives n numbers. (Always ยท Sometimes ยท Never)
    Answer: Always โ€” Always. range(5) is 0, 1, 2, 3, 4: five numbers, starting from 0.
  4. How many stars are in a triangle with 10 rows: 1 star, then 2, then 3, โ€ฆ?
    Answer: 55: 1 + 2 + โ€ฆ + 10 = 55.
  5. 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.

  1. ๐Ÿ”’

    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.

  2. ๐Ÿ”ง

    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.

  3. ๐Ÿ› ๏ธ

    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

  1. for i in range(5):

    Read it: for each i in 0 to 4.

  2. print(i)

    Indented: this line repeats. Run it: 0 to 4, not 5.

  3. for n in range(2, 11, 2):

    Start 2, stop before 11, jump 2.

  4. print(n, end=" ")

    end=" " keeps them on one line.

Type here

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
Example: run it, then change it

โ–ถ Output

1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35

๐Ÿ“ฆ Memory boxes

n5int

Change 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.

Warm-up

1.1 Print the numbers 1 to 10, one per line.

Task 1.1

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ 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.
Warm-up

1.2 Print 3, 6, 9, โ€ฆ 30, one per line, using range with a step.

Task 1.2

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ 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).
Core

1.3 Print a countdown from 10 to 1, then Liftoff!

Task 1.3

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ 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.
Core

1.4 Print the 9 times table from 9 x 1 = 9 to 9 x 10 = 90.

Task 1.4

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ 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.
Stretch ๐Ÿ”ฅ

1.5 Print a triangle of stars: 1 star, then 2, up to 5.

Task 1.5

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ 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.
Stretch ๐Ÿ”ฅ

1.6 Mix with lesson 2: print the numbers from 1 to 20 that divide by 3 or by 4, one per line.

Task 1.6

โ–ถ Output

(nothing is printed)

๐Ÿ“ฆ Memory boxes

No variables yet
๐Ÿง‘โ€๐Ÿซ 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

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Chat on WhatsApp
Esc

Try