getting-started-with-lists/script.rst
changeset 521 88a01948450d
parent 491 ebfe3a675882
equal deleted inserted replaced
520:8249ae9d570a 521:88a01948450d
    20 ..   #. basic datatypes
    20 ..   #. basic datatypes
    21      
    21      
    22 .. Author              : Amit 
    22 .. Author              : Amit 
    23    Internal Reviewer   : Anoop Jacob Thomas <anoop@fossee.in>
    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    Language Reviewer   : Bhanukiran
       
    26    Checklist OK?       : <12-11-2010, Anand, OK> [2010-10-05]
    26 
    27 
    27 .. #[[Anoop: Slides contain only outline and summary
    28 .. #[[Anoop: Slides contain only outline and summary
    28 
    29 
    29 Script
    30 Script
    30 ------
    31 ------
    42  * Structure of lists
    43  * Structure of lists
    43  * Access list elements
    44  * Access list elements
    44  * Append elements to lists
    45  * Append elements to lists
    45  * Delete elements from lists
    46  * Delete elements from lists
    46 
    47 
    47 List is a compound data type, it can contain data of other data
    48 List is a compound data type, it can contain data of mutually
    48 types. List is also a sequence data type, all the elements are in
    49 different datatypes. List is also a sequence data type, all the
    49 order and the order has a meaning.
    50 elements are arranged in a given order.
    50 
    51 
    51 .. #[[Anoop: "all the elements are in order and **there** order has a
    52 .. #[[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    meaning." - I guess something is wrong here, I am not able to
    53    follow this.]]
    54    follow this.]]
    54 
    55 
    67 Lets now see how to define a non-empty list. We do it as,::
    68 Lets now see how to define a non-empty list. We do it as,::
    68 
    69 
    69      nonempty = ['spam', 'eggs', 100, 1.234]
    70      nonempty = ['spam', 'eggs', 100, 1.234]
    70 
    71 
    71 Thus the simplest way of creating a list is typing out a sequence 
    72 Thus the simplest way of creating a list is typing out a sequence 
    72 of comma-separated values (items) between square brackets. 
    73 of comma-separated values (or items) between two square brackets. 
    73 All the list items need not be of the same data type.
       
    74 
    74 
    75 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
    76 previous example 'spam' and 'eggs' are strings and 100 and 1.234 are
    76 previous example 'spam' and 'eggs' are strings whereas 100 and 1.234 are
    77 integer and float. Thus we can put elements of heterogenous types in
    77 integer and float respectively. Thus we can put elements of different types in
    78 lists including list itself.
    78 lists including lists itself. This property makes lists heterogeneous
       
    79 data structures.
    79 
    80 
    80 .. #[[Anoop: the sentence "Thus list themselves can be one of the
    81 .. #[[Anoop: the sentence "Thus list themselves can be one of the
    81    element types possible in lists" is not clear, rephrase it.]]
    82    element types possible in lists" is not clear, rephrase it.]]
    82 
    83 
    83 Example ::
    84 Example ::
    84 
    85 
    85       listinlist=[[4,2,3,4],'and', 1, 2, 3, 4]
    86       listinlist=[[4,2,3,4],'and', 1, 2, 3, 4]
    86 
    87 
    87 We access list elements using the index. The index begins from 0. So
    88 We access an element of a list using its corresponding index. Index of
    88 for list nonempty, nonempty[0] gives the first element, nonempty[1]
    89 the first element of a list is 0. So for the list nonempty, nonempty[0] 
    89 the second element and so on and nonempty[3] the last element. ::
    90 gives the first element, nonempty[1] the second element and so on and 
       
    91 nonempty[3] the last element. ::
    90 
    92 
    91 	    nonempty[0] 
    93 	    nonempty[0] 
    92 	    nonempty[1] 
    94 	    nonempty[1] 
    93 	    nonempty[3]
    95 	    nonempty[3]
    94 
    96 
   110    nonempty[-1] 
   112    nonempty[-1] 
   111    nonempty[-2] 
   113    nonempty[-2] 
   112    nonempty[-4]
   114    nonempty[-4]
   113 
   115 
   114 -1 gives the last element which is the 4th element , -2 second to last
   116 -1 gives the last element which is the 4th element , -2 second to last
   115 and -4 gives the fourth from last element which is first element.
   117 and -4 gives the fourth from the last which, in this case,  is the first element.
   116 
   118 
   117 We can append elements to the end of a list using append command. ::
   119 We can append elements to the end of a list using the method append. ::
   118 
   120 
   119    nonempty.append('onemore') 
   121    nonempty.append('onemore') 
   120    nonempty
   122    nonempty
   121    nonempty.append(6) 
   123    nonempty.append(6) 
   122    nonempty
   124    nonempty
   132 Please, pause the video here. Do the exercise and then continue.  
   134 Please, pause the video here. Do the exercise and then continue.  
   133 
   135 
   134 The solution is on your screen
   136 The solution is on your screen
   135 
   137 
   136 
   138 
   137 As we can see non empty appends 'onemore' and 6 at the end.
   139 As we can see nonempty is appended with 'onemore' and 6 at the end.
   138 
   140 
   139 Using len function we can check the number of elements in the list
   141 Using len function we can check the number of elements in the list
   140 nonempty. In this case it 6 ::
   142 nonempty. In this case it is 6 ::
   141 	 
   143 	 
   142 	 len(nonempty)
   144 	 len(nonempty)
   143 
   145 
   144 
   146 
   145 
   147 
   151 
   153 
   152 
   154 
   153 deletes the element at index 1, i.e the second element of the
   155 deletes the element at index 1, i.e the second element of the
   154 list, 'eggs'. The other way is removing element by content. Lets say
   156 list, 'eggs'. The other way is removing element by content. Lets say
   155 one wishes to delete 100 from nonempty list the syntax of the command
   157 one wishes to delete 100 from nonempty list the syntax of the command
   156 should be 
   158 would be 
   157 
   159 
   158 .. #[[Anoop: let x = [1,2,1,3]
   160 .. #[[Anoop: let x = [1,2,1,3]
   159    	     now x.remove(x[2])
   161    	     now x.remove(x[2])
   160 	     still x is [2,1,3] so that is not the way to remove
   162 	     still x is [2,1,3] so that is not the way to remove
   161 	     element by index, it removed first occurrence of 1(by
   163 	     element by index, it removed first occurrence of 1(by
   173 	   nonempty
   175 	   nonempty
   174 	   nonempty.remove('spam') 
   176 	   nonempty.remove('spam') 
   175 	   nonempty
   177 	   nonempty
   176 
   178 
   177 If we check now we will see that the first occurence 'spam' is removed
   179 If we check now we will see that the first occurence 'spam' is removed
   178 thus remove removes the first occurence of the element in the sequence
   180 and therefore `remove` removes the first occurence of the element in the sequence
   179 and leaves others untouched.
   181 and leaves others untouched.
       
   182 
       
   183 One should remember this that while del removes by index number,
       
   184 `remove` removes on the basis of content being passed on. For instance
       
   185 if ::
       
   186        
       
   187        k = [1,2,1,3] 
       
   188        del([k[2])
       
   189 
       
   190 gives us [1,2,3]. ::
       
   191 
       
   192       k.remove(x[2])
       
   193 
       
   194 will give us [2,1,3]. Since it deletes the first occurence of what is
       
   195 returned by x[2] which is 1.      
       
   196 
       
   197 
   180 
   198 
   181 
   199 
   182 
   200 
   183 
   201 
   184 
   202