liststart.rst
author Nishanth <nishanth@fossee.in>
Thu, 23 Sep 2010 10:58:39 +0530
changeset 190 6dcc77582e26
parent 185 35a3811ca91e
permissions -rw-r--r--
made the comments in my script as rst comments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
     1
.. #[Nishanth]: liststart is not a good name. there is no consistency.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
     2
                Use underscores or hyphens instead of spaces and 
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
     3
                make the filename from LO name
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
     4
                Ex: getting_started_with_lists (or)
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
     5
                getting_started_lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
     6
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     7
Hello friends and welcome to the tutorial on getting started with
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
     8
lists.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     9
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    10
 {{{ Show the slide containing title }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    11
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    12
 {{{ Show the slide containing the outline slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    13
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    14
In this tutorial we will be getting acquainted with a python data
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    15
structure called lists.  We will learn :
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    16
 * How to create lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    17
 * Structure of lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    18
 * Access list elements
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    19
 * Append elements to lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    20
 * Deleting elements from lists
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    21
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    22
.. #[Nishanth]: Did you compile this??
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    23
                There must an empty before the bulleted list
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    24
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    25
I hope you have ipython running on your system.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    26
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    27
.. #[Nishanth]: need not specify. Implicit that IPython is running
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    28
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    29
List is a compound data type, it can contain data of other data
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    30
types. List is also a sequence data type, all the elements are in
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    31
order and there order has a meaning.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    32
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    33
We will first create an empty list with no elements. On your IPython
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    34
shell type ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    35
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    36
   empty = [] 
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    37
   type(empty)
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    38
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    39
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    40
This is an empty list without any elements.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    41
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    42
* Filled lists
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    43
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    44
Lets now define a list, nonempty and fill it with some random elements.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    45
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    46
nonempty = ['spam', 'eggs', 100, 1.234]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    47
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    48
Thus the simplest way of creating a list is typing out a sequence 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    49
of comma-separated values (items) between square brackets. 
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    50
All the list items need not have the same data type.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    51
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    52
.. #[Nishanth]: do not use "You" or anything else. Stick to "We"
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    53
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    54
As we can see lists can contain different kinds of data. In the
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    55
previous example 'spam' and 'eggs' are strings and 100 and 1.234
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    56
integer and float. Thus we can put elements of heterogenous types in
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    57
lists. Thus list themselves can be one of the element types possible
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    58
in lists. Thus lists can also contain other lists.  Example ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    59
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    60
      list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    61
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    62
We access list elements using the number of index. The
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    63
index begins from 0. So for list nonempty, nonempty[0] gives the
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    64
first element, nonempty[1] the second element and so on and
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    65
nonempty[3] the last element.::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    66
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    67
	    nonempty[0] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    68
	    nonempty[1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    69
	    nonempty[3]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    70
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    71
We can also access the elememts from the end using negative indices ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    72
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    73
   nonempty[-1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    74
   nonempty[-2] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    75
   nonempty[-4]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    76
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    77
-1 being the last element , -2 second to last and -4 being the first
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    78
element.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    79
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    80
.. #[Nishanth]: -1 being last element sounds like -1 is the last element
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    81
                Instead say -1 gives the last element which is 4
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    82
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    83
.. #[Nishanth]: Instead of saying -4 being the first, say -4 gives 4th 
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    84
                from the last which is the first element.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    85
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    86
* =append= elements 
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    87
We can append elements to the end of a list using append command. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    88
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    89
   nonempty.append('onemore') 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    90
   nonempty.append(6) 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    91
   nonempty
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    92
   
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    93
As we can see non empty appends 'onemore' and 6 at the end.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    94
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    95
.. #[Nishanth]: First show an example with only one append.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    96
                may be show the value of a after first append
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    97
                then show what happens after second append 
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    98
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    99
Using len function we can check the number of elements in the list
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   100
nonempty. Because we just appended two elements at the end this
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   101
returns us 6.::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   102
	 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   103
	 len(nonempty)
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   104
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   105
.. #[Nishanth]: the "because ..." can be removed. You can simply
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   106
                say len gives the no.of elements which is 6 here
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   107
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   108
Just like we can append elements to a list we can also remove them.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   109
There are two ways of doing. One is by using index. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   110
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   111
      del(nonempty[1])
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   112
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   113
.. #[Nishanth]: do not use "You" or anything else. Stick to We
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   114
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   115
deletes the element at index 1, i.e the second element of the
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   116
list, 'eggs'. The other way is removing element by content. Lets say
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   117
one wishes to delete 100 from nonempty list the syntax of the command
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   118
should be :: 
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   119
      
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   120
      a.remove(100)
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   121
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   122
but what if their were two 100's. To check that lets do a small
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   123
experiment. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   124
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   125
	   a.append('spam') 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   126
	   a 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   127
	   a.remove('spam') 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   128
	   a
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   129
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   130
If we check a now we will see that the first occurence 'spam' is removed
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   131
thus remove removes the first occurence of the element in the sequence
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   132
and leaves others untouched.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   133
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   134
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   135
{{{Slide for Summary }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   136
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   137
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   138
In this tutorial we came across a sequence data type called lists. ::
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   139
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   140
 * We learned how to create lists.  
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   141
 * Append elements to list.
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   142
 * Delete Element from list.  
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   143
 * And Checking list length.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   144
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   145
.. #[Nishanth]: See the diff. I have corrected punctuation in many places.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   146
                The first thing you do before committing is compile the script.
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   147
                I have corrected syntax errors also in many places.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   148
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   149
{{{ Sponsored by Fossee Slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   150
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   151
This tutorial was created as a part of FOSSEE project.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   152
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   153
I hope you found this tutorial useful.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   154
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   155
Thank You
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   156
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   157
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   158
Author : Amit Sethi 
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   159
First Reviewer : 
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   160
Second Reviewer : Nishanth