Anatomy of a Dictionary
Dictionaries store data in key-value pairs. Click each highlighted part of this example dictionary to learn its role.
Identify all 9 parts!
Accessing Dictionary Values
To get a value, use the dictionary name followed by the key in square brackets `[]`. Try it below!
Given the dictionary:
Your task: Write the Python code to get the value associated with the key 'version'
.
Adding & Updating Pairs
You can add new key-value pairs or change existing values using the `dictionary[key] = value` syntax. Follow the steps below.
Step 1: Add a Pair
Add a new key 'city'
with the string value 'New York'
to the dictionary above.
Dictionary Missions!
Combine your dictionary skills! Complete these tasks using the editor in each mission.
Mission 1: Create a Car Dictionary
Create a dictionary named `my_car`. It should have a key 'make'
with value 'Tesla'
and a key 'model'
with value 'Model 3'
.
Mission 2: Print the Name
Assume a dictionary `user = {'id': 101, 'name': 'Bob', 'level': 5}` already exists. Write one line of code to `print()` the value of the 'name'
key.
Mission 3: Add and Update Settings
Assume `settings = {'volume': 80, 'brightness': 65}` exists. Write code to first add a new key 'mode'
with the value 'eco'
, and then update the 'volume'
to 90
. (Two lines of code needed).
Module 20 Practice Complete!
Well done! You've practiced creating, accessing, and modifying dictionaries – essential tools for organizing data.
Ready for more? Try the Advanced Practice for Module 20!