Python range()

Learn how Python range() works with examples. Use range() in for loops, understand start, stop, and step, create reverse ranges, convert range to list, use negative steps, and avoid common range mistakes.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Python range()

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.

python
print(list(range(5)))
print(list(range(2, 6)))
print(list(range(2, 10, 2)))
Output

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 start up to but not including stop
  • 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

text
range(stop)
range(start, stop)
range(start, stop, step)

Rules:

  • start defaults to 0
  • stop is required
  • stop is not included in the sequence
  • step defaults to 1
  • step cannot be 0
  • Arguments must be integers or objects that behave like integers

Using start, stop, and step

range(stop)

Starts at 0 and stops before stop.

python
print(list(range(5)))
Output

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.

python
print(list(range(1, 6)))
Output

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.

python
print(list(range(0, 10, 2)))
print(list(range(1, 10, 2)))
Output

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.

python
for i in range(5):
    print(i)
Output

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.

python
for count in range(3):
    print("repeat")
Output

This prints repeat three times.


Reverse range and negative step

Use a negative step to count backward. The stop value is still excluded.

python
print(list(range(5, 0, -1)))
print(list(range(5, -1, -1)))
Output

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:

python
print(list(range(5, 2)))
print(list(range(2, 5, -1)))
Output

Both produce [].


Convert range to list

A range object is lazy. It does not store every number upfront.

python
numbers = range(5)
print(numbers)
print(list(numbers))
Output

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.

python
print(list(range(1, 5)))
print(list(range(1, 6)))
Output

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:

python
values = range(5)
print(type(values))
print(values[0])
print(values[-1])
print(3 in values)
Output

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

python
print(10 in range(0, 20, 2))
print(11 in range(0, 20, 2))
Output

The first line is True. The second is False.

range(len(items)) vs direct iteration

range(len(items)) works when you need indexes:

python
items = ["a", "b", "c"]

for i in range(len(items)):
    print(i, items[i])
Output

If you only need values, loop over the list directly:

python
items = ["a", "b", "c"]

for value in items:
    print(value)
Output

If you need both index and value, prefer enumerate():

python
items = ["a", "b", "c"]

for i, value in enumerate(items):
    print(i, value)
Output

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:

python
print(list(range(-5, 0)))
print(list(range(0, -5, -1)))
print(list(range(0, -5)))
Output

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:

python
print(list(range(0)))
print(len(range(5, 2)))
Output

range(0) and range(5, 2) both produce no values.


Can range() use floats?

No. range() is for integers.

python
# range(0.0, 5.0)  # TypeError
values = [x * 0.5 for x in range(0, 10, 2)]
print(values)
Output

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.


References


Frequently Asked Questions

1. What does range() do in Python?

range() returns a range object that represents a sequence of integers. It is most commonly used with for loops when you need to repeat code a fixed number of times or iterate over numeric values.

2. Does range() include the stop value?

No. range() uses a half-open interval, so counting stops before stop. range(1, 5) gives 1, 2, 3, 4. Use range(1, 6) to include 5.

3. Does range() return a list in Python 3?

No. In Python 3, range() returns a memory-efficient range object. Use list(range(...)) when you need a real list of all values.

4. How do you count backward with range()?

Use a negative step, for example range(5, 0, -1) for 5 down to 1. The stop value is still excluded.

5. Can range() use floating-point numbers?

No. range() works with integers. For decimal sequences in plain Python, build values inside a loop or comprehension. For numeric arrays, use NumPy functions such as arange or linspace.

6. When should I use enumerate() instead of range(len(items))?

Use range(n) when you only need to repeat code n times. Loop directly over the list when you only need values. Use enumerate(items) when you need both index and value.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …