Last modified: Mar 30, 2026 By Alexander Williams
Python Range Function: Generate Number Sequences
The range() function is a built-in Python tool. It generates a sequence of numbers. This sequence is often used to control loops.
It is memory efficient. It does not create a list in memory. Instead, it produces numbers one by one as needed. This is crucial for large sequences.
Understanding the Range Syntax
The range() function can be called with one, two, or three arguments. These are start, stop, and step.
The basic syntax is range(start, stop, step). Only the stop argument is required. The sequence goes up to, but does not include, the stop value.
For a deeper look at its arguments, see our Python Range Function Guide: Syntax & Examples.
Using One Argument: range(stop)
When you use one argument, it is the stop value. The sequence starts at 0 and increments by 1.
# Generate numbers from 0 to 4
for i in range(5):
print(i)
0
1
2
3
4
Using Two Arguments: range(start, stop)
With two arguments, you define the start and stop. The step remains 1.
# Generate numbers from 2 to 6
for i in range(2, 7):
print(i)
2
3
4
5
6
Using Three Arguments: range(start, stop, step)
Three arguments give you full control. You set the start, stop, and step size. The step can be positive or negative.
# Count even numbers from 0 to 10
for i in range(0, 11, 2):
print(i)
0
2
4
6
8
10
# Count down from 5 to 1
for i in range(5, 0, -1):
print(i)
5
4
3
2
1
Common Use Cases for Range
The primary use is in for loops. It lets you repeat an action a specific number of times.
Iterating Over a Sequence
You can use range() with len() to loop through a list by index.
fruits = ['apple', 'banana', 'cherry']
for i in range(len(fruits)):
print(f"Index {i}: {fruits[i]}")
Index 0: apple
Index 1: banana
Index 2: cherry
Creating Lists and Tuples
While range() itself is not a list, you can convert it. Use the list() or tuple() functions.
# Create a list of squares
squares = []
for i in range(1, 6):
squares.append(i ** 2)
print(squares)
[1, 4, 9, 16, 25]
A more Pythonic way is to use a list comprehension.
squares = [i**2 for i in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]
Important Behaviors and Limitations
Understanding how range() works prevents common errors.
Range is Not Inclusive of Stop
The stop value is never included in the sequence. This is a key point for beginners. The sequence runs from start to stop-1.
If you need to include the stop value, you must adjust your arguments. For example, use range(1, 11) to get 1 through 10.
Learn more about this behavior in our article Is Python Range Inclusive? Stop Value Explained.
Range Works Only with Integers
The range() function only accepts integer arguments. You cannot use floats for start, stop, or step.
# This will cause a TypeError
# for i in range(0.0, 5.0, 0.5):
# print(i)
If you need a sequence of floats, you must use a different method. Check out Python Range Float: How to Generate Sequences for solutions.
Range Objects are Iterables
A range object is its own type. It is not a list or tuple. It is a lazy iterable that generates values on demand.
my_range = range(3)
print(type(my_range)) #
print(list(my_range)) # Convert to list to see values
[0, 1, 2]
Tips and Best Practices
Use range() effectively with these tips.
Prefer direct iteration over sequences when you don't need the index. Use for fruit in fruits: instead of range(len(fruits)).
For counting backwards, remember to use a negative step. Ensure your start value is greater than your stop value.
If you frequently need to include the stop value, consider writing a helper function. Our guide on Python Range Inclusive: How to Include the Stop Value shows you how.
Conclusion
The Python range() function is a fundamental tool. It creates efficient integer sequences for looping and iteration.
Remember its three arguments: start, stop, and step. The stop value is exclusive. The function only works with integers.
Mastering range() is a key step in writing clean, efficient, and Pythonic code. Use it to control loops, generate lists, and handle repetitive tasks with ease.