Creating a dictionary is as simple as
placing items inside curly braces {} separated by comma.
An item has a key and the corresponding value expressed as a pair, key, and value. While values can be of any data type and can
repeat, keys must be of immutable type (string, number or tuple with immutable
elements) and must be unique. Dictionary are mutable. We can add new items or change the value of existing
items using assignment operator.
The best way to learn new things is to take a
practical approach of the things you want to learn. Here is a fragment
of code that demonstrates
the use of the dictionary statement in Python:
Æ’ python command
family_dictionary = {'The_Smiths': '
- have a boy' , 'The_Nicholsons': ' - have a child as well'}
print(family_dictionary)
print(family_dictionary['The_Smiths'])
for Ralph, Robert in
family_dictionary.items():
print(Ralph + Robert)
√ output
{'The_Smiths': ' - have a boy',
'The_Nicholsons': ' - have a child as well'}
- have a boy
The_Smiths - have a boy
The_Nicholsons - have a child as well
Sign up here with your email
ConversionConversion EmoticonEmoticon