Dictionary is also a collection of unordered data structure. Dictionary can be created using key and value pairs inside pair of curly braces. Each key and value pair is separated by colon(:) and each item of dictionary is separated with comma (,).
Keys of dictionary must be unique but values may not be. Dictionary can contain any type of values but keys must be immutable type like strings, numbers or tuples.
#Example code to create and print a dictionary
mydict ={
“brand”: “Renault”,
“model”: “Climber”,
“year”: 2019
}
print(mydict)
Accessing Values in Dictionary
Elements of the dictionary can be accessed using their keys inside square brackets. The in-built get() method can also be used for this.
#Example code
dict1 = {‘Name’: ‘Shiva’, ‘Age’: 18, ‘Class’: ‘First’}
print (“dict1[‘Name’]: “, dict1[‘Name’])
print (“dict1[‘Age’]: “, dict1[‘Age’])
print(dict1.get(‘Name’))