getting-started-with-lists/script.rst
author Amit Sethi
Thu, 11 Nov 2010 17:28:23 +0530
changeset 468 ac1198488c0e
parent 466 00c1ba1cb9ef
child 490 2280bbdce638
permissions -rw-r--r--
Merging heads
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   :
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
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    27
.. #[[Anoop: Slides contain only outline and summary
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    28
374
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    29
Script
57d145c18ccd Adding metadata to getting started with lists.
amit
parents: 342
diff changeset
    30
------
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    31
 {{{ Show the slide containing title }}}
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    32
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    33
Hello friends and welcome to the tutorial on getting started with
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    34
lists.
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
 {{{ Show the slide containing the outline slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    37
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    38
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
    39
structure called lists.  We will learn ::
65e5e2362bc9 small change in getting_started_with_lists
amit
parents: 201
diff changeset
    40
 
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    41
 * How to create lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    42
 * Structure of lists
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    43
 * Access list elements
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    44
 * Append elements to lists
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    45
 * Delete elements from lists
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    46
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    47
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
    48
types. List is also a sequence data type, all the elements are in
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    49
order and the order has a meaning.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    50
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    51
.. #[[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
    52
   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
    53
   follow this.]]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    54
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    55
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
    56
shell type ::
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
   empty = [] 
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
    59
   type(empty)
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    60
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    61
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    62
This is an empty list without any elements.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    63
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    64
.. #[[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
    65
   subheadings, removing * Filled lists]]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    66
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    67
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
    68
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    69
     nonempty = ['spam', 'eggs', 100, 1.234]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    70
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    71
Thus the simplest way of creating a list is typing out a sequence 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    72
of comma-separated values (items) between square brackets. 
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    73
All the list items need not be of the same data type.
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
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    76
previous example 'spam' and 'eggs' are strings and 100 and 1.234 are
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
    77
integer and float. Thus we can put elements of heterogenous types in
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    78
lists including list itself.
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    79
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    80
.. #[[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
    81
   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
    82
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    83
Example ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    84
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    85
      listinlist=[[4,2,3,4],'and', 1, 2, 3, 4]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    86
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    87
We access list elements using the index. The index begins from 0. So
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    88
for list nonempty, nonempty[0] gives the first element, nonempty[1]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
    89
the second element and so on and nonempty[3] the last element. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    90
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    91
	    nonempty[0] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    92
	    nonempty[1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    93
	    nonempty[3]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    94
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    95
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
    96
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    97
%% %% 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
    98
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
    99
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
   100
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   101
.. #[[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
   102
   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
   103
   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
   104
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   105
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
   106
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   107
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   108
In python negative indices are used to access elements from the end::
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
   nonempty[-1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   111
   nonempty[-2] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   112
   nonempty[-4]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   113
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   114
-1 gives the last element which is the 4th element , -2 second to last
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   115
and -4 gives the fourth 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
   116
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   117
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
   118
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   119
   nonempty.append('onemore') 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   120
   nonempty
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   121
   nonempty.append(6) 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   122
   nonempty
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   123
   
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   124
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
   125
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   126
%% %% 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
   127
in the list,listinlist ?
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   128
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   129
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   130
%% %% 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
   131
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   132
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
   133
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   134
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
   135
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   136
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   137
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
   138
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   139
Using len function we can check the number of elements in the list
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   140
nonempty. In this case it 6 ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   141
	 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   142
	 len(nonempty)
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   143
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   144
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   145
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   146
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
   147
There are two ways of doing it. One is by using index. ::
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
      del(nonempty[1])
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   150
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   151
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   152
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   153
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
   154
list, 'eggs'. The other way is removing element by content. Lets say
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   155
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
   156
would be 
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   157
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   158
.. #[[Anoop: let x = [1,2,1,3]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   159
   	     now x.remove(x[2])
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   160
	     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
   161
	     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
   162
	     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
   163
	     changes]]
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   164
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   165
::
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
    nonempty.remove(100)
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
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
   170
experiment. ::
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   171
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   172
	   nonempty.append('spam') 
342
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   173
	   nonempty
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   174
	   nonempty.remove('spam') 
342
588b681e70c6 Added a few large questions and quickrefs
amit
parents: 320
diff changeset
   175
	   nonempty
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   176
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   177
If we check now we will see that the first occurence 'spam' is removed
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   178
thus remove removes the first occurence of the element in the sequence
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   179
and leaves others untouched.
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   180
466
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   181
One should remember this that while del removes by index number.
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   182
Remove , removes on the basis of content being passed so if ::
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   183
       
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   184
       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
   185
       del([k[2])
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
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
   188
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   189
      k.remove(x[2])
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   190
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   191
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
   192
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
   193
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   194
00c1ba1cb9ef Changes in getting started with list. Elaborating some points as suggested in review
Amit Sethi
parents: 441
diff changeset
   195
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   196
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   197
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   198
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   199
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   200
.. #[[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
   201
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   202
.. #[[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
   203
   add them]]
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   204
441
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   205
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
   206
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   207
%% %% 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
   208
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   209
%% %% 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
   210
430035b678f7 Exercises for getting started with lists and some changes based on review
Amit Sethi
parents: 430
diff changeset
   211
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
   212
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
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   215
{{{Slide for Summary }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   216
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   217
185
35a3811ca91e reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents: 182
diff changeset
   218
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
   219
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   220
 * 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
   221
 * How to access lists.
182
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   222
 * Append elements to list.
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   223
 * Delete Element from list.  
ddfb8b89f5bc some small changes , especially punctuation
amit
parents: 178
diff changeset
   224
 * And Checking list length.
320
223044cf254f Adding new format st-scripts with questions etc for basic-data-type and
amit
parents: 208
diff changeset
   225
 
201
6b1efb74d914 Applied the suggestion of Nishanth on getting started with lists and basicdatatypes(Partially)
amit
parents: 200
diff changeset
   226
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   227
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   228
{{{ show Sponsored by Fossee Slide }}}
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   229
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   230
This tutorial was created as a part of FOSSEE project.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   231
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   232
I hope you found this tutorial useful.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   233
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   234
Thank You
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   235
430
7b2275daab60 reviewed getting started with lists.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 415
diff changeset
   236
..
208
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   237
 * Author : Amit Sethi 
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   238
 * First Reviewer : 
0f78508a4478 Some punctuation changes
amit
parents: 204
diff changeset
   239
 * Second Reviewer : Nishanth