Tuple Operations
Accessing and unpacking tuples
Interview Relevant: Common operations
Tuple Operations
Operations on tuples.
Code Examples
Tuple operations and methods.
python
1t = (1, 2, 3, 2, 1)
2
3# Methods (only 2!)
4t.count(2) # 2
5t.index(3) # 2
6
7# Concatenation
8(1, 2) + (3, 4) # (1, 2, 3, 4)
9
10# Repetition
11(1, 2) * 3 # (1, 2, 1, 2, 1, 2)
12
13# Membership
142 in t # True
15
16# Length
17len(t) # 5
18
19# Convert to list and back
20list(t) # [1, 2, 3, 2, 1]
21tuple([1, 2]) # (1, 2)