In [15]:
d = dict()
In [16]:
d
Out[16]:
{}
In [17]:
d = {}
In [18]:
d
Out[18]:
{}
In [19]:
passwd = {"guido": "superprogrammer",
         "turing": "genius",
         "bill": "monopoly"}
In [20]:
passwd
Out[20]:
{'guido': 'superprogrammer', 'turing': 'genius', 'bill': 'monopoly'}
In [21]:
passwd["guido"]
Out[21]:
'superprogrammer'
In [22]:
passwd["bill"] = "bluescreen"
In [23]:
passwd
Out[23]:
{'guido': 'superprogrammer', 'turing': 'genius', 'bill': 'bluescreen'}
In [24]:
passwd["musk"] = "mad scientist with money"
In [25]:
passwd
Out[25]:
{'guido': 'superprogrammer',
 'turing': 'genius',
 'bill': 'bluescreen',
 'musk': 'mad scientist with money'}
In [26]:
passwd["jobs"]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-26-629094a57b75> in <module>()
----> 1 passwd["jobs"]

KeyError: 'jobs'
In [29]:
for name in ["guido", "turing", "jobs", "musk"]:
    try:
        password = passwd[name]
        print(password)
    except KeyError:
        print("Error: Key {0} not found!".format(name))
superprogrammer
genius
Error: Key jobs not found!
mad scientist with money
In [13]:
foo = {}
In [14]:
foo[23] = "an int"
In [16]:
foo[3.14] = "pie slice"
In [17]:
foo["text"] = "a stuff with characters"
In [18]:
foo[(43.1566, 77.6088)] = "Rochester, NY"
In [19]:
foo[[43.1566, 77.6088]] = "Rochester, NY"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-f7eca5a6cefc> in <module>()
----> 1 foo[[43.1566, 77.6088]] = "Rochester, NY"

TypeError: unhashable type: 'list'
In [20]:
foo
Out[20]:
{23: 'an int',
 3.14: 'pie slice',
 'text': 'a stuff with characters',
 (43.1566, 77.6088): 'Rochester, NY'}