getting-started-with-lists/script.rst
changeset 441 430035b678f7
parent 430 7b2275daab60
child 466 00c1ba1cb9ef
equal deleted inserted replaced
434:c8ffd52305ff 441:430035b678f7
    44  * Append elements to lists
    44  * Append elements to lists
    45  * Delete elements from lists
    45  * Delete elements from lists
    46 
    46 
    47 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
    48 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
    49 order and there order has a meaning.
    49 order and the order has a meaning.
    50 
    50 
    51 .. #[[Anoop: "all the elements are in order and **there** order has a
    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
    52    meaning." - I guess something is wrong here, I am not able to
    53    follow this.]]
    53    follow this.]]
    54 
    54 
    73 All the list items need not be of the same data type.
    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 and 100 and 1.234 are
    77 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
    78 lists. Thus list themselves can be one of the element types possible
    78 lists including list itself.
    79 in lists. Thus lists can also contain other lists.  
       
    80 
    79 
    81 .. #[[Anoop: the sentence "Thus list themselves can be one of the
    80 .. #[[Anoop: the sentence "Thus list themselves can be one of the
    82    element types possible in lists" is not clear, rephrase it.]]
    81    element types possible in lists" is not clear, rephrase it.]]
    83 
    82 
    84 Example ::
    83 Example ::
    85 
    84 
    86       list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4]
    85       listinlist=[[4,2,3,4],'and', 1, 2, 3, 4]
    87 
    86 
    88 We access list elements using the index. The index begins from 0. So
    87 We access list elements using the index. The index begins from 0. So
    89 for list nonempty, nonempty[0] gives the first element, nonempty[1]
    88 for list nonempty, nonempty[0] gives the first element, nonempty[1]
    90 the second element and so on and nonempty[3] the last element. ::
    89 the second element and so on and nonempty[3] the last element. ::
    91 
    90 
    92 	    nonempty[0] 
    91 	    nonempty[0] 
    93 	    nonempty[1] 
    92 	    nonempty[1] 
    94 	    nonempty[3]
    93 	    nonempty[3]
    95 
    94 
       
    95 Following is an exercise that you must do. 
       
    96 
       
    97 %% %% What happens when you do nonempty[-1]. 
       
    98 
       
    99 Please, pause the video here. Do the exercise and then continue.  
       
   100 
    96 .. #[[Anoop: was negative indices introduced earlier, if not may be we
   101 .. #[[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
   102    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.]]
   103    tell that it gives the last element in the list.]]
    99 
   104 
   100 We can also access the elememts from the end using negative indices ::
   105 As you can see you get the last element which is 1.234.
       
   106 
       
   107 
       
   108 In python negative indices are used to access elements from the end::
   101    
   109    
   102    nonempty[-1] 
   110    nonempty[-1] 
   103    nonempty[-2] 
   111    nonempty[-2] 
   104    nonempty[-4]
   112    nonempty[-4]
   105 
   113 
   111    nonempty.append('onemore') 
   119    nonempty.append('onemore') 
   112    nonempty
   120    nonempty
   113    nonempty.append(6) 
   121    nonempty.append(6) 
   114    nonempty
   122    nonempty
   115    
   123    
       
   124 Following are  exercises that you must do. 
       
   125 
       
   126 %% %% What is the syntax to get the element 'and' 
       
   127 in the list,listinlist ?
       
   128 
       
   129 
       
   130 %% %% How would you get 'and' using negative indices?
       
   131 
       
   132 Please, pause the video here. Do the exercise and then continue.  
       
   133 
       
   134 The solution is on your screen
       
   135 
       
   136 
   116 As we can see non empty appends 'onemore' and 6 at the end.
   137 As we can see non empty appends 'onemore' and 6 at the end.
   117 
   138 
   118 Using len function we can check the number of elements in the list
   139 Using len function we can check the number of elements in the list
   119 nonempty. In this case it 6 ::
   140 nonempty. In this case it 6 ::
   120 	 
   141 	 
   146     nonempty.remove(100)
   167     nonempty.remove(100)
   147 
   168 
   148 but what if there were two 100's. To check that lets do a small
   169 but what if there were two 100's. To check that lets do a small
   149 experiment. ::
   170 experiment. ::
   150 
   171 
   151 	   nonempty.append('python') 
   172 	   nonempty.append('spam') 
   152 	   nonempty
   173 	   nonempty
   153 	   nonempty.remove('python') 
   174 	   nonempty.remove('spam') 
   154 	   nonempty
   175 	   nonempty
   155 
   176 
   156 If we check now we will see that the first occurence 'spam' is removed
   177 If we check now we will see that the first occurence 'spam' is removed
   157 thus remove removes the first occurence of the element in the sequence
   178 thus remove removes the first occurence of the element in the sequence
   158 and leaves others untouched.
   179 and leaves others untouched.
   159 
   180 
       
   181 
       
   182 
       
   183 
       
   184 
   160 .. #[[Anoop: does it have two spams or two pythons?]]
   185 .. #[[Anoop: does it have two spams or two pythons?]]
   161 
   186 
   162 .. #[[Anoop: there are no exercises/solved problems in this script,
   187 .. #[[Anoop: there are no exercises/solved problems in this script,
   163    add them]]
   188    add them]]
       
   189 
       
   190 Following are  exercises that you must do. 
       
   191 
       
   192 %% %% Remove the third element from the list, listinlist.   
       
   193 
       
   194 %% %% Remove 'and' from the list, listinlist.
       
   195 
       
   196 Please, pause the video here. Do the exercise and then continue.  
       
   197 
       
   198 
   164 
   199 
   165 {{{Slide for Summary }}}
   200 {{{Slide for Summary }}}
   166 
   201 
   167 
   202 
   168 In this tutorial we came across a sequence data type called lists. ::
   203 In this tutorial we came across a sequence data type called lists. ::