Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print all keys in the dictionary.
Python
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key in my_dict[1]:
print(key) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using .values() instead of .keys() will give values, not keys.
Using .items() returns key-value pairs, not just keys.
โ Incorrect
Using .keys() lets you loop through all keys in the dictionary.
2fill in blank
mediumComplete the code to print all values in the dictionary.
Python
my_dict = {'x': 10, 'y': 20, 'z': 30}
for value in my_dict[1]:
print(value) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using .keys() will give keys, not values.
Using .items() returns key-value pairs, not just values.
โ Incorrect
Using .values() lets you loop through all values in the dictionary.
3fill in blank
hardFix the error in the code to print keys and values.
Python
my_dict = {'name': 'Alice', 'age': 25}
for key, value in my_dict[1]:
print(key, value) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using .keys() or .values() will cause unpacking errors.
Not using any method will cause a runtime error.
โ Incorrect
Using .items() returns key-value pairs, which can be unpacked in the loop.
4fill in blank
hardFill both blanks to create a dictionary of squares for even numbers only.
Python
numbers = [1, 2, 3, 4, 5] squares = {num: num[1]2 for num in numbers if num [2] 2 == 0}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using + instead of ** will add instead of square.
Using // instead of % will not correctly check even numbers.
โ Incorrect
The ** operator squares the number, and % checks if the number is even.
5fill in blank
hardFill all three blanks to create a dictionary with uppercase keys and values greater than 10.
Python
data = {'a': 5, 'b': 15, 'c': 20}
result = [1]: [2] for [3], val in data.items() if val > 10} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 'v' instead of 'val' causes undefined variable error.
Not using .upper() keeps keys lowercase.
โ Incorrect
Use k.upper() to make keys uppercase, val for values, and k to unpack keys from items.