Last active
July 26, 2021 04:19
-
-
Save sibil/d5ce8f5c9cdd6a9723a2322e8107be08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dictionaries are made of key-value pairs | |
# In the example below, names are the keys and values are their ages. | |
students = {'bilal' : 15, 'ibraheem' : 19, 'luqman' : 15} | |
print(students) | |
#add a new key/value pair to the Dictionary | |
students['imad'] = 18 | |
#Change the value of a key | |
students['bilal'] = 19 | |
print(students) | |
#Print all keys | |
for name in students.keys(): | |
print("Name: " + name) | |
#Print all values | |
for age in students.values(): | |
print("age:" + str(age)) | |
#Print all keys and values | |
for name,age in students.items(): | |
print(name + " is " + str(age) + " years old") | |
#Dictionary keys have to be immutable | |
# search in keys. | |
if 'bilal' in students.keys(): | |
print("Key(bilal) exists") | |
# search in values. | |
if 19 in students.values(): | |
print("Value 19 exists") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment