File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Dictionary is collection of key-value pairs. Dictionary is ordered(from
2
+ # python 3.6 onwards), changeable and does not contain any duplicates
3
+
4
+ capitals = {'US' :'Washington' ,
5
+ 'UK' :'London' ,
6
+ 'Germany' :'Berlin' ,
7
+ 'South Korea' :'Seoul' }
8
+ print (capitals )
9
+
10
+ # To print value we will make use of specific key
11
+ print (capitals ['US' ])
12
+ # print(capitals['France']) # This will generate error as this not defined in dictionary
13
+ # So to overcome this we will make use of .get
14
+ print (capitals .get ('France' )) #This will give output 'NONE'
15
+
16
+ # To add a new key to dictionary we will make use of update
17
+ capitals .update ({'France' :'Paris' })
18
+ print ("Dictionary was updated :" ,end = "" )
19
+ print (capitals )
20
+
21
+ # To change an existing value of a key we make use of update
22
+ capitals .update ({'US' :'Chicago' })
23
+ print ("Value for Washington was updated" ,end = "" )
24
+ print (capitals )
25
+
26
+ # To remove a particular key-value pair we will make use of pop
27
+ capitals .pop ('US' )
28
+ print ("Value popped is US: " ,end = "" )
29
+ print (capitals )
30
+
31
+ # To clear a list make use of clear
32
+ capitals .clear ()
33
+ print ("Dictionary Cleared: " ,end = "" )
34
+ print (capitals )
You can’t perform that action at this time.
0 commit comments