-
Notifications
You must be signed in to change notification settings - Fork 0
/
34-Dictionary Functions and Methods.py
68 lines (57 loc) · 1.23 KB
/
34-Dictionary Functions and Methods.py
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
"""
For get element from dictionary:
1 - get()
2 - keys()
3 - values()
4 - items()
"""
dic = {
'name': 'Fakhar Zaman',
'class': 'BS Information Technology',
'cgpa': 3.43
}
print(dic, type(dic))
print("\n")
# get()
result = dic.get('name')
print(result)
print("\n")
# keys()
for n in dic.keys():
print("Key: ", n)
print("\n")
# values()
for n in dic.values():
print("Value: ", n)
print("\n")
# items()
for n, m in dic.items():
print(n, m) # n for keys, m for values
print("--------------------------------------------------------------\n")
"""
For delete element from dictionary:
1 - del
2 - pop()
"""
# del
del dic['cgpa']
print(dic)
# pop()
a = dic.pop('class')
print(a)
print(dic)
print("--------------------------------------------------------------\n")
"""
Other important functions and methods of dictionary:
1 - dict()
2 - update()
3 - clear()
"""
# dict()
d = dict(name='Hammad Asif', level='BS Information Technology', cgpa=2.70, friend="Fakher Zaman")
print(d)
d['description'] = "Both are best students of the year"
d.update(name='Fakher Zaman', cgpa=3.43, friend="Hammad Asif")
print(d)
d.clear()
print(d)