-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict.py
More file actions
143 lines (120 loc) · 3.85 KB
/
dict.py
File metadata and controls
143 lines (120 loc) · 3.85 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# dictionary_methods_demo.py
# Demonstration of common Python dictionary methods and operations
# This for revision
# Must remember all the functions
# -----------------------------
# Dictionary creation
# -----------------------------
student = {
"name": "Rudra",
"age": 20,
"course": "Cyber Security"
}
print("Initial dictionary:")
print(student)
# -----------------------------
# len() → Number of key-value pairs
# -----------------------------
print("\nlen(student):", len(student))
# -----------------------------
# Access value by key
# -----------------------------
print("\nstudent['name']:", student["name"])
# -----------------------------
# .get() → Safe access (no KeyError)
# -----------------------------
print("\nstudent.get('age'):", student.get("age"))
print("student.get('grade'):", student.get("grade")) # None
print("student.get('grade', 'Not found'):", student.get("grade", "Not found"))
# -----------------------------
# Add / Update item
# -----------------------------
student["grade"] = "A"
student["age"] = 21
print("\nAfter adding/updating keys:")
print(student)
# -----------------------------
# in operator → Check if key exists
# -----------------------------
print("\nIs 'name' in student?", "name" in student)
print("Is 'email' in student?", "email" in student)
# -----------------------------
# .keys() → Get all keys
# -----------------------------
print("\nKeys:", student.keys())
# -----------------------------
# .values() → Get all values
# -----------------------------
print("Values:", student.values())
# -----------------------------
# .items() → Get key-value pairs
# -----------------------------
print("Items:", student.items())
# -----------------------------
# for loop → Iterate dictionary
# -----------------------------
print("\nIterating over dictionary:")
for key, value in student.items():
print(f"{key} → {value}")
# -----------------------------
# .update() → Merge another dictionary
# -----------------------------
extra_info = {"email": "rudra@example.com", "city": "Kolkata"}
student.update(extra_info)
print("\nAfter update():")
print(student)
# -----------------------------
# .pop() → Remove & return value by key
# -----------------------------
removed_age = student.pop("age")
print("\nRemoved age:", removed_age)
print("After pop('age'):", student)
# -----------------------------
# .popitem() → Remove & return last inserted item
# -----------------------------
last_item = student.popitem()
print("\nPopitem():", last_item)
print("After popitem():", student)
# -----------------------------
# del → Delete key
# -----------------------------
del student["city"]
print("\nAfter del student['city']:", student)
# -----------------------------
# .setdefault() → Get or set default value
# -----------------------------
student.setdefault("phone", "Not provided")
student.setdefault("name", "Unknown") # won't overwrite
print("\nAfter setdefault():")
print(student)
# -----------------------------
# .copy() → Shallow copy
# -----------------------------
student_copy = student.copy()
print("\nCopied dictionary:")
print(student_copy)
# -----------------------------
# .clear() → Remove all items
# -----------------------------
temp = student.copy()
temp.clear()
print("\nAfter clear():", temp)
# -----------------------------
# Dictionary comprehension
# -----------------------------
squares = {x: x**2 for x in range(1, 6)}
print("\nDictionary comprehension (squares):")
print(squares)
# -----------------------------
# Nested dictionary
# -----------------------------
users = {
"user1": {"role": "admin", "active": True},
"user2": {"role": "guest", "active": False}
}
print("\nNested dictionary access:")
print("user1 role:", users["user1"]["role"])
# -----------------------------
# End of demo
# -----------------------------
print("\nAll dictionary methods demonstrated successfully!")