dictionaries/script.rst
changeset 300 a130a1f494c3
parent 261 c7f0069d698a
child 319 e8c02b3c51ac
equal deleted inserted replaced
299:8d61625510b6 300:a130a1f494c3
    32 
    32 
    33 Before we can proceed, start your IPython interpreter with the
    33 Before we can proceed, start your IPython interpreter with the
    34 ``-pylab`` option.
    34 ``-pylab`` option.
    35 
    35 
    36 {{{ start ipython interpreter by issuing command ipython -pylab }}}
    36 {{{ start ipython interpreter by issuing command ipython -pylab }}}
       
    37 
       
    38 {{{ switch to next slide, Creating dictionary }}}
    37 
    39 
    38 Let us start by creating an empty dictionary, type the following in
    40 Let us start by creating an empty dictionary, type the following in
    39 your IPython interpreter.
    41 your IPython interpreter.
    40 ::
    42 ::
    41 
    43 
    71     extensions
    73     extensions
    72 
    74 
    73 in the interpreter to see the content of the dictionary. Notice that
    75 in the interpreter to see the content of the dictionary. Notice that
    74 in dictionaries the order cannot be predicted and you can see that the
    76 in dictionaries the order cannot be predicted and you can see that the
    75 values are not in the order that we entered in.
    77 values are not in the order that we entered in.
       
    78 
       
    79 {{{ switch to next slide, accessing elements }}}
    76 
    80 
    77 Like in lists, the elements in a dictionary can be accessed using the
    81 Like in lists, the elements in a dictionary can be accessed using the
    78 index, here the index is the key. Try,
    82 index, here the index is the key. Try,
    79 ::
    83 ::
    80 
    84 
   151 
   155 
   152 {{{ switch to next slide, problem statement for the next solved
   156 {{{ switch to next slide, problem statement for the next solved
   153 exercise }}}
   157 exercise }}}
   154 
   158 
   155 Now let us try to print the data in the dictionary. We can use ``for``
   159 Now let us try to print the data in the dictionary. We can use ``for``
   156 loop to iterate.
   160 loop to iterate. Pause here and try to do it yourself.
       
   161 
       
   162 It can be solved as,
   157 ::
   163 ::
   158 
   164 
   159     for each in extensions.keys():
   165     for each in extensions.keys():
   160         print each, "-->", extensions[each]
   166         print each, "-->", extensions[each]
   161 
   167 
   162 
   168 
   163 {{{ switch to next slide, recap }}}
   169 {{{ switch to next slide, summary }}}
   164 
   170 
   165 This brings us to the end of this tutorial, we learned dictionaries
   171 This brings us to the end of this tutorial, we learned dictionaries
   166 and saw how to create an empty dictionary, build a dictionary with
   172 and saw how to create an empty dictionary, build a dictionary with
   167 some data in it, adding data, ``keys()`` and ``values()`` methods, and
   173 some data in it, adding data, ``keys()`` and ``values()`` methods, and
   168 iterating over the dictionaries.
   174 iterating over the dictionaries.