Skip to content

Instantly share code, notes, and snippets.

@sibil
Last active July 26, 2021 04:19
Show Gist options
  • Save sibil/d5ce8f5c9cdd6a9723a2322e8107be08 to your computer and use it in GitHub Desktop.
Save sibil/d5ce8f5c9cdd6a9723a2322e8107be08 to your computer and use it in GitHub Desktop.
# 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