Tuples are immutable sequences (cannot be modified after creation) defined with parentheses (). Operations (indexing, slicing) are similar to lists, but elements cannot be changed.
Creating and Deleting Tuples
Parentheses:
code.python
>>> t = ('a', 0, {}, False) # Tuple with mixed types
>>> t = 'a', 0, {}, False # Parentheses optional
>>> t = (1,) # Single-element tuple(comma required)
>>> type(t) # <class 'tuple'>
tuple() Function: Convert iterables to tuples.
code.python
>>> tuple() # ()
>>> tuple('abcde') # ('a', 'b', 'c', 'd', 'e')
>>> tuple(range(5)) # (0, 1, 2, 3, 4)
zip() Function: Combine multiple lists into tuples.
code.python
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = zip(a, b) # <zip object>
>>> list(c) # [(1, 4), (2, 5), (3, 6)]
Delete: Use del to remove the entire tuple.
Indexing and Slicing
Identical to lists, but elements cannot be modified:
code.python
>>> t = (1, 2, 3)
>>> t[0] # 1
>>> t[-1] # 3
>>> t[1:5:2] # (2, 4) (indices 1–4, step 2)
>>> t[1] = 3 # Error: 'tuple' object does not support item assignment
Basic Operations
Concatenation: (1, 2, 3) + (4, 5, 6) → (1, 2, 3, 4, 5, 6)
Repetition: ('Hi') * 3 → ('Hi', 'Hi', 'Hi')
Membership: 1 in (1, 2, 3) (True)
Length: len(t)
Min/Max: max(t), min(t)