forked from powerexploit/Awesome-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.py
47 lines (31 loc) · 1.06 KB
/
dictionary.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
#!/usr/bin/python3
#dictionary in python
#creating empty dictionary
my_dict = {}
print("Empty Dictionary: ")
print(my_dict)
#Creating a diictionary with int keys and string values then printing it
my_dict = {1: 'Apple', 2: 'Orange', 3: 'Banana'}
print(my_dict)
#You can access the items of a dictionary by referring to its key name, inside square brackets:
orange = my_dict[2]
print(orange)
#Creating a diictionary with string keys and printing it
my_dict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(my_dict)
#Another example of accessing items from a dictionary by its keys
model = my_dict["model"]
print(my_dict)
#looping through all the keys in dictionary and printing them
for key in my_dict:
print('key in dictionary: ',key)
#looping through all the values in dictionary and printing them
for value in my_dict.values():
print('value in dictionary:',value)
#looping through all the key and values in dictionary and printing them
for key, value in my_dict.items():
print(key, value)