Skip to content

Commit 82e020f

Browse files
authored
Added a simple example of creating dictionary
1 parent a444d31 commit 82e020f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

dictionary.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+

0 commit comments

Comments
 (0)