📘 Python Crash Course – Chapter 6: Dictionaries – Exercise 6-12 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
📝 Exercise 6-12
Task:
We’re now working with examples that are complex enough
that they can be extended in any number of ways . Use one of the example pro
grams from this chapter, and extend it by adding new keys and values, changing the context of the program or improving the formatting of the output .
✅ Solution Code:
# we use exercise 6.11 , we add new cities
cities = {
"paris": {
"country": "france",
"population": 2148000,
"fact": "The Eiffel Tower is located in Paris."
},
"new york city": {
"country": "usa",
"population": 8405837,
"fact": "New York City is home to the United Nations Headquarters."
},
"tokyo": {
"country": "japan",
"population": 13929286,
"fact": "Tokyo is home to the famous Tsukiji Fish Market."
}
}
cities["seoul"] = {
"country": "south korea",
"population": 10231815,
"fact": "Seoul is home to the Gyeongbokgung Palace."
}
# Add Beijing
cities["beijing"] = {
"country": "china",
"population": 21540000,
"fact": "Beijing is home to the Great Wall of China."
}
# Add Sydney
cities["sydney"] = {
"country": "australia",
"population": 5300000,
"fact": "Sydney is home to the iconic Sydney Opera House."
}
for city,info in cities.items():
print(f"About {city}")
for key,value in info.items():
print(f"{key} : {value}")
print("------------------------------------")
🧠 Code Explanation:
This Python code builds upon a previous example (Exercise 6.11) by adding new cities to a nested dictionary and displaying detailed information about each city. Here's how it works:
-
Create a nested dictionary:
The variable
cities
is assigned a dictionary where the keys are city names, and the values are dictionaries containing detailed information about each city. Each inner dictionary includes:"country"
: The country where the city is located."population"
: The approximate population of the city."fact"
: A notable fact about the city.
-
Add new cities:
Three new cities are added to the dictionary:
"seoul"
: Includes details about South Korea's capital city."beijing"
: Includes details about China's capital city."sydney"
: Includes details about Australia's iconic city.
-
Iterate through the dictionary:
The
items()
method is used to retrieve all key-value pairs from thecities
dictionary. Afor
loop iterates through these pairs, wherecity
represents the city name (key) andinfo
represents the inner dictionary (value). -
Iterate through the inner dictionary:
For each city, a nested
for
loop iterates through the key-value pairs in the inner dictionary. The keys represent the type of information (e.g., "country", "population", "fact"), and the values represent the corresponding details. -
Display results:
For each city, its name and all its details (country, population, and fact) are displayed in a structured format, followed by a separator line for better readability.
This code demonstrates how to extend a nested dictionary by adding new entries and how to iterate through multiple levels of a dictionary to display the information in an organized manner.
🔍 Output:
About paris
country : france
population : 2148000
fact : The Eiffel Tower is located in Paris.
------------------------------------
About new york city
country : usa
population : 8405837
fact : New York City is home to the United Nations Headquarters.
------------------------------------
About tokyo
country : japan
population : 13929286
fact : Tokyo is home to the famous Tsukiji Fish Market.
------------------------------------
About seoul
country : south korea
population : 10231815
fact : Seoul is home to the Gyeongbokgung Palace.
------------------------------------
About beijing
country : china
population : 21540000
fact : Beijing is home to the Great Wall of China.
------------------------------------
About sydney
country : australia
population : 5300000
fact : Sydney is home to the iconic Sydney Opera House.
------------------------------------
📚 Related Exercises from Chapter 6:
- ➤ Exercise 6-1 | Person
- ➤ Exercise 6-2 | Favorite Numbers
- ➤ Exercise 6-3 | Glossary
- ➤ Exercise 6-4 | Glossary 2
- ➤ Exercise 6-5 | Rivers
- ➤ Exercise 6-6 | Polling
- ➤ Exercise 6-7 | People
- ➤ Exercise 6-8 | Pets
- ➤ Exercise 6-9 | Favorite Places
- ➤ Exercise 6-10 | Favorite Numbers
- ➤ Exercise 6-11 | Cities
- ➤ Exercise 6-12 | Extensions
🔗 Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”
— Only Python
📌 Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips