What is the syntax to use Dictionary?

#to declare a dictionary-type variable
<dictionary name> = {<key1>:<value1>, <key12:<value2>, <key3>:<value3>...etc}

#to access/retrieve values from a dictionary
<dictionary name>[<key>]

#to print all keys
<dictionary name>.keys()

#to print all values
<dictionary name>.values()
#example
>>> passwords = {'John':1123421,'Shirley':'socks', 'Galen':4234.123}
>>> emptyD = {}

>>> passwords['Galen']
4234.123

>>> passwords.keys()
dict_keys(['John', 'Shirley', 'Galen'])

>>> passwords.values()
dict_values([1123421, 'socks', 4234.123])
Advertisements