Python range() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, range(stop) produces the integers 0, 1, ..., stop - 1, and range(start, stop, step) counts from start up to, but not including, stop. It is the standard way to repeat a for loop a number of times.

A range is lazy: it does not store its numbers, it computes them when asked, so range(10 ** 9) uses almost no memory. It is still a real sequence that supports len(), indexing, slicing, and in. Printing it shows its definition, such as range(0, 5), not the numbers.

range is a built-in type (a class) that you call like a function: each call creates a new range object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

One, two, or three integer arguments.

python
for i in range(stop): ...
for i in range(start, stop): ...
for i in range(start, stop, step): ...
CallWhat it does
range(5)0, 1, 2, 3, 4.
range(2, 8)2 up to 7.
range(10, 0, -3)10, 7, 4, 1 (counting down).
list(range(3))Returns [0, 1, 2].

Use enumerate() instead of range(len(items)) when you need both the index and the item.

Library declarations
python
range(stop, /)
range(start, stop, step=1, /)
# class range: an immutable built-in sequence type

Parameters

ParameterDescription
startOptional. First number. Default 0.
stopRequired. The range stops before this number.
stepOptional. Difference between numbers; negative counts down. Default 1; must not be 0.

Return Value

Return ValueDescription
rangeAn immutable, lazy sequence of ints.

Python Examples

Example 1 — range() with One, Two, and Three Arguments

List the numbers of several ranges and print a range object directly.

python
print(list(range(5)))
print(list(range(2, 8)))
print(list(range(10, 0, -3)))
print(list(range(0)))
print(range(5))

How It Works

  1. Stop is excluded. range(5) ends at 4 and range(2, 8) ends at 7.
  2. Negative step. range(10, 0, -3) counts down by three and stops before 0.
  3. Empty ranges. range(0) has no numbers at all.
  4. Lazy repr. Printing the range itself shows range(0, 5); list() is needed to see the numbers.

Example 2 — len, Indexing, in, and Slicing on a range

Treat a range like a list without ever building one.

python
r = range(0, 100, 5)

print(len(r))
print(r[3], r[-1])
print(35 in r, 36 in r)
print(r.index(50))
print(r[2:5])
print(range(0, 3) == range(0, 3, 1))

How It Works

  1. Length computed. Multiples of 5 below 100 number 20, and len() calculates that without generating them.
  2. Indexing. r[3] is 15 and r[-1] is the last value, 95.
  3. Fast in. For ints, in is answered with arithmetic, so 35 is found and 36 is not.
  4. Slices are ranges. r[2:5] returns a new range, and equal ranges compare equal.

Example 3 — Count Down, Add Up, and Invalid Arguments

Use range() in a countdown loop and a total, then pass invalid arguments.

python
for i in range(3, 0, -1):
    print("countdown", i)

total = sum(range(1, 101))
print("1 + 2 + ... + 100 =", total)

try:
    range(0, 10, 0)
except ValueError:
    print("step must not be zero")

try:
    range(2.5)
except TypeError:
    print("range() needs integers")

How It Works

  1. Countdown. range(3, 0, -1) yields 3, 2, 1 and stops before 0.
  2. Upper bound + 1. To include 100, the stop must be 101; sum() then gives 5050.
  3. Zero step. A step of 0 would never finish, so Python raises ValueError.
  4. Integers only. A float argument raises TypeError.

Frequently Asked Questions

Ranges stop before the stop value. range(5) has exactly 5 numbers, 0 to 4, which matches list indexes.
Use a negative step: range(10, 0, -1) counts 10 down to 1. reversed(range(1, 11)) does the same.
A range is lazy and prints its definition. Use list(range(5)) to see the numbers.
No. All arguments must be integers. For float steps, multiply an int range or use a loop with a counter.
No, it is an immutable lazy sequence. It supports indexing, slicing, len(), and in, but you cannot append to it.

Next: repr()

Developer-friendly string representation of an object.

repr() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful