The range() function represents a sequence of integers in Python. It is most commonly used with for loops when you want to repeat code a fixed number of times or walk through numeric values without building a full list in memory.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Quick answer: create a numeric sequence with range()
Use range(stop), range(start, stop), or range(start, stop, step) to create a sequence of integers, usually for a for loop. The stop value is not included.
print(list(range(5)))
print(list(range(2, 6)))
print(list(range(2, 10, 2)))These print [0, 1, 2, 3, 4], [2, 3, 4, 5], and [2, 4, 6, 8]. The last value in each sequence stops before stop.
Python range() quick reference
| Task | Use |
|---|---|
| Count from 0 to 4 | range(5) |
| Count from 1 to 5 | range(1, 6) |
| Count by 2 | range(0, 10, 2) |
| Count backward | range(5, 0, -1) |
| Include 10 when counting up | range(1, 11) |
| Convert range to list | list(range(5)) |
| Loop 5 times | for i in range(5): |
| Loop over indexes | for i in range(len(items)): |
| Prefer index + value | for i, value in enumerate(items): |
| Check membership | 5 in range(10) |
| Empty range | range(5, 2) |
| Invalid step | range(1, 5, 0) raises ValueError |
What is range() in Python?
range() is a built-in function that represents a sequence of integers. In Python 3 it returns a range object, not a list. The object stores only start, stop, and step, so it uses a small fixed amount of memory even for large spans.
Use range() when you need:
- A loop that runs a fixed number of times
- Numbers from
startup to but not includingstop - Custom increments, including counting backward with a negative step
The Python tutorial on range describes range() as generating arithmetic progressions. Range objects are one of Python’s basic immutable sequence types.
Python range() syntax
range(stop)
range(start, stop)
range(start, stop, step)Rules:
startdefaults to0stopis requiredstopis not included in the sequencestepdefaults to1stepcannot be0- Arguments must be integers or objects that behave like integers
Using start, stop, and step
range(stop)
Starts at 0 and stops before stop.
print(list(range(5)))The result is [0, 1, 2, 3, 4]. This form is common when you only need to repeat a loop five times and the exact numbers matter less than the count.
range(start, stop)
Starts at start and stops before stop.
print(list(range(1, 6)))The result is [1, 2, 3, 4, 5]. When counting upward and you want to include 5, set stop to 6.
range(start, stop, step)
The third argument controls the increment.
print(list(range(0, 10, 2)))
print(list(range(1, 10, 2)))The first call gives even numbers 0, 2, 4, 6, 8. The second gives odd numbers 1, 3, 5, 7, 9. step can be positive or negative.
range() with for loops
The most common use of range() is inside a for loop.
for i in range(5):
print(i)This runs five times. On each pass, i is 0, then 1, then 2, then 3, then 4. Use a meaningful variable name when the number itself carries meaning, such as year or page_index.
for count in range(3):
print("repeat")This prints repeat three times.
Reverse range and negative step
Use a negative step to count backward. The stop value is still excluded.
print(list(range(5, 0, -1)))
print(list(range(5, -1, -1)))The first gives [5, 4, 3, 2, 1]. The second continues down to [5, 4, 3, 2, 1, 0]. For more reverse-range patterns, see Python range reverse examples.
If start, stop, and step point in conflicting directions, the range is empty:
print(list(range(5, 2)))
print(list(range(2, 5, -1)))Both produce [].
Convert range to list
A range object is lazy. It does not store every number upfront.
numbers = range(5)
print(numbers)
print(list(numbers))You should see range(0, 5) and then [0, 1, 2, 3, 4]. Use list(range(...)) when you need to inspect or reuse all values. Avoid converting huge ranges to lists unless you truly need every value in memory.
range() does not include the stop value
This is one of the most common beginner mistakes. range(1, 5) stops before 5.
print(list(range(1, 5)))
print(list(range(1, 6)))The first prints [1, 2, 3, 4]. The second includes 5 because the stop is 6. This half-open interval matches Python slicing: the end index is never part of the sequence.
range() returns a range object, not a list
In Python 3, range(5) is a range object:
values = range(5)
print(type(values))
print(values[0])
print(values[-1])
print(3 in values)You get range, index access, and membership testing without creating a list. In Python 2, range() returned a list. Python 2’s xrange() behaved more like Python 3’s range().
Membership, indexes, and enumerate()
Check if a number is in range
print(10 in range(0, 20, 2))
print(11 in range(0, 20, 2))The first line is True. The second is False.
range(len(items)) vs direct iteration
range(len(items)) works when you need indexes:
items = ["a", "b", "c"]
for i in range(len(items)):
print(i, items[i])If you only need values, loop over the list directly:
items = ["a", "b", "c"]
for value in items:
print(value)If you need both index and value, prefer enumerate():
items = ["a", "b", "c"]
for i, value in enumerate(items):
print(i, value)range() vs enumerate()
| Use case | Better choice |
|---|---|
| Repeat code N times | range(n) |
| Need only values from a list | for value in items |
| Need index and value | enumerate(items) |
| Need numeric sequence | range(start, stop, step) |
Negative numbers and empty ranges
start and stop can be negative when the direction matches the step:
print(list(range(-5, 0)))
print(list(range(0, -5, -1)))
print(list(range(0, -5)))The first gives [-5, -4, -3, -2, -1]. The second counts down from 0 to -4. The third is empty because the default step is +1 while start is greater than stop.
Other empty examples:
print(list(range(0)))
print(len(range(5, 2)))range(0) and range(5, 2) both produce no values.
Can range() use floats?
No. range() is for integers.
# range(0.0, 5.0) # TypeError
values = [x * 0.5 for x in range(0, 10, 2)]
print(values)For large numeric arrays with decimals, use NumPy functions such as numpy.arange() or numpy.linspace() instead of forcing range().
Summary
range() represents a sequence of integers. Use range(stop) to start from 0, range(start, stop) to choose the start, and range(start, stop, step) to change the increment. The stop value is always excluded. A negative step counts backward. In Python 3, range() returns a memory-efficient range object, not a list. Pair it with a for loop or enumerate() when you need iteration patterns beyond bare counts.

