π Python Crash Course – Chapter 6: Dictionaries – Exercise 6-8 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 6-8
Task:
Make several dictionaries, where the name of each dictionary is the
name of a pet . In each dictionary, include the kind of animal and the owner’s
name .
Store these dictionaries in a list called pets . Next, loop through your list
and as you do print everything you know about each pet
✅ Solution Code:
tommy = {"kind of animal": "dog", "Owner name": "Eminem"}
whiskers = {"kind of animal": "cat", "Owner name": "Dr. Dre"}
fluffy = {"kind of animal": "rabbit", "Owner name": "Snoop Dogg"}
spike = {"kind of animal": "fish", "Owner name": "Kanye West"}
tweety = {"kind of animal": "bird", "Owner name": "Lady Gaga"}
pets = [tommy,whiskers,fluffy,spike,tweety]
for pet in pets: # you can also use method that is in exercise 6.7
print(f'Animal Type: {pet["kind of animal"]}')
print(f'Owner Name: {pet["Owner name"]}')
print("\n")
π§ Code Explanation:
This Python code demonstrates how to use dictionaries to store information about pets and their owners, and how to organize these dictionaries in a list for iteration. Here's how it works:
-
Create dictionaries for pets:
Each pet is represented by a dictionary containing the following key-value pairs:
"kind of animal": "dog"
,"Owner name": "Eminem"
fortommy
."kind of animal": "cat"
,"Owner name": "Dr. Dre"
forwhiskers
."kind of animal": "rabbit"
,"Owner name": "Snoop Dogg"
forfluffy
."kind of animal": "fish"
,"Owner name": "Kanye West"
forspike
."kind of animal": "bird"
,"Owner name": "Lady Gaga"
fortweety
.
-
Store dictionaries in a list:
All the pet dictionaries are stored in a list called
pets
. This allows for easy iteration through all the dictionaries. -
Iterate through the list of dictionaries:
A
for
loop iterates through each dictionary in thepets
list. Each dictionary represents a pet. -
Access and display pet details:
For each dictionary, the program accesses the values associated with the keys
"kind of animal"
and"Owner name"
. These details are displayed for each pet, followed by a blank line for better readability.
This code demonstrates how to use dictionaries to store structured data and how to organize multiple dictionaries in a list for iteration and display.
π Output:
Animal Type: dog
Owner Name: Eminem
Animal Type: cat
Owner Name: Dr. Dre
Animal Type: rabbit
Owner Name: Snoop Dogg
Animal Type: fish
Owner Name: Kanye West
Animal Type: bird
Owner Name: Lady Gaga
π 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