This blogs explain dictionaries in Python
Dictionary represents a group of element which are arranged in key-value pairs
First element is key while element next to it is value
Each key value pair is separated by colon :
All key-value pairs are inserted in curly braces
Syntax
dict = {key1:value,key2:value2,..,keyn:valuen}
Example of country details
country = {'name':'U.K','capital':'London','region':'europe'}
In above line, country is the name of the dictionary. The first element ‘name’ is the key while ‘U.K’ is the value. In the same way, keys ‘capital’ and ‘region’ has their values ‘London’ and ‘europe’
Above line contains 3 pairs of key and value
Use key to retrieve the value
For example, to retrieve value ‘London’ use key ‘capital’
Below is the example of dictionary which prints country code
country_code = {'Algeria':213,'Argentina':53,'Austrialia':61,'Belgium':32,'China':86,'India':91,'Italy':39}
print(country_code)
Output
{'Algeria': 213, 'Argentina': 53, 'Austrialia': 61, 'Belgium': 32, 'China': 86, 'India': 91, 'Italy': 39}
We can also use key to display value. For example to display country code for China use key ‘China’
print(country_code['China'])
Output
86