""" In a dictionary, the first value of an element is the *key* and
the other one is the *value* """
new_dict = {
'name': 'Alex',
'age': 21
}
""" You can also use different data type for key in the same dictionary """
new_dict = {
'name': 'Alex',
1: 21,
2: False
}
population = {"Shanghai":17.8, "Istanbul":13.3, "Karachi":13.0, "Mumbai":12.5}
#Consider the following when creating a dictionary
# 1. Must be inside {}
# 2. Have two parts in an item
# Key - e.g. Istanbul and Value - e.g. 13.3
# 3. There must be a colon ":" between the key and the value
# 4. Item must be seperated by commas ","
# 3. Any immutable type can be used as keys (not only integers like in lists)
keys = ['a', 'b', 'c']
values = [1, 2, 3]
def create_dictionary(keys, values):
result = {} # empty dictionary
for key, value in zip(keys, values):
result[key] = value
return result
#create an empty dictionary
my_dictionary = {}
print(my_dictionary)
#to check the data type use the type() function
print(type(my_dictionary))
#output
#{}
#<class 'dict'>