getting-started-with-lists/script.rst
changeset 430 7b2275daab60
parent 415 8afa31ae6129
child 441 430035b678f7
equal deleted inserted replaced
429:bb6bab81e9f2 430:7b2275daab60
    18 ..   1. getting started with strings
    18 ..   1. getting started with strings
    19 ..   #. getting started with lists
    19 ..   #. getting started with lists
    20 ..   #. basic datatypes
    20 ..   #. basic datatypes
    21      
    21      
    22 .. Author              : Amit 
    22 .. Author              : Amit 
    23    Internal Reviewer   : 
    23    Internal Reviewer   : Anoop Jacob Thomas <anoop@fossee.in>
    24    External Reviewer   :
    24    External Reviewer   :
    25    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    25    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    26 
    26 
       
    27 .. #[[Anoop: Slides contain only outline and summary
       
    28 
    27 Script
    29 Script
    28 ------
    30 ------
       
    31  {{{ Show the slide containing title }}}
       
    32 
    29 Hello friends and welcome to the tutorial on getting started with
    33 Hello friends and welcome to the tutorial on getting started with
    30 lists.
    34 lists.
    31 
       
    32  {{{ Show the slide containing title }}}
       
    33 
    35 
    34  {{{ Show the slide containing the outline slide }}}
    36  {{{ Show the slide containing the outline slide }}}
    35 
    37 
    36 In this tutorial we will be getting acquainted with a python data
    38 In this tutorial we will be getting acquainted with a python data
    37 structure called lists.  We will learn ::
    39 structure called lists.  We will learn ::
    38  
    40  
    39  * How to create lists
    41  * How to create lists
    40  * Structure of lists
    42  * Structure of lists
    41  * Access list elements
    43  * Access list elements
    42  * Append elements to lists
    44  * Append elements to lists
    43  * Deleting elements from lists
    45  * Delete elements from lists
    44 
    46 
    45 List is a compound data type, it can contain data of other data
    47 List is a compound data type, it can contain data of other data
    46 types. List is also a sequence data type, all the elements are in
    48 types. List is also a sequence data type, all the elements are in
    47 order and there order has a meaning.
    49 order and there order has a meaning.
       
    50 
       
    51 .. #[[Anoop: "all the elements are in order and **there** order has a
       
    52    meaning." - I guess something is wrong here, I am not able to
       
    53    follow this.]]
    48 
    54 
    49 We will first create an empty list with no elements. On your IPython
    55 We will first create an empty list with no elements. On your IPython
    50 shell type ::
    56 shell type ::
    51 
    57 
    52    empty = [] 
    58    empty = [] 
    53    type(empty)
    59    type(empty)
    54    
    60    
    55 
    61 
    56 This is an empty list without any elements.
    62 This is an empty list without any elements.
    57 
    63 
    58 * Filled lists
    64 .. #[[Anoop: the document has to be continous, without any
       
    65    subheadings, removing * Filled lists]]
    59 
    66 
    60 Lets now define a list, nonempty and fill it with some random elements.
    67 Lets now see how to define a non-empty list. We do it as,::
    61 
    68 
    62 nonempty = ['spam', 'eggs', 100, 1.234]
    69      nonempty = ['spam', 'eggs', 100, 1.234]
    63 
    70 
    64 Thus the simplest way of creating a list is typing out a sequence 
    71 Thus the simplest way of creating a list is typing out a sequence 
    65 of comma-separated values (items) between square brackets. 
    72 of comma-separated values (items) between square brackets. 
    66 All the list items need not have the same data type.
    73 All the list items need not be of the same data type.
    67 
       
    68 
       
    69 
    74 
    70 As we can see lists can contain different kinds of data. In the
    75 As we can see lists can contain different kinds of data. In the
    71 previous example 'spam' and 'eggs' are strings and 100 and 1.234
    76 previous example 'spam' and 'eggs' are strings and 100 and 1.234 are
    72 integer and float. Thus we can put elements of heterogenous types in
    77 integer and float. Thus we can put elements of heterogenous types in
    73 lists. Thus list themselves can be one of the element types possible
    78 lists. Thus list themselves can be one of the element types possible
    74 in lists. Thus lists can also contain other lists.  Example ::
    79 in lists. Thus lists can also contain other lists.  
       
    80 
       
    81 .. #[[Anoop: the sentence "Thus list themselves can be one of the
       
    82    element types possible in lists" is not clear, rephrase it.]]
       
    83 
       
    84 Example ::
    75 
    85 
    76       list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4]
    86       list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4]
    77 
    87 
    78 We access list elements using the number of index. The
    88 We access list elements using the index. The index begins from 0. So
    79 index begins from 0. So for list nonempty, nonempty[0] gives the
    89 for list nonempty, nonempty[0] gives the first element, nonempty[1]
    80 first element, nonempty[1] the second element and so on and
    90 the second element and so on and nonempty[3] the last element. ::
    81 nonempty[3] the last element. ::
       
    82 
    91 
    83 	    nonempty[0] 
    92 	    nonempty[0] 
    84 	    nonempty[1] 
    93 	    nonempty[1] 
    85 	    nonempty[3]
    94 	    nonempty[3]
       
    95 
       
    96 .. #[[Anoop: was negative indices introduced earlier, if not may be we
       
    97    can ask them to try out nonempty[-1] and see what happens and then
       
    98    tell that it gives the last element in the list.]]
    86 
    99 
    87 We can also access the elememts from the end using negative indices ::
   100 We can also access the elememts from the end using negative indices ::
    88    
   101    
    89    nonempty[-1] 
   102    nonempty[-1] 
    90    nonempty[-2] 
   103    nonempty[-2] 
    91    nonempty[-4]
   104    nonempty[-4]
    92 
   105 
    93 -1 gives the last element which is the 4th element , -2 second to last and -4 gives the fourth
   106 -1 gives the last element which is the 4th element , -2 second to last
    94 from last element which is first element.
   107 and -4 gives the fourth from last element which is first element.
    95 
   108 
    96 We can append elements to the end of a list using append command. ::
   109 We can append elements to the end of a list using append command. ::
    97 
   110 
    98    nonempty.append('onemore') 
   111    nonempty.append('onemore') 
    99    nonempty
   112    nonempty
   100    nonempty.append(6) 
   113    nonempty.append(6) 
   101    nonempty
   114    nonempty
   102    
   115    
   103 As we can see non empty appends 'onemore' and 6 at the end.
   116 As we can see non empty appends 'onemore' and 6 at the end.
   104 
   117 
   105 
       
   106 
       
   107 Using len function we can check the number of elements in the list
   118 Using len function we can check the number of elements in the list
   108 nonempty. In this case it being 6 ::
   119 nonempty. In this case it 6 ::
   109 	 
   120 	 
   110 	 len(nonempty)
   121 	 len(nonempty)
   111 
   122 
   112 
   123 
   113 
   124 
   119 
   130 
   120 
   131 
   121 deletes the element at index 1, i.e the second element of the
   132 deletes the element at index 1, i.e the second element of the
   122 list, 'eggs'. The other way is removing element by content. Lets say
   133 list, 'eggs'. The other way is removing element by content. Lets say
   123 one wishes to delete 100 from nonempty list the syntax of the command
   134 one wishes to delete 100 from nonempty list the syntax of the command
   124 should be :: 
   135 should be 
   125       
       
   126       nonempty.remove(100)
       
   127 
   136 
   128 but what if their were two 100's. To check that lets do a small
   137 .. #[[Anoop: let x = [1,2,1,3]
       
   138    	     now x.remove(x[2])
       
   139 	     still x is [2,1,3] so that is not the way to remove
       
   140 	     element by index, it removed first occurrence of 1(by
       
   141 	     content) and not based on index, so make necessary
       
   142 	     changes]]
       
   143 
       
   144 ::
       
   145 
       
   146     nonempty.remove(100)
       
   147 
       
   148 but what if there were two 100's. To check that lets do a small
   129 experiment. ::
   149 experiment. ::
   130 
   150 
   131 	   nonempty.append('python') 
   151 	   nonempty.append('python') 
   132 	   nonempty
   152 	   nonempty
   133 	   nonempty.remove('python') 
   153 	   nonempty.remove('python') 
   134 	   nonempty
   154 	   nonempty
   135 
   155 
   136 If we check a now we will see that the first occurence 'spam' is removed
   156 If we check now we will see that the first occurence 'spam' is removed
   137 thus remove removes the first occurence of the element in the sequence
   157 thus remove removes the first occurence of the element in the sequence
   138 and leaves others untouched.
   158 and leaves others untouched.
   139 
   159 
       
   160 .. #[[Anoop: does it have two spams or two pythons?]]
       
   161 
       
   162 .. #[[Anoop: there are no exercises/solved problems in this script,
       
   163    add them]]
   140 
   164 
   141 {{{Slide for Summary }}}
   165 {{{Slide for Summary }}}
   142 
   166 
   143 
   167 
   144 In this tutorial we came across a sequence data type called lists. ::
   168 In this tutorial we came across a sequence data type called lists. ::
   149  * Delete Element from list.  
   173  * Delete Element from list.  
   150  * And Checking list length.
   174  * And Checking list length.
   151  
   175  
   152 
   176 
   153 
   177 
   154 {{{ Sponsored by Fossee Slide }}}
   178 {{{ show Sponsored by Fossee Slide }}}
   155 
   179 
   156 This tutorial was created as a part of FOSSEE project.
   180 This tutorial was created as a part of FOSSEE project.
   157 
   181 
   158 I hope you found this tutorial useful.
   182 I hope you found this tutorial useful.
   159 
   183 
   160 Thank You
   184 Thank You
   161 
   185 
   162 
   186 ..
   163  * Author : Amit Sethi 
   187  * Author : Amit Sethi 
   164  * First Reviewer : 
   188  * First Reviewer : 
   165  * Second Reviewer : Nishanth
   189  * Second Reviewer : Nishanth