-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30dictnary.py
More file actions
55 lines (40 loc) · 1.6 KB
/
30dictnary.py
File metadata and controls
55 lines (40 loc) · 1.6 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
# PYTHON DICTNARY: Ordered collection of data items. Store multiple items in a single variable.
# separated by commas and enclosed by{}
namelist = {'name':'Sourav','Age':23,'Egigible':True}
print(namelist)
# Acessing the dictnary items:
# 1. Acessing the single dictnary items:
namelist = {'name':'Sourav','Age':23,'Eligible':True}
print(namelist['name']) #method 1
print(namelist.get('Eligible')) #method 2
# 2. Acessing multiple items: We can print all the value by value().
namelist = {'name':'Sourav','Age':23,'Eligible':True}
print(namelist.values())
# 3. Acessing Keys: We can print all the keys in the dictnary using keys() method.
namelist = {'name':'Sourav','Age':23,'Eligible':True}
print(namelist.keys())
# 4. Acessing key values:
namelist = {'name':'Sourav','Age':23,'Eligible':True}
print(namelist.items())
# Dictnary methods: used for manipulation of dictnary items.
# update(): For add dictnary items.
namelist = {'name':'Sourav','Age':23,'Eligible':True}
print(namelist)
namelist.update({'salary':'5CR'})
print(namelist)
# clear()- Remove all the items from dictnary.
namelist = {'name':'Sourav','Age':23,'Eligible':True}
namelist.clear()
print(namelist)
# pop(): This removes the key value pair whose key is passed as the parameter.
namelist = {'name':'Sourav','Age':23,'Eligible':True}
namelist.pop('Age')
print(namelist)
# popitem() Remove the last key value pair.
namelist = {'name':'Sourav','Age':23,'Eligible':True}
namelist.popitem()
print(namelist)
# del : we use del to remove a dictnary.
namelist = {'name':'Sourav','Age':23,'Eligible':True}
del namelist['Age']
print(namelist)