getting-started-with-lists/script.rst
author anand
Fri, 12 Nov 2010 00:40:47 +0530
changeset 491 ebfe3a675882
parent 490 2280bbdce638
permissions -rw-r--r--
checklist ok for `getting started with lists`
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
     
415
8afa31ae6129 small changes to getting started with lists
Amit Sethi
parents: 374
diff changeset
    22
.. Author              : Amit 
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    23
   Internal Reviewer   : Anoop Jacob Thomas <anoop@fossee.in>
374
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    24
   External Reviewer   :
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    25
   Language Reviewer   : Bhanukiran
491
ebfe3a675882 checklist ok for `getting started with lists`
anand
parents: 490
diff changeset
    26
   Checklist OK?       : <12-11-2010, Anand, OK> [2010-10-05]
374
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    27
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    28
.. #[[Anoop: Slides contain only outline and summary
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    29
374
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    30
Script
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    31
------
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    32
 {{{ Show the slide containing title }}}
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    33
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    34
Hello friends and welcome to the tutorial on getting started with
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    35
lists.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    36
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    37
 {{{ Show the slide containing the outline slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    38
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    39
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
    40
structure called lists.  We will learn ::
65e5e2362bc9 small change in getting_started_with_lists
amit
parents: 201
diff changeset
    41
 
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    42
 * How to create lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    43
 * Structure of lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    44
 * Access list elements
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    45
 * Append elements to lists
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    46
 * Delete elements from lists
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    47
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    48
List is a compound data type, it can contain data of mutually
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    49
different datatypes. List is also a sequence data type, all the
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    50
elements are arranged in a given order.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    51
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    52
.. #[[Anoop: "all the elements are in order and **there** order has a
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    53
   meaning." - I guess something is wrong here, I am not able to
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    54
   follow this.]]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    55
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    56
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
    57
shell type ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    58
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    59
   empty = [] 
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    60
   type(empty)
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    61
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    62
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    63
This is an empty list without any elements.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    64
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    65
.. #[[Anoop: the document has to be continous, without any
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    66
   subheadings, removing * Filled lists]]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    67
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    68
Lets now see how to define a non-empty list. We do it as,::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    69
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    70
     nonempty = ['spam', 'eggs', 100, 1.234]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    71
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    72
Thus the simplest way of creating a list is typing out a sequence 
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    73
of comma-separated values (or items) between two square brackets. 
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    74
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    75
As we can see lists can contain different kinds of data. In the
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    76
previous example 'spam' and 'eggs' are strings whereas 100 and 1.234 are
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    77
integer and float respectively. Thus we can put elements of different types in
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    78
lists including lists itself. This property makes lists heterogeneous
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    79
data structures.
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    80
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    81
.. #[[Anoop: the sentence "Thus list themselves can be one of the
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    82
   element types possible in lists" is not clear, rephrase it.]]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    83
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    84
Example ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    85
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    86
      listinlist=[[4,2,3,4],'and', 1, 2, 3, 4]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    87
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    88
We access an element of a list using its corresponding index. Index of
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    89
the first element of a list is 0. So for the list nonempty, nonempty[0] 
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    90
gives the first element, nonempty[1] the second element and so on and 
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
    91
