manipulating-lists/script.rst
author amit
Wed, 13 Oct 2010 17:32:59 +0530
changeset 324 4054b1a6392d
parent 312 8cb703eee88d
child 421 c4c5d1123f07
permissions -rw-r--r--
merging heads
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
.. Objectives
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
.. ----------
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
.. Clearly state the objectives of the LO (along with RBT level)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
.. Prerequisites
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     7
.. -------------
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
..   1. getting started with lists
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    10
..   2. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
..   3. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
     
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
.. Author              : Madhu
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
   Internal Reviewer   : 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
   External Reviewer   :
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    17
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
Script
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
------
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
{{{ Show the slide containing the title }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
Hello friends. Welcome to this spoken tutorial on Manipulating Lists. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
{{{ Show the slide containing the outline }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
We have already learnt a lot about Lists in Python. In this tutorial,
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
we will learn more about advanced features of Lists in Python. We will
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
see in detail how to concatenate two lists, slicing and striding of
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
lists, methods to sort and reverse the list.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
{{{ Shift to terminal and start ipython }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
To begin with let us start ipython, by typing::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
  ipython
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
on the terminal
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
We already know what Lists are in Python, how to access individual
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
elements in the list and some of the functions that can be run on the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
lists like max, min, sum len and so on. Now let us learn some of the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
basic operations that can be performed on Lists.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
We already know how to access individual elements in a List. But what
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
if we have a scenario where we need to get a part of the entire list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
or what we call as a slice of the list? Python supports slicing on
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
lists. Let us say I have the list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
  primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
To obtain the all the primes between 10 and 20 from the above list of
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
primes we say::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
  primes[4:8]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
This gives us all the elements in the list starting from the element
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
with the index 4 which is 11 in our list upto the element with index 8
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
in the list but not including the eigth element. So we obtain a slice
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
starting from 11 upto 19th. It is a very important to remember that
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
when ever we specify a range of elements in Python the start index is
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
included and end index is not included. So in the above case, 11 which
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
was the element with the index 4 was included but 23 which was the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
element with index 8 was excluded.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
Generalizing, we can obtain a slice of the list "p" from the index
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
"start" upto the index "end" but excluding "end" with the following
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
syntax
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
{{{ Show the slide containing p[start:stop] }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
By default the slice fetches all the elements between start and stop
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
including start but not stop. So as to say we obtain all the elements
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
between start and stop in steps of one. Python also provides us the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    76
functionality to specify the steps in which the slice must be
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
obtained. Say we have::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    78
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
  num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
If we want to obtain all the odd numbers less than 10 from the list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
"num" we have to start from element with index 1 upto the index 10 in
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
steps of 2::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
  num[1:10:2]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
So if we don't specify the step it is by default 1. Similary there are
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
default values for start and stop indices as well. If we don't specify
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
the start index it is implicitly taken as the first element of the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
  num[:10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
This gives us all the elements from the beginning upto the 10th
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
element but not including the 10th element in the list "num". Similary
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
if the stop index is not specified it is implicitly assumed to be the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
end of the list, including the last element of the list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
  num[10:]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
gives all the elements starting from the 10th element in the list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
"num" upto the final element including that last element. Now::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   104
  num[::2]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
gives us all the even numbers in the list "num".
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   108
The other basic operation that we can perform on list is concatenation
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
of two or more lists. We can combine two lists by using the "plus"
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
operator. Say we have
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
{{{ Read as you type }}}::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
  a = [1, 2, 3, 4]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
  b = [4, 5, 6, 7]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
  a + b
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
When we concatenate lists using the "plus" operator we get a new
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
list. We can store this list in a new variable::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
  c = a + b
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   122
  c
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   123
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
It is important to observe that the "plus" operator always returns a
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
new list without touching anything in the existing lists which are the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
operands of the concatenation operation.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
We know that list is a collection of data. Whenever we have a
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
collection we run into situations where we want to start the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   130
collection. Lists support sort method which sorts the list inplace::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   131
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   132
  a = [5, 1, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   133
  a.sort()
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   134
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
Now the contents of the list "a" will be::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
  a
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
  [1, 5, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
Since the sort method sorts the list inplace the original list we had
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
is overwritten or replaced. We have no way to obtain the original list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
back. One way to avoid this is to keep a copy of the original list in
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   143
another variable and run the sort method on the list. However Python
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
also provides a built-in function called sorted which sorts the list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
which is passed as an argument to it and returns a new sorted list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
  a = [5, 1, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
  sorted(a)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
  
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
We can store this sorted list another list variable::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
  sa = sorted(a)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
Similarly to perform certain operations on the list we would like to
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
reverse the list. Python provides reverse method which again reverses
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
the list inplace::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   157
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   158
  a = [1, 2, 3, 4, 5]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   159
  a.reverse()
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
reverses the list "a" and stores the reversed list inplace i.e. in "a"
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
itself. Lets see the list "a"::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
  a
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
  [5, 4, 3, 2, 1]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
But again the original list is lost. If we want to obtain the reverse
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
of a list keeping the original list intact we can use the Python
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
built-in function reversed. reversed function returns a new list which
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
is the reverse of the list which was passed as the argument to the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
reversed function::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
  a = [1, 2, 3, 4, 5]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
  reversed(a)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
We can also store this new reversed list in another list variable.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
{{{ Show summary slide }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
This brings us to the end of another session. In this tutorial session
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
we learnt
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
  * How to define strings
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
  * Different types of defining a string
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
  * String concatenation and repeatition
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
  * Accessing individual elements of the string
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
  * Immutability of strings
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   188
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
{{{ Show the "sponsored by FOSSEE" slide }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
Hope you have enjoyed and found it useful.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   194
Thank you!
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   195
 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196