0
0
Pythonprogramming~10 mins

Accessing values using keys 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 access the value of the key 'name' in the dictionary.

Python
person = {'name': 'Alice', 'age': 30}
print(person[1])
Drag options to blanks, or click blank then click option'
A['location']
B['age']
C['name']
D['gender']
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses instead of square brackets.
Using a key that does not exist in the dictionary.
2fill in blank
medium

Complete the code to print the value of the key 'color' from the dictionary.

Python
car = {'brand': 'Toyota', 'color': 'red', 'year': 2020}
print(car[1])
Drag options to blanks, or click blank then click option'
A['model']
B['color']
C['year']
D['brand']
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a key that is not in the dictionary.
Forgetting to put the key name in quotes.
3fill in blank
hard

Fix the error in the code to correctly access the value of the key 'score'.

Python
results = {'score': 85, 'grade': 'B'}
print(results[1])
Drag options to blanks, or click blank then click option'
Ascore
B{score}
C(score)
D['score']
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets.
Not putting the key name in quotes.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length.

Python
words = ['apple', 'banana', 'cherry']
lengths = {word[1] for word in words if len(word) [2] 5}
Drag options to blanks, or click blank then click option'
A: len(word)
B>
C<
D==
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '=' instead of ':' in the dictionary comprehension.
Using the wrong comparison operator in the if condition.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase keys and their values, filtering values greater than 10.

Python
data = {'a': 5, 'b': 15, 'c': 20}
result = [1]: [2] for k, v in data.items() if v [3] 10}
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().
Using '<' or '==' instead of '>' in the condition.