getting-started-with-lists/script.rst
author bhanu
Mon, 08 Nov 2010 01:34:02 +0530
changeset 454 0c69c2b32183
parent 374 57d145c18ccd
child 415 8afa31ae6129
permissions -rw-r--r--
Language check done for the script `strings`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
374
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     1
.. Objectives
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     2
.. ----------
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     3
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     4
.. By the end of this tutorial, you will be able to
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     5
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     6
.. Create Lists.
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     7
.. Access List elements.
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     8
.. Append elemets to list
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
     9
.. Delete list elemets
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    10
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    11
.. 1. getting started with ipython 
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    12
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    13
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    14
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    15
.. Prerequisites
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    16
.. -------------
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    17
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    18
..   1. getting started with strings
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    19
..   #. getting started with lists
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    20
..   #. basic datatypes
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    21
     
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    22
.. Author              : Puneeth 
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    23
   Internal Reviewer   : Amit 
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    24
   External Reviewer   :
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    25
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    26
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    27
Script
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    28
------
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    29
Hello friends and welcome to the tutorial on getting started with
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    30
lists.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    31
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    32
 {{{ Show the slide containing title }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    33
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    34
 {{{ Show the slide containing the outline slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    35
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    36
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
    37
structure called lists.  We will learn ::
65e5e2362bc9 small change in getting_started_with_lists
amit
parents: 201
diff changeset
    38
 
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    39
 * How to create lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    40
 * Structure of lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    41
 * Access list elements
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    42
 * Append elements to lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    43
 * Deleting elements from lists
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    44
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    45
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
    46
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
    47
order and there order has a meaning.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    48
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    49
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
    50
shell type ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    51
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    52
   empty = [] 
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    53
   type(empty)
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
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    56
This is an empty list without any elements.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    57
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    58
* Filled lists
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
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
    61
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    62
nonempty = ['spam', 'eggs', 100, 1.234]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    63
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    64
Thus the simplest way of creating a list is typing out a sequence 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    65
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
    66
All the list items need not have the same data type.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
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
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
    71
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
    72
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
    73
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
    74
in lists. Thus lists can also contain other lists.  Example ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    75
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    76
      list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    77
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    78
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
    79
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
    80
first element, nonempty[1] the second element and so on and
208
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
    81
nonempty[3] the last element. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    82
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    83
	    nonempty[0] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    84
	    nonempty[1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    85
	    nonempty[3]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    86
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    87
We can also access the elememts from the end using negative indices ::
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[-1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    90
   nonempty[-2] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    91
   nonempty[-4]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    92
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    93
-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
    94
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
    95
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    96
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
    97
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    98
   nonempty.append('onemore') 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
    99
   nonempty
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   100
   nonempty.append(6) 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   101
   nonempty
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   102
   
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   103
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
   104
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   105
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   106
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   107
Using len function we can check the number of elements in the list
208
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   108
nonempty. In this case it being 6 ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   109
	 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   110
	 len(nonempty)
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   111
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   112
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   113
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   114
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
   115
There are two ways of doing it. One is by using index. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   116
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   117
      del(nonempty[1])
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   118
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   119
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   120
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   121
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
   122
list, 'eggs'. The other way is removing element by content. Lets say
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   123
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
   124
should be :: 
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   125
      
342
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   126
      nonempty.remove(100)
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   127
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   128
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
   129
experiment. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   130
342
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   131
	   nonempty.append('python') 
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   132
	   nonempty
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   133
	   nonempty.remove('python') 
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   134
	   nonempty
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   135
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   136
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
   137
thus remove removes the first occurence of the element in the sequence
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   138
and leaves others untouched.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   139
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   140
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   141
{{{Slide for Summary }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   142
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   143
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   144
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
   145
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   146
 * We learned how to create lists.  
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 208
diff changeset
   147
 * How to access lists.
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   148
 * Append elements to list.
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   149
 * Delete Element from list.  
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   150
 * And Checking list length.
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 208
diff changeset
   151
 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   152
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   153
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   154
{{{ Sponsored by Fossee Slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   155
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   156
This tutorial was created as a part of FOSSEE project.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   157
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   158
I hope you found this tutorial useful.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   159
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   160
Thank You
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   161
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   162
208
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   163
 * Author : Amit Sethi 
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   164
 * First Reviewer : 
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   165
 * Second Reviewer : Nishanth