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

A tuple is a built-in Python data structure that represents an ordered, immutable sequence of elements. Tuples can store heterogeneous data types and are hashable if all their constituent elements are also hashable. Because they are immutable, their size and element references are fixed at the time of creation.

## Syntax and Creation

Tuples are typically defined using parentheses `()`, with elements separated by commas. However, the comma is the actual tuple-defining operator; the parentheses are only required for empty tuples or to avoid syntactic ambiguity.

```python theme={"dark"}

# Standard initialization
empty_tuple = ()
standard_tuple = (1, "string", 3.14)


# Single-element tuple requires a trailing comma
single_element = (42,)
not_a_tuple = (42)  # Evaluates to an integer, not a tuple


# Implicit creation (Tuple Packing)
packed_tuple = 1, 2, 3


# Constructor from an iterable
iterable_to_tuple = tuple([1, 2, 3])
```

## Immutability Mechanics

Immutability in tuples applies to the *references* held by the tuple, not necessarily the underlying objects. You cannot add, remove, or reassign elements. However, if a tuple contains a reference to a mutable object (like a list), the internal state of that mutable object can still be modified.

```python theme={"dark"}
t = (1, [2, 3], 4)


# Attempting to reassign a reference raises a TypeError

# t[0] = 10  # TypeError: 'tuple' object does not support item assignment


# Modifying a mutable object referenced by the tuple is permitted
t[1].append(99)

# t evaluates to: (1, [2, 3, 99], 4)
```

## Indexing and Slicing

Tuples support standard Python sequence operations, including zero-based indexing, negative indexing, and slicing. Slicing a tuple always returns a new tuple object.

```python theme={"dark"}
t = ('a', 'b', 'c', 'd', 'e')


# Indexing
first_item = t[0]        # 'a'
last_item = t[-1]        # 'e'


# Slicing [start:stop:step]
slice_tuple = t[1:4]     # ('b', 'c', 'd')
reversed_tuple = t[::-1] # ('e', 'd', 'c', 'b', 'a')
```

## Unpacking

Tuple unpacking allows the extraction of tuple elements into distinct variables in a single operation. Python also supports extended unpacking using the `*` (iterable unpacking) operator to capture remaining elements into a list.

```python theme={"dark"}
t = (10, 20, 30)
x, y, z = t  # x=10, y=20, z=30


# Extended unpacking
t2 = (1, 2, 3, 4, 5)
first, *middle, last = t2

# first = 1

# middle = [2, 3, 4]

# last = 5
```

## Built-in Methods

Because tuples cannot be modified in place, they lack the mutation methods found in lists (e.g., `append()`, `remove()`). They expose only two built-in methods:

```python theme={"dark"}
t = (1, 2, 2, 3, 1, 1)


# count(value): Returns the integer count of occurrences of 'value'
ones_count = t.count(1)  # Returns 3


# index(value): Returns the zero-based index of the first occurrence of 'value'
first_two_idx = t.index(2)  # Returns 1
```

## Memory and Performance Characteristics

Under the hood, CPython implements tuples as arrays of pointers. Because their length is immutable, Python allocates the exact memory block required for the elements at creation. This contrasts with lists, which are dynamic arrays that utilize over-allocation to optimize future `append()` operations. Consequently, tuples have a strictly smaller memory footprint than lists of the same length and offer slightly faster iteration and instantiation times.

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