Challenge - 5 Problems
Tuple Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateWhat 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)
Attempts:
2 left
๐ก Hint
Tuples cannot be changed after creation.
โ Incorrect
Tuples are immutable, so trying to assign a new value to an index causes a TypeError.
โ Predict Output
intermediateWhat 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)
Attempts:
2 left
๐ก Hint
Adding tuples joins their elements in order.
โ Incorrect
Using + with tuples concatenates them into a new tuple with all elements.
๐ง Debug
advancedWhy 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)
Attempts:
2 left
๐ก Hint
Number of variables must match number of tuple elements.
โ Incorrect
Unpacking requires the same number of variables as tuple elements; here 3 elements but only 2 variables cause ValueError.
โ Predict Output
advancedWhat 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)
Attempts:
2 left
๐ก Hint
Tuples are immutable but can contain mutable objects.
โ Incorrect
The tuple itself cannot be changed, but the list inside it can be modified, so the list gains the new element.
๐ง Conceptual
expertWhy is tuple immutability important for dictionary keys?
Which reason best explains why tuples can be used as dictionary keys but lists cannot?
Attempts:
2 left
๐ก Hint
Dictionary keys must be hashable and unchanging.
โ Incorrect
Dictionary keys must be immutable and hashable. Tuples are immutable and hashable, lists are mutable and unhashable.
