Picture a character running a lap around a track - one full lap for every single item you're counting through. A for loop in Python does exactly that: for i in range(5) runs its indented block once for each number from 0 up to (but not including) 5 - five laps, five numbers: 0, 1, 2, 3, 4. Miscounting the starting or ending point is exactly why a loop might run one too many, or one too few, times.
A for loop repeats a block of code once for each item in a sequence - like counting through a fixed set of numbers, or going through every item in a list, one at a time.
for variable in range(n):
# runs n times, variable = 0, 1, ..., n-1
for variable in range(start, stop):
# runs from start up to (not including) stop
for item in some_list:
# runs once per item in the listfor i in range(3):
print("Lap", i)range(3) produces the numbers 0, 1, 2 - three values in total, so the loop body runs exactly three times. Each time through, i takes the next value in that sequence, which is why the printed laps are 0, 1, 2, not 1, 2, 3.
for stars in range(2, 10, 2):
print(stars)range() accepts a third number: the step. range(2, 10, 2) starts at 2, stops before 10, and jumps by 2 each time, producing 2, 4, 6, 8 - like collecting stars two at a time instead of counting every single one.
In the app this lesson continues with the animated explanation, spoken aloud, and then the practice: Make your character do a repeating action exactly 5 times using a for loop over range(). The editor runs your child’s real code and checks the result, with their coding buddy reacting to what they wrote.
Try it free