getting-started-with-lists/script.rst
changeset 342 588b681e70c6
parent 320 223044cf254f
child 374 57d145c18ccd
equal deleted inserted replaced
341:9f7eb1ed0e08 342:588b681e70c6
       
     1 Hello friends and welcome to the tutorial on getting started with
       
     2 lists.
       
     3 
       
     4  {{{ Show the slide containing title }}}
       
     5 
       
     6  {{{ Show the slide containing the outline slide }}}
       
     7 
       
     8 In this tutorial we will be getting acquainted with a python data
       
     9 structure called lists.  We will learn ::
       
    10  
       
    11  * How to create lists
       
    12  * Structure of lists
       
    13  * Access list elements
       
    14  * Append elements to lists
       
    15  * Deleting elements from lists
       
    16 
       
    17 List is a compound data type, it can contain data of other data
       
    18 types. List is also a sequence data type, all the elements are in
       
    19 order and there order has a meaning.
       
    20 
       
    21 We will first create an empty list with no elements. On your IPython
       
    22 shell type ::
       
    23 
       
    24    empty = [] 
       
    25    type(empty)
       
    26    
       
    27 
       
    28 This is an empty list without any elements.
       
    29 
       
    30 * Filled lists
       
    31 
       
    32 Lets now define a list, nonempty and fill it with some random elements.
       
    33 
       
    34 nonempty = ['spam', 'eggs', 100, 1.234]
       
    35 
       
    36 Thus the simplest way of creating a list is typing out a sequence 
       
    37 of comma-separated values (items) between square brackets. 
       
    38 All the list items need not have the same data type.
       
    39 
       
    40 
       
    41 
       
    42 As we can see lists can contain different kinds of data. In the
       
    43 previous example 'spam' and 'eggs' are strings and 100 and 1.234
       
    44 integer and float. Thus we can put elements of heterogenous types in
       
    45 lists. Thus list themselves can be one of the element types possible
       
    46 in lists. Thus lists can also contain other lists.  Example ::
       
    47 
       
    48       list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4]
       
    49 
       
    50 We access list elements using the number of index. The
       
    51 index begins from 0. So for list nonempty, nonempty[0] gives the
       
    52 first element, nonempty[1] the second element and so on and
       
    53 nonempty[3] the last element. ::
       
    54 
       
    55 	    nonempty[0] 
       
    56 	    nonempty[1] 
       
    57 	    nonempty[3]
       
    58 
       
    59 We can also access the elememts from the end using negative indices ::
       
    60    
       
    61    nonempty[-1] 
       
    62    nonempty[-2] 
       
    63    nonempty[-4]
       
    64 
       
    65 -1 gives the last element which is the 4th element , -2 second to last and -4 gives the fourth
       
    66 from last element which is first element.
       
    67 
       
    68 We can append elements to the end of a list using append command. ::
       
    69 
       
    70    nonempty.append('onemore') 
       
    71    nonempty
       
    72    nonempty.append(6) 
       
    73    nonempty
       
    74    
       
    75 As we can see non empty appends 'onemore' and 6 at the end.
       
    76 
       
    77 
       
    78 
       
    79 Using len function we can check the number of elements in the list
       
    80 nonempty. In this case it being 6 ::
       
    81 	 
       
    82 	 len(nonempty)
       
    83 
       
    84 
       
    85 
       
    86 Just like we can append elements to a list we can also remove them.
       
    87 There are two ways of doing it. One is by using index. ::
       
    88 
       
    89       del(nonempty[1])
       
    90 
       
    91 
       
    92 
       
    93 deletes the element at index 1, i.e the second element of the
       
    94 list, 'eggs'. The other way is removing element by content. Lets say
       
    95 one wishes to delete 100 from nonempty list the syntax of the command
       
    96 should be :: 
       
    97       
       
    98       nonempty.remove(100)
       
    99 
       
   100 but what if their were two 100's. To check that lets do a small
       
   101 experiment. ::
       
   102 
       
   103 	   nonempty.append('python') 
       
   104 	   nonempty
       
   105 	   nonempty.remove('python') 
       
   106 	   nonempty
       
   107 
       
   108 If we check a now we will see that the first occurence 'spam' is removed
       
   109 thus remove removes the first occurence of the element in the sequence
       
   110 and leaves others untouched.
       
   111 
       
   112 
       
   113 {{{Slide for Summary }}}
       
   114 
       
   115 
       
   116 In this tutorial we came across a sequence data type called lists. ::
       
   117 
       
   118  * We learned how to create lists.  
       
   119  * How to access lists.
       
   120  * Append elements to list.
       
   121  * Delete Element from list.  
       
   122  * And Checking list length.
       
   123  
       
   124 
       
   125 
       
   126 {{{ Sponsored by Fossee Slide }}}
       
   127 
       
   128 This tutorial was created as a part of FOSSEE project.
       
   129 
       
   130 I hope you found this tutorial useful.
       
   131 
       
   132 Thank You
       
   133 
       
   134 
       
   135  * Author : Amit Sethi 
       
   136  * First Reviewer : 
       
   137  * Second Reviewer : Nishanth