Python: Unexpected behaviour with dict and a colon. ( my_dict['key']: None ) what does the colon ( : ) do? : <p>I have encountered some <em>unexpected</em> behavior with dicts in python.<br>
It might be because of annotations, but i'm not sure.<br>
Please see the snippet below:  </p>

<pre><code>&gt;&gt;&gt; d = {}  # lets create a dictionary and add something to it.
&gt;&gt;&gt; d['a'] = 'a'
&gt;&gt;&gt; d
{'a': 'a'}
&gt;&gt;&gt; d['a']
'a'
&gt;&gt;&gt; # ok all well and good, we know how dicts work, right ?
...
&gt;&gt;&gt; d['z']
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
KeyError: 'z'
&gt;&gt;&gt;
&gt;&gt;&gt; # yea no key 'z' was inserted. Lets add a colon (:) in the mix 
...
&gt;&gt;&gt; d['z']: d
&gt;&gt;&gt;
&gt;&gt;&gt; # nothing happend! weird...
...
&gt;&gt;&gt; d['z']: d['z']
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
KeyError: 'z'

&gt;&gt;&gt; # again 'z' was not added to the dict, but what did the colon ???
...
&gt;&gt;&gt; d['z']: d['z']: None
  File "&lt;stdin&gt;", line 1
    d['z']: d['z']: None
                  ^
SyntaxError: invalid syntax
&gt;&gt;&gt;
&gt;&gt;&gt; # NOW the colon gives an SyntaxError !?!
&gt;&gt;&gt; # Lets try to directly use a colon after the dict
...
&gt;&gt;&gt; {'a': 'a'}: ()
  File "&lt;stdin&gt;", line 1
SyntaxError: illegal target for annotation
&gt;&gt;&gt;
&gt;&gt;&gt; # so annotation huh ?
&gt;&gt;&gt; # is it possible to annotate on the fly ?
...
&gt;&gt;&gt; def func(x): return x
... 
&gt;&gt;&gt; d = {'f': func}  # overwrite previous dict 
&gt;&gt;&gt; d['f']: callable
&gt;&gt;&gt; d['f'].__annotations__
{}
&gt;&gt;&gt; # doesn't look like it was taken over
... # lets check if d['f'] returns the callable function: func
...
&gt;&gt;&gt;
&gt;&gt;&gt; def func(x: str) -&gt; callable: return x
... 
&gt;&gt;&gt; d = {'f': func}
&gt;&gt;&gt; d['f'].__annotations__  # lets check for annotations !!
{'x': &lt;class 'str'&gt;, 'return': &lt;built-in function callable&gt;}
&gt;&gt;&gt;
&gt;&gt;&gt; # I'm confused, what does the colon do?
...
&gt;&gt;&gt;
</code></pre>

<p>I'm curious why the dict syntax doesn't always react on the given colon (:).<br>
Also the annotations doesn't seem to be the case (or my conclusion is wrong)</p>

<p>So dear readers, what is the purpose of the colon used in the context described above?</p>
