0
0
Pythonprogramming~10 mins

Dictionary use cases 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 create a dictionary with keys 'a' and 'b' and values 1 and 2.

Python
my_dict = {'a': [1], 'b': 2}
Drag options to blanks, or click blank then click option'
A1
B'1'
C3
D0
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a string '1' instead of number 1
Using wrong number like 3 or 0
2fill in blank
medium

Complete the code to access the value of key 'name' from the dictionary.

Python
person = {'name': 'Alice', 'age': 30}
print(person[1])
Drag options to blanks, or click blank then click option'
A['Name']
B['name']
C['age']
D['NAME']
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using wrong key case
Using key 'age' instead of 'name'
3fill in blank
hard

Fix the error in the code to add a new key 'city' with value 'Paris' to the dictionary.

Python
data = {'country': 'France'}
data[1] = 'Paris'
print(data)
Drag options to blanks, or click blank then click option'
A('city')
B.append('city')
C['city']
D.add('city')
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using append or add methods which are for lists or sets
Using parentheses instead of brackets
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 squaring
Using '<' instead of '>' for filtering
5fill in blank
hard

Fill all three blanks to create a dictionary from a list of tuples, including only items with value greater than 10.

Python
items = [('apple', 5), ('banana', 15), ('cherry', 20)]
result = [1]: [2] for [3] in items if [2] > 10}
Drag options to blanks, or click blank then click option'
Afruit
Bcount
Cfruit, count
Ditem
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using single variable instead of unpacking
Using wrong variable names
Not filtering values correctly