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

 

📘 Python Crash Course – Chapter 6: Dictionaries – Exercise 6-1 Solution

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


📝 Exercise 6-1

Task:
Use a dictionary to store information about a person you know .
Store their first name, last name, age, and the city in which they live .
You should have keys such as first_name, last_name, age, and city . Print each piece of information stored in your dictionary

✅ Solution Code:


fav_person = {
    "First name" : "Tony",
    "Last Name" : "Stark",
    "Age":36,
    "City":"New york"
}


# Method 1
print(f'First Name : {fav_person["First name"]}')
print(f'Last Name : {fav_person["Last Name"]}')
print(f'Age : {fav_person["Age"]}')
print(f'City : {fav_person["City"]}')


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

# Method 2
for i,j in fav_person.items():
    print(f'{i} : {j}')

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

# Method 3
for i in fav_person.keys():
    print(f'{i} : {fav_person[i]}')

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

🧠 Code Explanation:

This Python code demonstrates how to use a dictionary to store and retrieve information about a person. It also shows multiple methods to access and display the dictionary's contents. Here's how it works:

  1. Create a dictionary:

    The variable fav_person is assigned a dictionary containing information about a person. The dictionary includes the following key-value pairs:

    • "First name": "Tony"
    • "Last Name": "Stark"
    • "Age": 36
    • "City": "New york"
  2. Access dictionary values (Method 1):

    Individual dictionary values are accessed using their keys (e.g., fav_person["First name"]) and displayed one by one.

  3. Iterate through items (Method 2):

    The items() method is used to retrieve all key-value pairs from the dictionary. A for loop iterates through these pairs, and both the key and value are displayed for each pair.

  4. Iterate through keys (Method 3):

    The keys() method is used to retrieve all keys from the dictionary. A for loop iterates through these keys, and the corresponding value for each key is accessed using fav_person[key].

This code demonstrates how to create and work with dictionaries in Python, including accessing values directly, iterating through key-value pairs, and iterating through keys.

🔍 Output:


First Name : Tony
Last Name : Stark
Age : 36
City : New york
------------------------------------------------  
First name : Tony
Last Name : Stark
Age : 36
City : New york
------------------------------------------------  
First name : Tony
Last Name : Stark
Age : 36
City : New york
------------------------------------------------  

📚 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