getting-started-with-lists/script.rst
changeset 490 2280bbdce638
parent 466 00c1ba1cb9ef
child 491 ebfe3a675882
equal deleted inserted replaced
484:a1828587a617 490:2280bbdce638
    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    Language Reviewer   : Bhanukiran
    25    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    26    Checklist OK?       : <put date stamp here, if 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
    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 
   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.
   180 
   182 
   181 One should remember this that while del removes by index number.
   183 One should remember this that while del removes by index number,
   182 Remove , removes on the basis of content being passed so if ::
   184 `remove` removes on the basis of content being passed on. For instance
       
   185 if ::
   183        
   186        
   184        k = [1,2,1,3] 
   187        k = [1,2,1,3] 
   185        del([k[2])
   188        del([k[2])
   186 
   189 
   187 gives us [1,2,3]. ::
   190 gives us [1,2,3]. ::