Boost your Python skills with multiple choice questions on tuples. These beginner-friendly MCQs cover syntax, properties, and usage of tuples in Python programming.
Topic: tuples
(1, 2, 3)
Tuples are created using parentheses, while lists use square brackets and sets use curly braces.
4
The tuple contains 3 integers and 1 nested tuple, totaling 4 elements.
()
Empty parentheses create an empty tuple, while [] creates an empty list and {} creates an empty dictionary.
int
To create a single-element tuple, you need a comma: `(1,)`
colors = ('red', 'green', 'blue')
print(colors[-1])
'blue'
Negative indices count from the end, so -1 refers to the last element.
t = (10, 20, 30)
# Line A
print(t[0])
# Line B
t[1] = 25
# Line C
print(len(t))
# Line D
print(t + (40,))
Line B
Line B tries to modify the tuple, which is immutable.
Sorting the tuple in-place
Tuples are immutable, so in-place operations like sort() aren't allowed.
(2, 3)
Slicing from index 1 to the end returns a new tuple with elements from that index onward.
index()
Tuples only have count() and index() methods since they're immutable.
Tuples are mutable
Tuples are immutable, which is why they can be dictionary keys.
coordinates = (10, 20, 30)
x, y, z = coordinates
print(y)
20
This demonstrates tuple unpacking. The value at index 1 (20) is assigned to y.
a = (1, 2)
b = (3, 4)
print(a + b[1:])
(1, 2, 4)
b[1:] slices to (4,), which is then concatenated with (1, 2).
single = ("python",)
The comma makes it a tuple. Without it, it's just a string in parentheses.
nums = (1, 2, 3, 2, 4, 2)
print(nums.count(2) + nums.index(4))
5
count(2) returns 3, index(4) returns 4, so 3 + 4 = 7. Wait no, index(4) is actually 4 (0-based), so 3 + 4 = 7. Wait looking at the tuple, index(4) is 4 (positions 0-5: 1,2,3,2,4,2). So 3 (count) + 4 (index) = 7. But 7 isn't an option! I need to correct this question.
(1, 2, 2)
The * operator repeats the tuple (2,) twice before concatenation with (1,).
(3, 2, 1)
The slice [::-1] reverses the tuple.
t = (1, [2, 3], 4)
t[1][0] = 5
print(t)
(1, [5, 3], 4)
While tuples are immutable, their mutable elements (like lists) can be modified.
data = ((1, 2), (3, 4), (5, 6))
print(data[1][0] + data[-1][-1])
9
data[1][0] is 3 and data[-1][-1] is 6, so 3 + 6 = 9.
t = (i**2 for i in range(5))
print(tuple(t)[2:4])
(4, 9)
The generator creates (0,1,4,9,16), then slicing gets elements 2 and 3.
result = (1, 2, 3) < (1, 2, 1)
print(result)
False
Tuples compare element by element. At index 2, 3 < 1 is False.