nonempty[3] the last element. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    92
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    93
	    nonempty[0] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    94
	    nonempty[1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    95
	    nonempty[3]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    96
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    97
Following is an exercise that you must do. 
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    98
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    99
%% %% What happens when you do nonempty[-1]. 
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   100
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   101
Please, pause the video here. Do the exercise and then continue.  
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   102
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   103
.. #[[Anoop: was negative indices introduced earlier, if not may be we
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   104
   can ask them to try out nonempty[-1] and see what happens and then
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   105
   tell that it gives the last element in the list.]]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   106
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   107
As you can see you get the last element which is 1.234.
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   108
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   109
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   110
In python negative indices are used to access elements from the end::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   111
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   112
   nonempty[-1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   113
   nonempty[-2] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   114
   nonempty[-4]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   115
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   116
-1 gives the last element which is the 4th element , -2 second to last
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
   117
and -4 gives the fourth from the last which, in this case,  is the first element.
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   118
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
   119
We can append elements to the end of a list using the method append. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   120
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   121
   nonempty.append('onemore') 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   122
   nonempty
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   123
   nonempty.append(6) 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   124
   nonempty
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   125
   
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   126
Following are  exercises that you must do. 
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   127
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   128
%% %% What is the syntax to get the element 'and' 
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   129
in the list,listinlist ?
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   130
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   131
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   132
%% %% How would you get 'and' using negative indices?
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   133
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   134
Please, pause the video here. Do the exercise and then continue.  
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   135
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   136
The solution is on your screen
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   137
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   138
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
   139
As we can see nonempty is appended with 'onemore' and 6 at the end.
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   140
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   141
Using len function we can check the number of elements in the list
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
   142
nonempty. In this case it is 6 ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   143
	 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   144
	 len(nonempty)
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   145
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   146
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   147
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   148
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
   149
There are two ways of doing it. One is by using index. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   150
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   151
      del(nonempty[1])
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   152
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   153
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   154
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   155
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
   156
list, 'eggs'. The other way is removing element by content. Lets say
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   157
one wishes to delete 100 from nonempty list the syntax of the command
466
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   158
would be 
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   159
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   160
.. #[[Anoop: let x = [1,2,1,3]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   161
   	     now x.remove(x[2])
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   162
	     still x is [2,1,3] so that is not the way to remove
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   163
	     element by index, it removed first occurrence of 1(by
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   164
	     content) and not based on index, so make necessary
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   165
	     changes]]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   166
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   167
::
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   168
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   169
    nonempty.remove(100)
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   170
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   171
but what if there were two 100's. To check that lets do a small
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   172
experiment. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   173
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   174
	   nonempty.append('spam') 
342
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   175
	   nonempty
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   176
	   nonempty.remove('spam') 
342
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   177
	   nonempty
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   178
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   179
If we check now we will see that the first occurence 'spam' is removed
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
   180
and therefore `remove` removes the first occurence of the element in the sequence
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   181
and leaves others untouched.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   182
490
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
   183
One should remember this that while del removes by index number,
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
   184
`remove` removes on the basis of content being passed on. For instance
2280bbdce638 Language check done for `getting started with lists`
bhanu
parents: 466
diff changeset
   185
if ::
466
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   186
       
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   187
       k = [1,2,1,3] 
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   188
       del([k[2])
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   189
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   190
gives us [1,2,3]. ::
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   191
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   192
      k.remove(x[2])
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   193
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   194
will give us [2,1,3]. Since it deletes the first occurence of what is
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   195
returned by x[2] which is 1.      
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   196
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   197
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   198
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   199
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   200
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   201
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   202
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   203
.. #[[Anoop: does it have two spams or two pythons?]]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   204
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   205
.. #[[Anoop: there are no exercises/solved problems in this script,
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   206
   add them]]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   207
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   208
Following are  exercises that you must do. 
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   209
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   210
%% %% Remove the third element from the list, listinlist.   
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   211
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   212
%% %% Remove 'and' from the list, listinlist.
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   213
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   214
Please, pause the video here. Do the exercise and then continue.  
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   215
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   216
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   217
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   218
{{{Slide for Summary }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   219
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   220
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   221
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
   222
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   223
 * 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
   224
 * How to access lists.
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   225
 * Append elements to list.
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   226
 * Delete Element from list.  
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   227
 * And Checking list length.
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 208
diff changeset
   228
 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   229
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   230
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   231
{{{ show Sponsored by Fossee Slide }}}
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   232
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   233
This tutorial was created as a part of FOSSEE project.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   234
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   235
I hope you found this tutorial useful.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   236
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   237
Thank You
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   238
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   239
..
208
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   240
 * Author : Amit Sethi 
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   241
 * First Reviewer : 
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   242
 * Second Reviewer : Nishanth