File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/python3
2
+ #dictionary in python
3
+
4
+ #creating empty dictionary
5
+ my_dict = {}
6
+
7
+ print ("Empty Dictionary: " )
8
+ print (my_dict )
9
+
10
+ #Creating a diictionary with int keys and string values then printing it
11
+
12
+ my_dict = {1 : 'Apple' , 2 : 'Orange' , 3 : 'Banana' }
13
+ print (my_dict )
14
+
15
+ #You can access the items of a dictionary by referring to its key name, inside square brackets:
16
+
17
+ orange = my_dict [2 ]
18
+ print (orange )
19
+
20
+ #Creating a diictionary with string keys and printing it
21
+ my_dict = {
22
+ "brand" : "Ford" ,
23
+ "model" : "Mustang" ,
24
+ "year" : 1964
25
+ }
26
+ print (my_dict )
27
+
28
+ #Another example of accessing items from a dictionary by its keys
29
+
30
+ model = my_dict ["model" ]
31
+ print (my_dict )
32
+
33
+ #looping through all the keys in dictionary and printing them
34
+
35
+ for key in my_dict :
36
+ print ('key in dictionary: ' ,key )
37
+
38
+ #looping through all the values in dictionary and printing them
39
+
40
+ for value in my_dict .values ():
41
+ print ('value in dictionary:' ,value )
42
+
43
+ #looping through all the key and values in dictionary and printing them
44
+ for key , value in my_dict .items ():
45
+ print (key , value )
46
+
47
+
You can’t perform that action at this time.
0 commit comments