> ## 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.

# Python range

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)$ 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.

```text theme={"dark"}
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.

```python theme={"dark"}

# 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)$ time complexity. For membership testing (`in`), the `__contains__` method evaluates mathematically in $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)$ linear search.

```python theme={"dark"}
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.

```python theme={"dark"}

# 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
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
