PYTHON Reference Guide
Revision Time: 3 mins

Tuple Reference

Immutable sequences for packing fixed records.

count
Return: int

Count occurrences of a value inside the tuple.

Used In: Record metrics counters.

Syntax signature:tuple.count(x)
Code snippet:
python
(1, 2, 1, 1).count(1)
Expected Output:3

Time Complexity: O(N)

Remember: Scans the immutable tuple linearly in O(N) time.
index
Return: int

Return index of the first occurrence of a value in the tuple.

Used In: Locating column offsets inside fixed schema tuples.

Syntax signature:tuple.index(x)
Code snippet:
python
(10, 20, 30).index(20)
Expected Output:1

Time Complexity: O(N)

Remember: Raises ValueError if the value is missing.
Tuple Packing
Return: tuple

Pack multiple independent values into a single tuple object.

Used In: Returning multi-attribute structures from data parsers.

Syntax signature:t = x, y, z
Code snippet:
python
record = 1, 'Alex', 'DE'
print(record)
Expected Output:(1, 'Alex', 'DE')

Time Complexity: O(1)

Related Methods: Tuple Unpacking

Remember: Parentheses are optional but highly recommended for readability.
Tuple Unpacking
Return: None

Assign individual elements of a tuple to separate variables.

Used In: Deconstructing database rows, reading zip objects.

Syntax signature:x, y = t
Code snippet:
python
t = (1, 'DE')
id, dept = t
print(id, dept)
Expected Output:1 DE

Time Complexity: O(1)

Related Methods: Tuple Packing

Remember: Requires the number of variables to exactly match the tuple size (unless asterisk * is used).
Multiple Assignment
Return: None

Define and assign values to multiple variables in a single line.

Used In: Boundary swaps in algorithms, bulk initializer settings.

Syntax signature:x, y = val1, val2
Code snippet:
python
left, right = 0, 10
left, right = right, left
print(left, right)
Expected Output:10 0

Time Complexity: O(1)

Remember: Under the hood, Python packs the right side into a tuple first, then unpacks it into the left variables, making it safe for variables swapping.
Nested Tuples
Return: tuple

Store tuples inside another tuple to represent immutable nested records.

Used In: Immutable matrices, coordinate lists.

Syntax signature:t = ((x, y), (a, b))
Code snippet:
python
coord = ((10, 20), (30, 40))
print(coord[0][1])
Expected Output:20

Time Complexity: O(1)

Remember: Perfect for matrix references or coordinates that must never change.