Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new key-value pair to the dictionary.
Python
my_dict = {'a': 1, 'b': 2}
my_dict[[1]] = 3
print(my_dict) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using an existing key will update the value, not add a new pair.
Forgetting to put the key in quotes if it is a string.
โ Incorrect
To add a new key-value pair, use a new key like 'c'.
2fill in blank
mediumComplete the code to update the value of an existing key in the dictionary.
Python
my_dict = {'x': 10, 'y': 20}
my_dict[[1]] = 30
print(my_dict) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a new key will add a new pair instead of updating.
Forgetting to use quotes around the key string.
โ Incorrect
To update a value, use the existing key 'x'.
3fill in blank
hardFix the error in the code to add a key-value pair correctly.
Python
data = {'name': 'Alice'}
data[[1]] = 'Bob'
print(data) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a variable name without quotes causes a NameError.
Confusing keys with values.
โ Incorrect
Keys must be strings in quotes if they are text.
4fill in blank
hardFill both blanks to add a new key-value pair where the key is 'color' and the value is 'blue'.
Python
info = {'size': 'large'}
info[[1]] = [2]
print(info) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Swapping key and value positions.
Forgetting quotes around strings.
โ Incorrect
Use 'color' as the key and 'blue' as the value to add the pair.
5fill in blank
hardFill all three blanks to update the dictionary with a new key 'age' and value 25, then print the updated dictionary.
Python
person = {'name': 'John', 'city': 'NYC'}
person[[1]] = [2]
print([3]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using quotes around numbers.
Printing a wrong variable name.
โ Incorrect
Use 'age' as the key, 25 as the value, and print the dictionary variable 'person'.