0
0
Pythonprogramming~10 mins

Adding and updating key-value pairs 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 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'
A'd'
B'b'
C'a'
D'c'
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.
2fill in blank
medium

Complete 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'
A'z'
B'x'
C'a'
D'y'
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.
3fill in blank
hard

Fix 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'
A'age'
Bage
Cname
D'name'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a variable name without quotes causes a NameError.
Confusing keys with values.
4fill in blank
hard

Fill 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'
A'color'
B'blue'
C'red'
D'shape'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Swapping key and value positions.
Forgetting quotes around strings.
5fill in blank
hard

Fill 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'
A'age'
B25
Cperson
D'name'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using quotes around numbers.
Printing a wrong variable name.