dictionaries/script.rst
changeset 355 75a182d6e908
parent 319 e8c02b3c51ac
child 370 7419836c90c1
equal deleted inserted replaced
354:4c09238600e0 355:75a182d6e908
     4 .. At the end of this tutorial, you will be able to 
     4 .. At the end of this tutorial, you will be able to 
     5 
     5 
     6 .. 1. Create dictionaries
     6 .. 1. Create dictionaries
     7 .. #. Add data to dictionaries
     7 .. #. Add data to dictionaries
     8 .. #. Retrieve data
     8 .. #. Retrieve data
     9 .. #. Familiarize using ``.keys()`` and ``.values()`` methods
     9 .. #. use ``.keys()`` and ``.values()`` methods
    10 .. #. Checking for container-ship of keys
    10 .. #. Check for container-ship of keys
    11 .. #. Iterating over elements
    11 .. #. Iterate over elements
    12 
    12 
    13 .. Prerequisites
    13 .. Prerequisites
    14 .. -------------
    14 .. -------------
    15 
    15 
    16 ..   1. should have ``ipython``  installed. 
    16 ..   1. should have ``ipython``  installed. 
    20 .. Author              : Anoop Jacob Thomas <anoop@fossee.in>
    20 .. Author              : Anoop Jacob Thomas <anoop@fossee.in>
    21    Internal Reviewer   : 
    21    Internal Reviewer   : 
    22    External Reviewer   :
    22    External Reviewer   :
    23    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    23    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    24 
    24 
       
    25 .. #[Puneeth: Quickref]
    25 
    26 
    26 ============
    27 ============
    27 Dictionaries
    28 Dictionaries
    28 ============
    29 ============
    29 
    30 
    47 indexes, dictionaries have keys or strings as indexes.
    48 indexes, dictionaries have keys or strings as indexes.
    48 
    49 
    49 Before we can proceed, start your IPython interpreter with the
    50 Before we can proceed, start your IPython interpreter with the
    50 ``-pylab`` option.
    51 ``-pylab`` option.
    51 
    52 
       
    53 .. #[Puneeth: We don't need pylab]
       
    54 
    52 {{{ start ipython interpreter by issuing command ipython -pylab }}}
    55 {{{ start ipython interpreter by issuing command ipython -pylab }}}
    53 
    56 
    54 {{{ switch to next slide, Creating dictionary }}}
    57 {{{ switch to next slide, Creating dictionary }}}
    55 
    58 
    56 Let us start by creating an empty dictionary, type the following in
    59 Let us start by creating an empty dictionary, type the following in
    57 your IPython interpreter.
    60 your IPython interpreter.
    58 ::
    61 ::
    59 
    62 
    60     mt_dict = {}    
    63     mt_dict = {}    
    61 
    64 
    62 Notice that unlike lists curly braces are used define ``dictionary``,
    65 Notice that unlike lists, curly braces are used define ``dictionary``.
    63 
    66 
    64 {{{ move the mouse over curly braces to grab attention }}}
    67 {{{ move the mouse over curly braces to grab attention }}}
    65 
    68 
    66 Now let us see how to create a filled dictionary,
    69 Now let us see how to create a non-empty dictionary,
    67 ::
    70 ::
    68 
    71 
    69     extensions = {'jpg' : 'JPEG Image', 'py' : 'Python script', 'html' : 'Html document', 'pdf' : 'Portable Document Format'}
    72     extensions = {'jpg' : 'JPEG Image', 'py' : 'Python script', 'html' : 'Html document', 'pdf' : 'Portable Document Format'}
    70 
    73 
    71 Notice that each key-value pair is separated by a comma
    74 Notice that each key-value pair is separated by a comma
   144 ::
   147 ::
   145 
   148 
   146     'py' in extensions
   149     'py' in extensions
   147     'odt' in extensions
   150     'odt' in extensions
   148 
   151 
   149 So in short it will return ``True`` if the key is found in the
   152 It will return ``True`` if the key is found in the dictionary, and
   150 dictionary, and will return ``False`` if key is not present. Note that
   153 will return ``False`` if key is not present. Note that we can check
   151 we can check only for container-ship of keys in dictionaries and not
   154 only for container-ship of keys in dictionaries and not values.
   152 values.
       
   153 
   155 
   154 {{{ switch to next slide, Retrieve keys and values }}}
   156 {{{ switch to next slide, Retrieve keys and values }}}
   155 
   157 
   156 Now let us see how to retrieve the keys and values. We can use the
   158 Now let us see how to retrieve the keys and values. We can use the
   157 method ``keys()`` for getting a list of the keys in a particular
   159 method ``keys()`` for getting a list of the keys in a particular