Practice and reinforce the concepts from Lesson 7
Total Time: 45-60 minutes
:bulb: :bulb: Remember to run each code cell in order as you complete the exercises!
Question One: Basic Dictionary Operations
⏱️ Time: 15-20 minutes
Part One: Create a dictionary
Create a dictionary named
students_id
Print the dictionary to verify it's created correctly
:information_source: Info :memo: You may copy the code below to create the dictionary:
python
students_id = {111: "Harry Potter", 112: "Draco Malfoy", 113: "Hermione Granger"}
Expected output:
output{111: 'Harry Potter', 112: 'Draco Malfoy', 113: 'Hermione Granger'}
Part 2: Access a specific value
- Access the value of
students_id[111]
in the dictionary- Print the result
Expected output:
outputHarry Potter
Part 3: Access all dictionary components
- Access and print all items of the dictionary
- Access and print all keys of the dictionary
- Access and print all values of the dictionary
Expected output:
outputdict_items([(111, 'Harry Potter'), (112, 'Draco Malfoy'), ]($2)]) dict_keys([111, 112, 113]) dict_values(['Harry Potter', 'Draco Malfoy', 'Hermione Granger'])
Part 4: Loop through dictionary items
- Use a for loop to iterate through all items in the dictionary
- Print each item
Expected output:
output(111, 'Harry Potter') (112, 'Draco Malfoy') (113, 'Hermione Granger')
Part 5: Add a new entry
- Add a new student called "Ron Weasley" with id 114 to the dictionary
- Print the updated dictionary
Expected output:
output{111: 'Harry Potter', 112: 'Draco Malfoy', 113: 'Hermione Granger', 114: 'Ron Weasley'}
Part 6: Modify an existing entry
- Change the student name "Hermione Granger" to "Terry Boot"
- Print the updated dictionary
Expected output:
output{111: 'Harry Potter', 112: 'Draco Malfoy', 113: 'Terry Boot', 114: 'Ron Weasley'}
Part 7: Delete an entry
- Delete the student named "Draco Malfoy" from the dictionary
- Print the updated dictionary tip :bulb: To delete by value, you'll need to find the key first!
Expected output:
{111: 'Harry Potter', 113: 'Terry Boot', 114: 'Ron Weasley'}
⏱️ Time: 10-15 minutes
players | name | country |
---|---|---|
player1 | Viktor Axelsen | Denmark |
player2 | Lee Zii Jia | Malaysia |
player3 | Loh Kean Yew | Singapore |
player4 | Jonatan Christie | Indonesia |
Expected output:
{'name': ['Viktor Axelsen', 'Lee Zii Jia', 'Loh Kean Yew', 'Jonatan Christie'], 'country': ['Denmark', 'Malaysia', 'Singapore', 'Indonesia']}
⏱️ Time: 15-20 minutes
Create a nested dictionary named players
(Dictionaries in Dictionary) using data from the Question 2 table
:information_source: Info
:memo: Run the first code cell under Advanced Level section in the Colab notebook
Access and print all player1 values from the dictionary
Access and print player2's name from the dictionary
Access and print player3's country from the dictionary
Add a new player to the dictionary:
Example player data:
players name country player5 Kento Momota Japan Then print the updated dictionary
Delete player4 from the dictionary and print the result to verify the update
:bulb: Helpful Resources
tip Need help with nested dictionaries?
Common Issues:
:warning: Warning Before submitting:
- :white_check_mark: Complete all questions (Question 1, Question 2, and Advanced Level)
- :white_check_mark: Test your code to ensure all outputs match the expected results
- :white_check_mark: Save your Colab notebook
- :white_check_mark: Review your answers one final time
:information_source: Ready to submit? :link: Submit your exercise here
Your responses will help track your progress in the course.