Python range() Function Explained
The Python range() function creates a sequence of numbers.
Beginners usually use it in for loops to repeat something a certain number of times or to work with number patterns. A very important detail is that range() stops before the end value.
Quick example #
for i in range(5):
print(i)
# Output:
# 0
# 1
# 2
# 3
# 4
range(5) creates numbers starting at 0 and stopping before 5.
What range() does #
range() is used to generate numbers in order.
Key points:
range()creates a sequence of numbers- It is commonly used with
forloops - The stop value is not included
- It returns a
rangeobject, not a regular list
You will often see it with a loop like this:
for number in range(3):
print(number)
Output:
0
1
2
If you are still learning loops, see Python for loops explained.
range() syntax #
range() has three common forms:
range(stop)
range(start, stop)
range(start, stop, step)
Meaning of each argument:
start= first numberstop= end limit, not includedstep= amount to move each time
range(stop) #
When you give one value, Python treats it as the stop value and starts from 0.
print(list(range(5)))
Output:
[0, 1, 2, 3, 4]
range(start, stop) #
This starts at start and stops before stop.
print(list(range(2, 6)))
Output:
[2, 3, 4, 5]
range(start, stop, step) #
This also lets you control how much the value changes each time.
print(list(range(1, 10, 2)))
Output:
[1, 3, 5, 7, 9]
How the arguments work #
Here are the most common patterns beginners use.
range(5) #
print(list(range(5)))
Output:
[0, 1, 2, 3, 4]
This starts at 0 and stops before 5.
range(2, 6) #
print(list(range(2, 6)))
Output:
[2, 3, 4, 5]
This starts at 2 and stops before 6.
range(1, 10, 2) #
print(list(range(1, 10, 2)))
Output:
[1, 3, 5, 7, 9]
This moves by 2 each time, so it gives odd numbers from 1 to 9.
Using a negative step #
A negative step makes range() count backward.
print(list(range(5, 0, -1)))
Output:
[5, 4, 3, 2, 1]
This works because the numbers move downward.
If the direction does not match the step, you may get an empty result:
print(list(range(1, 5, -1)))
Output:
[]
step cannot be 0 #
This causes an error:
range(1, 5, 0)
Python raises a ValueError because it cannot move by zero.
Using range() in a for loop #
range() is most useful when:
- You want to repeat something a set number of times
- You need the current number in each loop
- You are using the common pattern
for i in range(n)
Example:
for i in range(3):
print("Loop number:", i)
Output:
Loop number: 0
Loop number: 1
Loop number: 2
Repeating something a fixed number of times #
for i in range(5):
print("Hello")
This prints "Hello" five times.
Using the current number #
for i in range(1, 4):
print("Current number is", i)
Output:
Current number is 1
Current number is 2
Current number is 3
range() and indexing #
You may see code like this:
items = ["a", "b", "c"]
for i in range(len(items)):
print(i, items[i])
Output:
0 a
1 b
2 c
This works, and it uses len() to get the number of items.
But for beginners, direct looping is often simpler:
items = ["a", "b", "c"]
for item in items:
print(item)
If you need both the index and the value, enumerate() is usually a better choice:
items = ["a", "b", "c"]
for index, item in enumerate(items):
print(index, item)
You can learn more in how to use enumerate() in Python.
Converting range() to a list #
range() does not directly show all numbers as a normal list.
To see the values, convert it with list():
print(list(range(3)))
Output:
[0, 1, 2]
This is helpful for:
- learning how
range()works - checking your values
- debugging code
For example:
numbers = range(4)
print(numbers)
print(list(numbers))
Output:
range(0, 4)
[0, 1, 2, 3]
If you want to understand this difference more clearly, see Python range vs list(range) explained.
Common beginner mistakes #
Here are some common problems with range().
Expecting the stop value to be included #
Many beginners think this:
print(list(range(1, 5)))
will produce:
[1, 2, 3, 4, 5]
But the real output is:
[1, 2, 3, 4]
The stop value is excluded.
Assuming range(5) starts at 1 #
It starts at 0, not 1.
print(list(range(5)))
Output:
[0, 1, 2, 3, 4]
Using a string instead of an integer #
This often happens with input():
user_number = input("Enter a number: ")
for i in range(user_number):
print(i)
This fails because input() returns a string.
Correct version:
user_number = int(input("Enter a number: "))
for i in range(user_number):
print(i)
If you see an error here, read TypeError: 'str' object cannot be interpreted as an integer.
Using a negative step with start smaller than stop #
This does not count backward correctly:
print(list(range(1, 5, -1)))
Output:
[]
If the step is negative, the start usually needs to be larger than the stop.
Trying to use step = 0 #
This is not allowed:
range(1, 10, 0)
Python raises an error because the sequence cannot move.
Forgetting that range() is not a list #
This can confuse beginners when printing values:
print(range(5))
Output:
range(0, 5)
To see the numbers, use:
print(list(range(5)))
Output:
[0, 1, 2, 3, 4]
Useful checks while debugging #
If range() is not doing what you expect, these quick checks can help:
print(range(5))
print(list(range(5)))
print(list(range(2, 8)))
print(list(range(10, 0, -2)))
print(type(range(5)))
These help you confirm:
- what values are being created
- whether the direction is correct
- whether you are working with a
rangeobject
FAQ #
Does range() include the last number? #
No. The stop value is excluded.
Why does range(5) start at 0? #
When only one argument is given, Python uses it as the stop value and starts from 0.
Can range() count backward? #
Yes. Use a negative step, such as range(5, 0, -1).
Is range() a list? #
No. It returns a range object. Use list(range(...)) if you need a list.
Why does range(input()) fail? #
input() returns a string. Convert it first with int(input()).