Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit82e020f

Browse files
authored
Added a simple example of creating dictionary
1 parenta444d31 commit82e020f

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+
forkeyinmy_dict:
36+
print('key in dictionary: ',key)
37+
38+
#looping through all the values in dictionary and printing them
39+
40+
forvalueinmy_dict.values():
41+
print('value in dictionary:',value)
42+
43+
#looping through all the key and values in dictionary and printing them
44+
forkey,valueinmy_dict.items():
45+
print(key,value)
46+
47+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp