0
0
Pythonprogramming~10 mins

Why dictionaries are used in Python - Test Your Understanding

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

Complete the code to create a dictionary with keys as names and values as ages.

Python
ages = {'Alice': 25, 'Bob': 30, 'Charlie': [1]
Drag options to blanks, or click blank then click option'
A35
B'35'
C[35]
D(35)
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Putting the number 35 in quotes, making it a string instead of an integer.
2fill in blank
medium

Complete the code to access the value for key 'name' in the dictionary.

Python
person = {'name': 'John', 'age': 22}
print(person[1])
Drag options to blanks, or click blank then click option'
A['age']
B['name']
C['john']
D['Name']
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a wrong key name or wrong case, which causes a KeyError.
3fill in blank
hard

Fix the error in the code to add a new key-value pair to the dictionary.

Python
data = {'x': 10, 'y': 20}
data[1] = 30
Drag options to blanks, or click blank then click option'
A['z']
Bappend('z')
Cadd('z')
D.z
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Trying to use list methods like append on a dictionary.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps numbers to their squares for numbers greater than 2.

Python
squares = {x: x[1]2 for x in range(1, 6) if x [2] 2 }
Drag options to blanks, or click blank then click option'
A**
B+
C>
D<
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' instead of '**' for power, or using '<' instead of '>' for the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values only if the value is positive.

Python
result = [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk.lower()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using k.lower() instead of k.upper(), or using '<' instead of '>' for filtering.