Skip to content

Commit 87ab84d

Browse files
authored
Merge pull request powerexploit#17 from fsjoyti/master
Added a simple example of creating dictionary
2 parents 7a7a72e + 82e020f commit 87ab84d

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy