Exercise 6-11 Solution Python Crash Course Chapter 6: Dictionaries

 

📘 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:

  1. 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."}
  2. Iterate through the outer dictionary:

    The items() method is used to retrieve all key-value pairs from the cities dictionary. A for loop iterates through these pairs, where city represents the city name (key) and info represents the inner dictionary (value).

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

  4. 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:


🔗 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

Previous Post Next Post

Contact Form