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

 

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

  1. Create dictionaries for pets:

    Each pet is represented by a dictionary containing the following key-value pairs:

    • "kind of animal": "dog", "Owner name": "Eminem" for tommy.
    • "kind of animal": "cat", "Owner name": "Dr. Dre" for whiskers.
    • "kind of animal": "rabbit", "Owner name": "Snoop Dogg" for fluffy.
    • "kind of animal": "fish", "Owner name": "Kanye West" for spike.
    • "kind of animal": "bird", "Owner name": "Lady Gaga" for tweety.
  2. 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.

  3. Iterate through the list of dictionaries:

    A for loop iterates through each dictionary in the pets list. Each dictionary represents a pet.

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


🔗 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