Codectionary / Developer documentation / Python

range()

range represents an immutable sequence of integers using a start, stop and step instead of storing every number. Use it to iterate over integer positions or repeat an action. Its stop boundary is always excluded.

How it works

range(stop) starts at zero with step one. range(start, stop) chooses the first number, and range(start, stop, step) also chooses the spacing. The arguments must be integers or objects providing an integer index; floats are not accepted.

With a positive step, values must stay below stop. With a negative step, values must stay above stop. A step pointing away from stop produces an empty range; a zero step raises ValueError.

A range is a reusable sequence, not a generator or a list. You can index it, test membership and iterate again without consuming it. Its compact representation avoids allocating a large list; list(range(...)) deliberately creates that list.

Common mistakes and boundaries

Do not add one to every stop value automatically. Decide whether your problem uses an exclusive boundary first.

Prefer for item in items for values, and enumerate(items) for both indexes and values. range(len(items)) is useful when positions are specifically needed, not as the default iteration pattern.

Try it yourself

Predict list(range(5, 0, -2)) and list(range(0, 5, -1)). Explain why one contains three numbers and the other is empty. Check the error from a zero step separately.

Syntax

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

Examples

Basic Range

Generating a sequence from 0 up to (but not including) a number.

for i in range(5):
    print(i)  # 0, 1, 2, 3, 4
  1. The first form includes zero and excludes five, so it prints five separate lines.
  2. Expected output: 0 1 2 3 4

Range with Start and Stop

Specifying both where to begin and where to stop.

for i in range(2, 8):
    print(i)  # 2, 3, 4, 5, 6, 7
  1. The start is included and the stop is excluded, giving six values.
  2. Expected output: 2 3 4 5 6 7

Range with Step

Controlling the interval between numbers, including counting backward with a negative step.

for i in range(0, 10, 2):
    print(i)  # 0, 2, 4, 6, 8

for i in range(10, 0, -2):
    print(i)  # 10, 8, 6, 4, 2
  1. The first loop adds two at a time; the second subtracts two. Neither reaches its stop boundary.
  2. Expected output: 0 2 4 6 8 10 8 6 4 2

Converting Range to a List

Materialise a compact range as a list only when a list is needed.

r = range(5)
print(r)              # range(0, 5)
print(list(r))         # [0, 1, 2, 3, 4]

# Useful when you need indices for a list
items = ["a", "b", "c"]
print(list(range(len(items))))  # [0, 1, 2]
  1. Printing the range shows its compact representation. list materialises the values; using len(items) gives the valid nonnegative indexes here.
  2. Expected output: range(0, 5) [0, 1, 2, 3, 4] [0, 1, 2]

Best practices

  • Remember range(stop) starts at 0 by default, and the stop value itself is never included in the sequence
  • Use range() directly in a for loop rather than converting it to a list first - it's more memory-efficient since it generates values lazily
  • Use a negative step to count downward, e.g. range(10, 0, -1) for a countdown
  • Prefer enumerate() over range(len(list)) when you actually need both the index and the corresponding item

Runtime & versions

Python interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Python language reference

Specifications & further reading