What built-in Python function is used to generate a sequence of numbers?

The story behind the answer

The built-in Python function used to generate a sequence of numbers is `range`.

`range()` represents an immutable sequence of integers defined by a start, a stop, and an optional step. With one argument, `range(stop)` begins at 0 and stops before the specified value, so `range(5)` represents 0 through 4. The stop value is exclusive, a detail that often causes off-by-one errors.

With two arguments, `range(start, stop)` sets the beginning explicitly; with three, the step controls the interval and may be negative. In Python 3, `range()` returns a compact range object rather than creating a full list immediately, which makes it memory-efficient for large loops. It supports indexing, length checks, and membership testing while still describing the pattern mathematically.

`xrange` was the Python 2 name for a lazy range-like object and is not a separate built-in in Python 3. `arange` belongs primarily to NumPy, while `linspace` is commonly used to create evenly spaced values, often including a chosen endpoint.

Source: Wikipedia · fact-checked Sept. 2026

Add question to a list

Choose a list to keep this question in: