getting_started_with_lists.rst
author Madhusudan.C.S <madhusudancs@gmail.com>
Thu, 23 Sep 2010 16:42:47 +0530
changeset 207 2f30ecfd6007
parent 204 65e5e2362bc9
child 208 0f78508a4478
permissions -rw-r--r--
Added changes as per Nishanth's suggestions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on getting started with
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
     2
lists.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     3
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     4
 {{{ Show the slide containing title }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     5
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     6
 {{{ Show the slide containing the outline slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     7
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     8
In this tutorial we will be getting acquainted with a python data
204
65e5e2362bc9 small change in getting_started_with_lists
amit
parents: 201
diff changeset
     9
structure called lists.  We will learn ::
65e5e2362bc9 small change in getting_started_with_lists
amit
parents: 201
diff changeset
    10
 
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    11
 * How to create lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    12
 * Structure of lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    13
 * Access list elements
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    14
 * Append elements to lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    15
 * Deleting elements from lists
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    16
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    17
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
    18
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
    19
order and there order has a meaning.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    20
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    21
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
    22
shell type ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    23
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    24
   empty = [] 
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    25
   type(empty)
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    26
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    27
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    28
This is an empty list without any elements.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    29
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    30
* Filled lists
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    31
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    32
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
    33
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    34
nonempty = ['spam', 'eggs', 100, 1.234]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    35
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    36
Thus the simplest way of creating a list is typing out a sequence 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    37
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
    38
All the list items need not have the same data type.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    39
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    40
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    41
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    42
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
    43
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
    44
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
    45
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
    46
in lists. Thus lists can also contain other lists.  Example ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    47
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    48
      list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    49
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    50
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
    51
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
    52
first element, nonempty[1] the second element and so on and
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    53
nonempty[3] the last element.::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    54
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    55
	    nonempty[0] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    56
	    nonempty[1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    57
	    nonempty[3]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    58
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    59
We can also access the elememts from the end using negative indices ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    60
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    61
   nonempty[-1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    62
   nonempty[-2] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    63
   nonempty[-4]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    64
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    65
-1 gives the last element which is the 4th element , -2 second to last and -4 gives the fourth
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    66
from last element which is first element.
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    67
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    68
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    69
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    70
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    71
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
    72
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    73
   nonempty.append('onemore') 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    74
   nonempty
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    75
   nonempty.append(6) 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    76
   nonempty
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    77
   
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    78
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
    79
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    80
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    81
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    82
Using len function we can check the number of elements in the list
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    83
nonempty. In this case it being 6:
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    84
	 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    85
	 len(nonempty)
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    86
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    87
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    88
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    89
Just like we can append elements to a list we can also remove them.
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    90
There are two ways of doing it. One is by using index. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    91
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    92
      del(nonempty[1])
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    93
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    94
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    95
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    96
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
    97
list, 'eggs'. The other way is removing element by content. Lets say
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    98
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
    99
should be :: 
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   100
      
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   101
      a.remove(100)
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   102
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   103
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
   104
experiment. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   105
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   106
	   a.append('spam') 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   107
	   a 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   108
	   a.remove('spam') 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   109
	   a
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   110
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   111
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
   112
thus remove removes the first occurence of the element in the sequence
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   113
and leaves others untouched.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   114
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   115
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   116
{{{Slide for Summary }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   117
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   118
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   119
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
   120
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   121
 * We learned how to create lists.  
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   122
 * Append elements to list.
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   123
 * Delete Element from list.  
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   124
 * And Checking list length.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   125
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   126
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   127
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   128
{{{ Sponsored by Fossee Slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   129
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   130
This tutorial was created as a part of FOSSEE project.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   131
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   132
I hope you found this tutorial useful.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   133
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   134
Thank You
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   135
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   136
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   137
Author : Amit Sethi 
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   138
First Reviewer : 
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   139
Second Reviewer : Nishanth