Understanding Python's range() Function
How It Works In this program, we are printing a sequence of numbers. We generate this sequence of numbers using the built-in range function.
What range() Does
The range() function is a built-in Python tool that generates a sequence of numbers. You use it most often inside a for loop to repeat a block of code a specific number of times. Instead of manually typing out every number you want to loop through, range() creates that sequence for you automatically. Think of it as a way to say 'give me the numbers from here to there' without having to write them all out by hand.
range() is a reusable piece of Python that works with any sequence. The for..in loop works for any sequence, so you can use range() to generate numbers, but you could also loop through lists, strings, or other sequences.
The Three Forms of range()
range() can be called in three different ways, each with a different number of arguments. Understanding which form to use depends on what sequence of numbers you need.
range(stop) — generates numbers starting from 0 up to (but not including) the stop value. For example, range(5) produces 0, 1, 2, 3, 4.
range(start, stop) — generates numbers starting from start up to (but not including) stop. For example, range(2, 7) produces 2, 3, 4, 5, 6.
range(start, stop, step) — generates numbers starting from start, moving by step each time, up to (but not including) stop. For example, range(0, 10, 2) produces 0, 2, 4, 6, 8.
How Parameters Control the Sequence
Each parameter in range() has a specific job. Understanding what each one does helps you predict exactly which numbers will be generated.
start — the first number in the sequence. If you do not provide a start value, it defaults to 0.
stop — the boundary where the sequence ends. The sequence stops BEFORE reaching this value; it never includes the stop value itself.
step — how much to add to each number to get the next one. If you do not provide a step value, it defaults to 1. You can use negative step values to count backwards.
The Exclusive Upper Bound
One of the most important things to remember about range() is that it stops BEFORE the stop value. This is called an exclusive upper bound. The stop value is a boundary that the sequence never reaches or includes.
This design choice makes range() work well with zero-based indexing in Python. If you want numbers from 0 to 4, you write range(5). If you want numbers from 0 to 9, you write range(10). The stop value is always one more than the last number you want.
Using range() in a For Loop
The most common use of range() is inside a for loop. This lets you repeat a block of code a specific number of times, with a counter variable that changes each iteration.
Printing Numbers 1 Through 5
Write a program that prints the numbers 1 through 5, each on its own line.
- 1Identify the sequence needed
You want to print 1, 2, 3, 4, 5. This is five consecutive numbers starting at 1.
- 2Choose the right range() call
To get numbers from 1 to 5, use
range(1, 6). The start is 1, and the stop is 6 (one more than the last number you want). - 3Write the for loop
Use for i in
range(1, 6): to loop through each number. The variable i will take on each value from the range, one at a time. - 4Print inside the loop
Inside the loop body, use
print(i)to output each number.
The program prints:
12345
1
2
3
4
5Counting Backwards with Negative Step
You can use a negative step value to make range() count backwards instead of forwards. When you do this, the start value should be larger than the stop value, and the sequence will decrease by the step amount each time.
Counting Down from 5 to 1
Write a program that counts down from 5 to 1.
- 1Identify the direction
You want to go backwards, so you need a negative step value.
- 2Set start and stop correctly
Start at 5 (the highest number) and stop before 0 (so use 0 as the stop value). This ensures you get 5, 4, 3, 2, 1.
- 3Use a negative step
Use step = -1 to decrease by 1 each time. The call is
range(5, 0, -1).
The program counts: 5, 4, 3, 2, 1
5
4
3
2
1Common Mistakes with range()
Common mistake Forgetting that range() stops before the stop value
What it looks likerange(5) produces 0, 1, 2, 3, 4 — not 0, 1, 2, 3, 4, 5Why it failsThe stop value is an exclusive boundary. The sequence never includes it.
Do this insteadIf you want numbers up to and including 5, use range(6) instead of range(5).
Common mistake Using the wrong order of arguments
What it looks likeWriting range(10, 1) when you meant range(1, 10)Why it failsWith a positive step (the default),
range(10, 1)produces an empty sequence because you cannot count up from 10 to 1.Do this insteadRemember the order: range(start, stop, step). If start is larger than stop, use a negative step.
Common mistake Confusing the number of arguments with their meaning
What it looks likeWriting range(5, 10) thinking it means 'give me 5 numbers up to 10'Why it failsrange(5, 10)means start at 5 and stop before 10, not 'start at 5 and give me 10 numbers'.Do this insteadrange(5, 10) produces 5, 6, 7, 8, 9 — five numbers, but because of the start and stop values, not because you asked for five.
Common mistake Forgetting to use range() inside a for loop
What it looks likeWriting print(range(5)) instead of looping through itWhy it failsprint(range(5))prints the range object itself, not the individual numbers.Do this insteadUse a for loop: for i in range(5): print(i) to print each number.
Common mistake Using a step of 0
What it looks likerange(0, 10, 0)Why it failsA step of 0 would create an infinite loop because the value never changes, so Python raises an error instead.
Do this insteadAlways use a non-zero step value, typically 1 or -1.
Practice: Predicting range() Output
What do you think happens?
What numbers does range(3, 12, 3) produce?
Reveal answer
3, 6, 9
Starting at 3, stepping by 3 each time, and stopping before 12 gives you 3, then 6 (3+3), then 9 (6+3). The next value would be 12, but 12 is the stop boundary, so it is not included.
What do you think happens?
What numbers does range(10, 0, -2) produce?
Reveal answer
10, 8, 6, 4, 2
Starting at 10 and stepping backwards by 2 each time, stopping before 0, gives you 10, 8, 6, 4, 2. The next value would be 0, but 0 is the stop boundary, so it is not included.
Key Takeaways
- 1
range()generates a sequence of numbers. You can call it with one argumentrange(stop), two argumentsrange(start, stop), or three argumentsrange(start, stop, step). - 2The stop value is always excluded from the sequence.
range(5)gives 0, 1, 2, 3, 4 — never 5. - 3If you do not provide a start value, it defaults to 0. If you do not provide a step value, it defaults to 1.
- 4Use
range()inside a for loop to repeat code a specific number of times, with a counter variable that changes each iteration. - 5You can use a negative step to count backwards. For example,
range(5, 0, -1)counts down from 5 to 1.
At a glance
range()is a built-in function that generates sequences of numbers, most commonly used in for loops to repeat code a specific number of times.range()has three forms:range(stop),range(start, stop), andrange(start, stop, step), each controlling which numbers are generated.- The stop value is always excluded —
range()stops before reaching it, which aligns with Python's zero-based indexing. - You can count backwards by using a negative step value, such as
range(10, 0, -1)to count down from 10 to 1. - Common mistakes include forgetting the exclusive upper bound, confusing argument order, and trying to print
range()directly instead of looping through it.