when do we use dictionary in python code example
Example 1: dictionary in python
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127
print(tel)
# OUTPUT {'jack': 4098, 'sape': 4139, 'guido': 4127}
print(tel['jack'])
# OUTPUT 4098
del tel['sape']
tel['irv'] = 4127
print(tel)
# OUTPUT {'jack': 4098, 'guido': 4127, 'irv': 4127}
print(list(tel))
# OUTPUT ['jack', 'guido', 'irv']
print(sorted(tel))
# OUTPUT ['guido', 'irv', 'jack']
print('guido' in tel)
# OUTPUT True
print('jack' not in tel)
# OUTPUT False
Example 2: dictionary in python
shapes={"square": 90, "triangle": 60}
Example 3: dictionary in python
Dict = {"name": 'Izhaan', "salary": 1234, "age": 23}
print("\nDictionary with the use of string Keys: ")
print(Dict)
Example 4: dictionary in python
#A dictionary has key-value pairs. Here 1,2,3 are the keys and Item1,Item2,Item3
#are their values respectively.
dictionaryName = { 1: "Item1", 2: "Item2", 3: "Item3"}
#retrieving value of a particular key
dictionaryName[1]
#retrieving all the keys in a dictionary
dictionaryName.keys()
#retrieving all the values in a dictionary
dictionaryName.values()