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