Module 20: Dictionary Practice

Let's organize data with Python's dictionaries!

Anatomy of a Dictionary

Dictionaries store data in key-value pairs. Click each highlighted part of this example dictionary to learn its role.

student = {'name': 'Alice', 'age': 25}

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:

config = {'theme': 'dark', 'version': 1.2, 'active': True}

Your task: Write the Python code to get the value associated with the key 'version'.

Result will appear here...

Adding & Updating Pairs

You can add new key-value pairs or change existing values using the `dictionary[key] = value` syntax. Follow the steps below.

# Initial Dictionary State will be shown here by JS

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'.

Output/State check appears here...

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.

Output/State check appears here...

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).

Output/State check appears here...

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!