Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The range object in Python is a built-in, immutable sequence type that represents a mathematical progression of integers. Rather than pre-allocating memory for every integer in the sequence, range computes its values dynamically on demand. This lazy evaluation strategy guarantees an O(1)O(1) space complexity, meaning a range of ten takes the exact same amount of memory as a range of ten billion.

Syntax

The range constructor is overloaded and can be instantiated using one, two, or three arguments. All arguments must be integers or objects that implement the __index__() special method.
range(stop)
range(start, stop[, step])
  • start (Optional): The inclusive lower bound of the sequence. Defaults to 0 if omitted.
  • stop (Required): The exclusive upper bound of the sequence. The progression stops before it reaches or exceeds this value.
  • step (Optional): The integer difference between consecutive values. Defaults to 1. It can be negative for descending progressions, but it cannot be 0 (raises a ValueError).

Mechanics and Instantiation

When instantiated, the range object calculates the length of the sequence and stores only the start, stop, and step attributes.

# Single argument: start=0, stop=5, step=1
r1 = range(5)          # Yields: 0, 1, 2, 3, 4


# Two arguments: start=2, stop=7, step=1
r2 = range(2, 7)       # Yields: 2, 3, 4, 5, 6


# Three arguments: start=10, stop=0, step=-2
r3 = range(10, 0, -2)  # Yields: 10, 8, 6, 4, 2

Sequence Protocol Implementation

Despite behaving similarly to a generator during iteration, range is a fully realized sequence type. It implements the complete Python sequence protocol (__getitem__, __len__, __contains__, __iter__, and __reversed__). Because it calculates values mathematically rather than traversing memory, most sequence operations execute in O(1)O(1) time complexity. For membership testing (in), the __contains__ method evaluates mathematically in O(1)O(1) time for integers. However, if testing for membership of a non-integer object (such as a custom class with an overloaded __eq__ method), it falls back to an O(N)O(N) linear search.
r = range(10, 100, 5)


# Length calculation
length = len(r)        # 18


# Indexing (Calculated as: start + (index * step))
val = r[3]             # 25
last_val = r[-1]       # 95


# Slicing (Returns a new range object, not a list)
sub_r = r[2:5]         # range(20, 35, 5)


# Membership testing 
is_member = 25 in r    # True (O(1) mathematical evaluation for integers)

Equality and Hashing

Because range objects are immutable, they are hashable and can be used as dictionary keys or set elements. Equality between range objects is evaluated based on the sequence of values they represent, not their strict instantiation arguments. Two range objects are considered equal (==) if they yield the exact same sequence of integers.

# Both represent an empty sequence
range(0) == range(2, 1, 3)  # True


# Both represent the sequence: 0, 2, 4
range(0, 5, 2) == range(0, 6, 2)  # True
Master Python with Deep Grasping Methodology!Learn More