-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprac 9.py
More file actions
45 lines (34 loc) · 1.07 KB
/
Copy pathprac 9.py
File metadata and controls
45 lines (34 loc) · 1.07 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
43
44
45
print (" ******** Madanraj SAGAR *******")
# 1. Create a dictionary
my_dict = {"name": "Alice", "age": 12, "city": "Wonderland"}
print("My dictionary:", my_dict)
# 2. Access dictionary items
print("Name:", my_dict["name"])
print("Age:", my_dict["age"])
print("City:", my_dict["city"])
# 3. Update dictionary
# Add a new key-value pair
my_dict["favorite_color"] = "purple"
print("Dictionary after adding favorite color:", my_dict)
# Update an existing value
my_dict["age"] = 13
print("Dictionary after updating age:", my_dict)
# Remove a key-value pair
del my_dict["city"]
print("Dictionary after removing city:", my_dict)
# 4. Delete the dictionary
# del my_dict
# print("Dictionary is now deleted.")
# 5. Looping through the dictionary
#looping through the keys.
print("\nLooping through keys:")
for key in my_dict:
print(key)
#looping through values.
print("\nLooping through values:")
for value in my_dict.values():
print(value)
#looping through keys and values.
print("\nLooping through keys and values:")
for key, value in my_dict.items():
print(f"{key}: {value}")