π Python Crash Course – Chapter 6: Dictionaries – Exercise 6-11 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 6-11
Task:
Make a dictionary called cities . Use the names of three cities as
keys in your dictionary .
Create a dictionary of information about each city and
include the country that the city is in, its approximate population, and one fact
about that city .
The keys for each city’s dictionary should be something like
country, population, and fact .
Print the name of each city and all of the infor
mation you have stored about it
✅ Solution Code:
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."
}
}
for city,info in cities.items():
print(f"About {city}")
for key,value in info.items():
print(f"{key} : {value}")
π§ Code Explanation:
This Python code demonstrates how to use a nested dictionary to store detailed information about multiple cities and how to iterate through the dictionary to display the data. 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 the following keys:"country"
: The country where the city is located."population"
: The approximate population of the city."fact"
: A notable fact about the city.
For example:
"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."}
-
Iterate through the outer 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.
This code demonstrates how to use nested dictionaries to store hierarchical data 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.
π 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
πTrending Topics
π 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
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment