-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_dictionary.py
More file actions
42 lines (27 loc) · 1.12 KB
/
8_dictionary.py
File metadata and controls
42 lines (27 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Creating and accessing elements in a dictionary
# Create a dictionary of student names and their corresponding ages
student_ages = {"Alice": 20, "Bob": 22, "Charlie": 21}
# Accessing elements in the dictionary
print("Alice's age:", student_ages["Alice"])
print("Bob's age:", student_ages["Bob"])
print("Charlie's age:", student_ages["Charlie"])
# Iterating over key-value pairs in a dictionary
# Create a dictionary of car brands and their corresponding prices
car_prices = {"Toyota": 20000, "Honda": 22000, "Ford": 25000}
# Iterating over key-value pairs in the dictionary
print("Car Prices:")
for brand, price in car_prices.items():
print(f"{brand}: ${price}")
# Modifying and adding elements to a dictionary
# Create an empty dictionary
phone_numbers = {}
# Add phone numbers to the dictionary
phone_numbers["Alice"] = "555-1234"
phone_numbers["Bob"] = "555-5678"
phone_numbers["Charlie"] = "555-9012"
# Modify Alice's phone number
phone_numbers["Alice"] = "555-4321"
# Print all phone numbers in the dictionary
print("Phone Numbers:")
for name, number in phone_numbers.items():
print(f"{name}: {number}")