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

 

πŸ“˜ Python Crash Course – Chapter 6: Dictionaries – Exercise 6-5 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


πŸ“ Exercise 6-5

Task:
Make a dictionary containing three major rivers and the country each river runs through . One key-value pair might be 'nile': 'egypt' .
• Use a loop to print a sentence about each river, such as The Nile runs through Egypt .
• Use a loop to print the name of each river included in the dictionary .
• Use a loop to print the name of each country included in the dictionary

✅ Solution Code:


rivers = {
    'nile': 'egypt',
    'amazon': 'brazil',
    'yangtze': 'china',
}

for name , country in rivers.items():
    print(f"{name} is located in {country}")


print("-----------------------------------------------")

for name in rivers.keys():
    print(name)


print("-----------------------------------------------")

for country in rivers.values():
    print(country)

🧠 Code Explanation:

This Python code demonstrates how to use a dictionary to store information about rivers and the countries they run through. It also shows how to iterate through the dictionary to display its contents in different ways. Here's how it works:

  1. Create a dictionary:

    The variable rivers is assigned a dictionary where the keys are river names, and the values are the countries they run through. For example:

    • 'nile': 'egypt'
    • 'amazon': 'brazil'
    • 'yangtze': 'china'
  2. Iterate through key-value pairs:

    The items() method is used to retrieve all key-value pairs from the dictionary. A for loop iterates through these pairs, where name represents the river name (key) and country represents the country (value). A sentence is displayed for each pair, indicating which country the river is located in.

  3. Iterate through keys:

    The keys() method is used to retrieve all keys (river names) from the dictionary. A for loop iterates through these keys, displaying each river name.

  4. Iterate through values:

    The values() method is used to retrieve all values (countries) from the dictionary. A for loop iterates through these values, displaying each country.

This code demonstrates how to use dictionaries to store related data and how to iterate through keys, values, and key-value pairs to display the information in different formats.

πŸ” Output:


nile is located in egypt
amazon is located in brazil
yangtze is located in china
-----------------------------------------------   
nile
amazon
yangtze
-----------------------------------------------   
egypt
brazil
china

πŸ“š 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

“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