Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Added a simple example of creating dictionary #17

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions dictionary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)