0
0
Pythonprogramming~20 mins

Tuple immutability in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Tuple Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of tuple modification attempt?
Consider this code trying to change a tuple element. What will it output or raise?
Python
t = (1, 2, 3)
t[1] = 5
print(t)
A(1, 2, 3)
BTypeError
C(1, 5, 3)
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint
Tuples cannot be changed after creation.
โ“ Predict Output
intermediate
2:00remaining
What is the output after concatenating tuples?
What will be printed after this code runs?
Python
t1 = (1, 2)
t2 = (3, 4)
t3 = t1 + t2
print(t3)
ATypeError
B[(1, 2), (3, 4)]
C(4, 3, 2, 1)
D(1, 2, 3, 4)
Attempts:
2 left
๐Ÿ’ก Hint
Adding tuples joins their elements in order.
๐Ÿ”ง Debug
advanced
2:00remaining
Why does this tuple unpacking code raise an error?
This code tries to unpack a tuple but raises an error. Which option explains the cause?
Python
t = (1, 2, 3)
a, b = t
print(a, b)
AValueError because tuple has 3 items but only 2 variables to unpack
BTypeError because tuple is immutable
CSyntaxError due to missing parentheses
DNo error, prints '1 2'
Attempts:
2 left
๐Ÿ’ก Hint
Number of variables must match number of tuple elements.
โ“ Predict Output
advanced
2:00remaining
What is the output when modifying a mutable element inside a tuple?
What will this code print?
Python
t = (1, [2, 3], 4)
t[1].append(5)
print(t)
A(1, [2, 3, 5], 4)
BSyntaxError
C(1, [2, 3], 4)
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Tuples are immutable but can contain mutable objects.
๐Ÿง  Conceptual
expert
2:00remaining
Why is tuple immutability important for dictionary keys?
Which reason best explains why tuples can be used as dictionary keys but lists cannot?
ALists can contain duplicate elements, tuples cannot
BTuples are faster to create than lists
CTuples are immutable, so their hash value does not change, making them valid keys
DLists are mutable but tuples are not, so lists cannot be stored in memory
Attempts:
2 left
๐Ÿ’ก Hint
Dictionary keys must be hashable and unchanging.