Rather than storing single objects like lists and sets do, dictionaries store pairs of elements: keys and values.
elements = {'hydrogen': 1, 'helium': 2, 'carbon': 6}
>>> print(element['carbon'])
6
>>> elements['lithium'] = 3 >>> print(elements['lithium']) 3
populations = {'Shanghai':17.8, 'Istanbul':13.3, 'Karachi':13.0, 'Mumbai':12.5}
if 'mithril' in elements:
print("That's a real element!")
else:
print("There's no such element")
>>> elements.get('dilithium')
>>> elements['dilithium']
Traceback (most recent call last):
File "", line 1, in
elements['dilithium']
KeyError: 'dilithium'
>>> elements.get('kryptonite', 'There\'s no such element!')
"There's no such element!"
colors = set(['Pathalo Blue', 'Indian Yellow', 'Sap Green']) for color in colors: print(color)
>>> elements = {'hydrogen': {'number':1, 'weight':1.00794, 'symbol':'H'},
'helium':{'number':2, 'weight':4.002602, 'symbol':'He'}}
>>> print(elements['helium'])
{'symbol': 'He', 'number': 2, 'weight': 4.002602}
>>> print(elements.get('unobtainium', 'There\'s no such element!'))
There's no such element!
>>> print(elements['helium']['weight'])
4.002602