Why range(5) Doesn't Count to 5 in Python

range(n) gives n numbers starting at 0 — and never reaches n itself.

  • Lesson 2 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

What range() actually counts

  1. The dragon: Here's the single trickiest thing about range(). Watch closely.

  2. The dragon: range(4) does NOT count 1, 2, 3, 4. It counts 0, 1, 2, 3 — starting at zero.

    example.pyPython 3
    for i in range(4):    print(i)
    Output0 1 2 3
  3. The dragon: That's still four numbers — range(4) always gives exactly 4 values. It just starts one earlier than most people expect.

    example.pyPython 3
    for i in range(4):    print(i)
  4. The dragon: And it STOPS before reaching 4. The number inside range() is never itself one of the results.

    example.pyPython 3
    for i in range(4):    print(i)
    Output0 1 2 3
  5. The dragon: Want to count from 1 instead? Don't fight range() — just add 1 to what you print.

    example.pyPython 3
    for i in range(3):    print(i + 1)
    Output1 2 3

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    What does range(5) actually produce?

    • 0, 1, 2, 3, 4Correct
    • 1, 2, 3, 4, 5
    • 0, 1, 2, 3, 4, 5

    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.

  2. Question 2 of 2

    You want the numbers 1 through 5 (five numbers, starting at 1). Which is right?

    • for i in range(5): print(i + 1)Correct
    • for i in range(6): print(i)
    • for i in range(1, 5): print(i)

    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.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    There are 4 treasure chests, numbered starting from 0. Print each chest's number, one per line.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.