Nex2School

Parent guide · 7 min read

Python for loops for kids: making the computer repeat, and why range(5) stops at 4

Sooner or later a child learning Python meets the for loop: a way to tell the computer to repeat something without typing it ten times. The idea is easy to say and surprisingly easy to get slightly wrong. Children print 1 to 5 and get 0 to 4, put a line at the wrong indent and see it run once instead of five times, or cannot say what the loop variable is. This guide explains what a for loop does, walks one through line by line, and lists the slips to look for, so a parent who has never written code can still help.

Try this tonight

4 things you can do at home

  1. Have them say what repeats, out loud

    Before any code, ask "what do you want done again and again?" A times-table printer, a countdown, adding up a list of prices. If the child can name the repeated action and how many times, the loop is mostly written already.

  2. Trace it on paper with a table

    Draw two columns, "i" and "what is printed". For range(3) with print(i * 2), write a row for each repeat: i is 0 and 0 is printed, i is 1 and 2 is printed, i is 2 and 4 is printed. Running the loop by hand on paper is the single most useful habit for any beginner coder.

  3. Change one number and predict first

    Ask your child to guess what happens if range(5) becomes range(1, 6), or if the step is 2. Then run it. A prediction made before pressing run turns the computer into a quick marker rather than the answer.

  4. Check indentation when it runs the wrong number of times

    Python decides which lines belong to the loop by the spaces at the start of the line. A line pushed in under the for line repeats, and a line at the left edge runs once, after the loop ends. If something prints once when it should print five times, look at the indent first.

  1. What a for loop is doing

    Here is the smallest useful loop: for i in range(5): followed, on the next line and indented, by print(i). It prints 0, 1, 2, 3, 4, each on its own line. The word for starts the loop, i is the loop variable, range(5) is the sequence of numbers to walk through, and the colon at the end of the line says that the indented lines below belong to the loop.

    The loop variable is just an ordinary variable that Python updates for you. On the first repeat i is 0, on the second it is 1, and so on until the sequence runs out. Children who see i as "the number we are on" rather than a mysterious letter usually find loops much less strange.

  2. Why range(5) stops at 4

    range(5) produces five numbers, but it starts at 0, so the last one is 4. This is the most common surprise, and it is not a mistake: counting from 0 is how Python (and most programming languages) number things, and range(5) means "five repeats", not "up to and including 5".

    To count from 1 to 5, give range two numbers: range(1, 6). The first is where to start and the second is where to stop, and the stop number is never included. A good rule to teach is "range stops just before its last number", and the 1-to-5 example is worth typing in and checking once.

  3. A worked example: a times-table printer

    for n in range(1, 11): print(7 * n) prints the seven times table, 7, 14, 21, up to 70. Trace the first three repeats. When n is 1, 7 × 1 = 7 is printed. When n is 2, 14. When n is 3, 21. Ten repeats, because range(1, 11) gives the numbers 1 to 10, and the last line printed is 7 × 10 = 70.

    The same loop would take an hour to type by hand for a hundred numbers and takes one changed number to extend. That is the practical point of a loop, and a times table is a good first project because the child can check every line against what they already know.

  4. A worked example: adding up a list

    Suppose a list holds three prices, 3, 5 and 2. Start with total = 0, then for price in [3, 5, 2]: total = total + price. Trace it. After the first repeat total is 3, after the second it is 8, and after the third it is 10. A loop can walk through a list directly, with no range at all, and the variable collects an answer as it goes.

    The usual slip here is to put total = 0 inside the loop, which resets it to 0 on every repeat and leaves the last price as the answer. Putting the starting value before the for line, and printing the total after the loop at the left edge, are the two details to check.

Signs it’s time for outside help

When home help has done what it can

  • Cannot say what the loop variable holds on the second repeat.
  • Prints 0 to 4 and cannot see why it is not 1 to 5.
  • Copies and pastes the same line several times instead of looping.
  • Puts every line at the left edge, or every line indented, and does not see the difference.
  • Resets a running total inside the loop and gets only the last value.
  • Can type a loop from an example but freezes when asked to change it.

Questions parents ask

FAQ

What is the difference between a for loop and a while loop?

A for loop repeats once for each item in a sequence, so you know how many repeats there will be. A while loop repeats for as long as a condition stays true, so it suits "keep going until something happens". Beginners usually meet the for loop first because it is harder to leave running forever by accident.

Why does Python count from 0?

It is a convention shared by most programming languages. Position 0 is the first item, which makes some calculations simpler for programmers. For a child it is something to learn once: range(5) gives five numbers starting at 0.

What does the i in "for i in range(5)" mean?

It is a variable name that Python fills in on each repeat. It could be called anything, such as n, count or day. Programmers often use i for "item" or "index", but a descriptive name like price or day is easier for a beginner to read.

At what age can a child learn for loops?

Many children meet loops after learning variables and printing, often around ages 8 to 10 in a beginner Python course, and some earlier. Readiness depends more on being comfortable counting, adding and following steps in order than on a particular age.

What is a nested loop?

A loop inside another loop, such as printing a whole multiplication grid with one loop for the rows and another for the columns. It is worth leaving until a single loop is easy.

Not sure which is right for your child?

Tell us their age and what they have tried so far, and we will suggest where to start before you book anything.

Keep reading