Tuple Basics
Creating and using tuples
Interview Relevant: Immutable sequences
Tuple Basics
Tuples are immutable, ordered sequences.
Code Examples
Basic tuple operations.
python
1# Create tuples
2empty = ()
3single = (1,) # Note the comma!
4multiple = (1, 2, 3)
5mixed = (1, "hello", 3.14)
6
7# Without parentheses (tuple packing)
8t = 1, 2, 3
9
10# Access (like lists)
11t[0] # 1
12t[-1] # 3
13t[1:3] # (2, 3)
14
15# Immutable - can't modify!
16# t[0] = 10 # TypeError!
17
18# But can contain mutable objects
19t = ([1, 2], [3, 4])
20t[0].append(3) # OK - modifying list inside