CurriculumPython for Loops and range() for KidsWhat range() actually counts
Why range(5) Doesn't Count to 5 in Python
range(n) gives n numbers starting at 0 — and never reaches n itself.
What range() actually counts
Here's the single trickiest thing about range(). Watch closely.
range(4) does NOT count 1, 2, 3, 4. It counts 0, 1, 2, 3 — starting at zero.
for i in range(4):
print(i)Output
0
1
2
3That's still four numbers — range(4) always gives exactly 4 values. It just starts one earlier than most people expect.
for i in range(4):
print(i)And it STOPS before reaching 4. The number inside range() is never itself one of the results.
for i in range(4):
print(i)Output
0
1
2
3Want to count from 1 instead? Don't fight range() — just add 1 to what you print.
for i in range(3):
print(i + 1)Output
1
2
3Check your understanding
What does range(5) actually produce?
Why: range(5) gives exactly five numbers, starting at 0: 0, 1, 2, 3, 4. The number you put in is how MANY values you get, not the last value you see — that's the part that trips almost everyone up at first.
You want the numbers 1 through 5 (five numbers, starting at 1). Which is right?
Why: range(5) reliably gives 5 values (0-4), and adding 1 to each shifts them to 1-5 without having to recompute the range size. Both of the other options are off by exactly one, in different directions.
What you'll practice
There are 4 treasure chests, numbered starting from 0. Print each chest's number, one per line.
More from this topic
What range() actually counts is one lesson inside Python for Loops and range() for Kids — see the full lesson order and what the whole topic covers.
View the full topic →