π 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:
-
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'
-
Iterate through key-value pairs:
The
items()
method is used to retrieve all key-value pairs from the dictionary. Afor
loop iterates through these pairs, wherename
represents the river name (key) andcountry
represents the country (value). A sentence is displayed for each pair, indicating which country the river is located in. -
Iterate through keys:
The
keys()
method is used to retrieve all keys (river names) from the dictionary. Afor
loop iterates through these keys, displaying each river name. -
Iterate through values:
The
values()
method is used to retrieve all values (countries) from the dictionary. Afor
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:
- ➤ 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