Python foundations

Python range(): Start, Stop, Step, and Countdown

Use range(1, 11) to count from 1 to 10. See how the exclusive stop and a negative step affect Python loops.

In Python, range(stop) starts at zero and stops before stop. To count from 1 through 10, use range(1, 11). Use a negative third argument to count down, such as range(3, 0, -1) for 3, 2, and 1.

assert list(range(4)) == [0, 1, 2, 3]
assert list(range(1, 11)) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
assert list(range(8, 0, -2)) == [8, 6, 4, 2]
assert list(range(3, -1, -1)) == [3, 2, 1, 0]

You can pass a range directly to a loop. The examples convert short ranges to lists only so that the exact values can be inspected.

Decide where the exclusive endpoint belongs

With a positive step, each value must be less than the stop. With a negative step, it must be greater than the stop. The step can skip right over the boundary. range(2, 10, 3) gives 2, 5, and 8, then stops because the next value would be 11.

The exclusive endpoint matches Python sequence positions. A list with six entries has valid positions 0 through 5, and range(6) produces exactly those possibilities. When you only need the values in the list, for value in items avoids any manual boundary. When you need neighboring pairs, zip(items, items[1:]) is an option for small lists, while a range of valid starting positions may avoid creating a large slice.

Why a range is empty or raises ValueError

An increasing range with a start at or above its stop is empty. A decreasing range with a start at or below its stop is also empty. These calls are valid. For example, range(5, 0) uses the default step of positive one and yields nothing.

A step of zero raises ValueError. Returning no values is a meaningful outcome when there is no work. A zero step has no progression at all, so guard against it at the input boundary if the step comes from a user.

Use a range for positions, not for every repeated action

Many programs use for _ in range(3): to repeat an action three times without using the iteration number. The underscore makes that intent apparent. If you need a display label, for attempt in range(1, 4): gives attempts numbered 1 through 3. The stop remains exclusive in each case.

range(0, 2, 0.5) raises TypeError. The arguments must be integers or objects Python accepts as integer indexes. To generate quarter steps, use [i / 4 for i in range(8)] for 0 through 1.75. For values that require exact decimal arithmetic, calculate with Decimal or integer minor units.

Keep memory and intent clear

A range stores its parameters rather than holding a separate list entry for every number. A loop over a large range therefore does not need to allocate the complete list first. Processing a large range can still take a long time if you visit every value. Distinguish the memory cost of representing the sequence from the work of consuming it.

A useful boundary test checks the first item, the final item, and the number of items for a small case. Add an empty range and a descending range if your program supports them. These tests catch the difference between “number of attempts” and “final attempt label” before the result appears in a user-facing report.

Sources and further reading

Related reading

Browse Python foundations · Explore project tutorials

Search PythonProjects.org