← All languages
Python function of the day
Random

range

Return an immutable sequence of numbers from start to stop with a given step.

Description

The range() function returns an immutable sequence of numbers, commonly used for looping a specific number of times in for loops. It can be called with one, two, or three arguments: range(stop), range(start, stop), or range(start, stop, step).

When called with a single argument, range(stop) generates numbers from 0 up to but not including stop. With two arguments, range(start, stop) generates numbers from start up to but not including stop. The optional step argument controls the increment between values and defaults to 1.

Range objects are memory-efficient because they calculate values on demand rather than storing them all in memory. They support membership testing, indexing, slicing, and negative indices. Range is one of the most fundamental built-in functions in Python, used extensively in iteration, list generation, and numeric computations.

Arguments

NameDescriptionOptional
start The starting value of the sequence (default 0). Yes
stop The end value of the sequence (exclusive). No
step The step between each value in the sequence (default 1). Yes

Example

list(range(5))  # Returns [0, 1, 2, 3, 4]
list(range(2, 8))  # Returns [2, 3, 4, 5, 6, 7]
list(range(0, 10, 2))  # Returns [0, 2, 4, 6, 8]

Reference