π 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:
-
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"
-
Access dictionary values (Method 1):
Individual dictionary values are accessed using their keys (e.g.,
fav_person["First name"]
) and displayed one by one. -
Iterate through items (Method 2):
The
items()
method is used to retrieve all key-value pairs from the dictionary. Afor
loop iterates through these pairs, and both the key and value are displayed for each pair. -
Iterate through keys (Method 3):
The
keys()
method is used to retrieve all keys from the dictionary. Afor
loop iterates through these keys, and the corresponding value for each key is accessed usingfav_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:
- ➤ 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