0
0
Pythonprogramming~10 mins

Tuple unpacking in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to unpack the tuple into variables a and b.

Python
t = (5, 10)
a, [1] = t
print(a, b)
Drag options to blanks, or click blank then click option'
Ax
Bc
Cb
Dy
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a variable name not matching the tuple length causes errors.
Trying to unpack into only one variable.
2fill in blank
medium

Complete the code to unpack the tuple and print the second value.

Python
coordinates = (3, 7)
x, [1] = coordinates
print(y)
Drag options to blanks, or click blank then click option'
Az
By
Ca
Db
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a variable name different from the one printed causes NameError.
Unpacking into fewer or more variables than tuple length causes errors.
3fill in blank
hard

Complete the code to unpack the tuple into variables a, b, and c.

Python
values = (1, 2, 3)
a, b, [1] = values
print(a, b, c)
Drag options to blanks, or click blank then click option'
Ac
Bd
Ce
Df
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a variable name that doesn't match the print statement causes NameError.
Unpacking into the wrong number of variables causes ValueError.
4fill in blank
hard

Fill both blanks to create a dictionary with keys as names in uppercase and values as ages over 20.

Python
people = {name[1]: age for name, age in data if age [2] 20}
Drag options to blanks, or click blank then click option'
A.upper()
B>
C<
D.lower()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using < instead of > for filtering.
Using .lower() instead of .upper() for keys.
5fill in blank
hard

Fill all three blanks to create a dictionary with keys as uppercase names, values as ages, filtered by age greater than 25.

Python
result = [1]: [2] for name, age in people if age [3] 25}
Drag options to blanks, or click blank then click option'
Aname.upper()
Bage
C>
Dname.lower()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using name.lower() instead of name.upper().
Using < instead of > for filtering.
Swapping keys and values in the dictionary comprehension.