basic_python/list_tuples.rst
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 07 Sep 2010 16:49:56 +0530
changeset 129 6e237b9442cd
parent 46 b4d0089294b9
permissions -rw-r--r--
Adding a huge question for lab exercise 4.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
Lists and Tuples
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
================
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
     4
Lists
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
     5
-----
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
     6
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
Python provides an intuitive way to represent a group items, called *Lists*. The
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
items of a *List* are called its elements. Unlike C/C++, elements can be of any
6
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
     9
type. A *List* is represented as a list of comma-sepated elements with square
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
    10
brackets around them::
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
  >>> a = [10, 'Python programming', 20.3523, 23, 3534534L]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
  [10, 'Python programming', 20.3523, 23, 3534534L]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
Common List Operations
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    18
~~~~~~~~~~~~~~~~~~~~~~
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    19
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    20
The following are some of the most commonly used operations on *Lists*.
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    23
~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
Indexing
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
Individual elements of a *List* can be accessed using an index to the element.
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
The indices start at 0. One can also access the elements of the *List* in reverse
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
using negative indices.::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
  >>> a[1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
  'Python programming'
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
  >>> a[-1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
  3534534L
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
It is important to note here that the last element of the *List* has an index of
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
-1.
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    39
~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
Concatenating
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
~~~~~~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
Two or more *Lists* can be concatenated using the + operator::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
  >>> a + ['foo', 12, 23.3432, 54]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
  [10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
  >>> [54, 75, 23] + ['write', 67, 'read']
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
  [54, 75, 23, 'write', 67, 'read']
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
  
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    51
~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
Slicing
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
A *List* can be sliced off to contain a subset of elements of the *List*. Slicing
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
can be done by using two indices separated by a colon, where the first index is
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
inclusive and the second index is exclusive. The resulting slice is also a *List*.::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    58
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
  >>> num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
  >>> num[3:6]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
  [4, 5, 6]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
  >>> num[0:1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
  [1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
  >>> num[7:10]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
  [7, 8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
The last example showed how to access last 3 elements of the *List*. There is a 
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
small catch here. The second index 10 actually refers to the 11th element of the
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    69
*List* which is still valid, even though it doesn't exist because the second 
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
index is exclusive and tells the Python interpreter to get the last element of
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    71
the *List*. But this can also be done in a much easier way using negative indices::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    72
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    73
  >>> num[-3:-1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
  [7, 8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    75
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    76
Excluding the first index implies that the slice must start at the beginning of 
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    77
the *List*, while excluding the second index includes all the elements till the
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    78
end of the *List*. A third parameter to a slice, which is implicitly taken as 1
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
is the step of the slice. It is specified as a value which follows a colon after
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
the second index::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    81
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
  >>> num[:4]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
  [1, 2, 3, 4]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
  >>> num[7:]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
  [8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
  >>> num[-3:]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
  [7, 8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
  >>> num[:]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
  [1, 2, 3, 4, 5, 6, 7, 8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
  >>> num[4:9:3]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    91
  [5, 8]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
  >>> num[3::2]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    93
  [4, 6, 8]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    94
  >>> num[::4]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    95
  [1, 5, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    96
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    97
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    98
~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    99
Multiplication
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   100
~~~~~~~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   101
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   102
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   103
A *List* can be multiplied with an integer to repeat itself::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   104
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   105
  >>> [20] * 5
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
  [20, 20, 20, 20, 20]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   107
  >>> [42, 'Python', 54] * 3
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
  [42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   109
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   110
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   111
~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   112
Membership
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   113
~~~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   114
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   115
**in** operator is used to find whether an element is part of the *List*. It
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   116
returns **True** if the element is present in the *List* or **False** if it is not 
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   117
present. Since this operator returns a Boolean value it is called a Boolean
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   118
operator::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   119
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   120
  >>> names = ['Guido', 'Alex', 'Tim']
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   121
  >>> 'Tim' in names
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   122
  True
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   123
  >>> 'Adam' in names
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   124
  False
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   125
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   126
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   127
~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   128
Length, Maximum and Minimum
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   129
~~~~~~~~~~~~~~~~~~~~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   130
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   131
Length of a *List* can be found out using the len function. The max function
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   132
returns the element with the largest value and the min function returns the 
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   133
element with the smallest value::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   134
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   135
  >>> num = [4, 1, 32, 12, 67, 34, 65]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   136
  >>> len(num)
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   137
  7
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   138
  >>> max(num)
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   139
  67
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   140
  >>> min(num)
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   141
  1
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   142
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   143
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   144
~~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   145
Changing Elements
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   146
~~~~~~~~~~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   147
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   148
Unlike Strings *Lists* are mutable, i.e. elements of a *List* can be manipulated::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   149
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   150
  >>> a = [1, 3, 5, 7]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   151
  >>> a[2] = 9
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   152
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   153
  [1, 3, 9, 7]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   154
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   155
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   156
~~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   157
Deleting Elements
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   158
~~~~~~~~~~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   159
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   160
An element or a slice of a *List* can be deleted by using the **del** statement::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   161
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   162
  >>> a = [1, 3, 5, 7, 9, 11]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   163
  >>> del a[-2:]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   164
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   165
  [1, 3, 5, 7]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   166
  >>> del a[1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   167
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   168
  [1, 5, 7]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   169
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   170
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   171
~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   172
Assign to Slices
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   173
~~~~~~~~~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   174
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   175
In the same way, values can be assigned to individual elements of the *List*, 
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   176
a *List* of elements can be assigned to a slice::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   177
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   178
  >>> a = [2, 3, 4, 5]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   179
  >>> a[:2] = [0, 1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   180
  [0, 1, 4, 5]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   181
  >>> a[2:2] = [2, 3]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   182
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   183
  [0, 1, 2, 3, 4, 5]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   184
  >>> a[2:4] = []
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   185
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   186
  [0, 1, 4, 5]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   187
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   188
The last two examples should be particularly noted carefully. The last but one
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   189
example insert elements or a list of elements into a *List* and the last example
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   190
deletes a list of elements from the *List*.
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   191
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   192
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   193
None, Empty Lists, and Initialization
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   194
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   195
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   196
An *Empty List* is a *List* with no elements and is simply represented as
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   197
[]. A *None List* is one with all elements in it being **None**. It serves
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   198
the purpose having a container list of some fixed number of elements with
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   199
no value::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   200
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   201
  >>> a = []
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   202
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   203
  []
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   204
  >>> n = [None] * 10
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   205
  >>> n
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   206
  [None, None, None, None, None, None, None, None, None, None]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   207
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   208
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   209
Nested Lists
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   210
~~~~~~~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   211
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   212
As mentioned earlier, a List can contain elements of any data type. This also
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   213
implies a *List* can have a *Lists* themselves as its elements. These are 
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   214
called as *Nested Lists*. There is no limit on the depth of the *Nested Lists*::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   215
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   216
  >>> a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   217
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   218
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   219
List Methods
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   220
~~~~~~~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   221
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   222
A method is a function that is coupled to an object. More about objects
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   223
and its methods are discussed in Advanced Python module. In general, a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   224
method is called like::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   225
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   226
  object.method(arguments)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   227
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   228
For now, it is enough to know that a list of elements is an object and
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   229
so *List* methods can be called upon them. Also some of the methods change
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   230
the *List* in-place, meaning it modifies the existing list instead of creating
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   231
a new one, while other methods don't. It must be noted as we run through
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   232
the *List* methods.
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   233
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   234
Some of the most commonly used *List* methods are as follows:
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   235
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   236
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   237
~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   238
append
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   239
~~~~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   240
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   241
The *append* method is used to append an object at the end of the list::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   242
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   243
  >>> prime = [2, 3, 5]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   244
  >>> prime.append(7)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   245
  >>> prime
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   246
  [2, 3, 5, 7]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   247
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   248
It is important to note that append changes the *List* in-place.
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   249
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   250
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   251
~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   252
count
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   253
~~~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   254
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   255
The *count* method returns the number of occurences of a particular element
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   256
in a list::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   257
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   258
  >>> [1, 4, 4, 9, 9, 9].count(9)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   259
  3
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   260
  >>> tlst = ['Python', 'is', 'a', 'beautiful', 'language']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   261
  >>> tlst.count('Python')
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   262
  1
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   263
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   264
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   265
~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   266
extend
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   267
~~~~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   268
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   269
The *extend* method extends the list on which it is called by the list supplied
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   270
as argument to it::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   271
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   272
  >>> a = [1, 2, 3]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   273
  >>> b = [4, 5, 6]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   274
  >>> a.extend(b)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   275
  [1, 2, 3, 4, 5, 6]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   276
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   277
This is an in-place method. This method is equivalent to using the + operator, but
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   278
using the + operator returns a new list.
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   279
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   280
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   281
~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   282
index
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   283
~~~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   284
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   285
The *index* method returns the index position of the element in the list 
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   286
specified as argument::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   287
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   288
  >>> a = [1, 2, 3, ,4, 5]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   289
  >>> a.index(4)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   290
  3
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   291
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   292
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   293
~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   294
insert
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   295
~~~~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   296
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   297
The *insert* method is used to insert an element specified as the second 
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   298
argument to the list at the position specified by the first argument::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   299
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   300
  >>> a = ['Python', 'is', 'cool']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   301
  >>> a.insert(2, 'so')
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   302
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   303
  ['Python', 'is', 'so', 'cool']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   304
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   305
The *insert* method changes the *List* in-place.
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   306
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   307
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   308
~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   309
pop
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   310
~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   311
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   312
The *pop* method removes an element from the list. The index position
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   313
of the element to be removed can be specified as an argument to the
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   314
*pop* method, if not it removes the last element by default::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   315
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   316
  >>> a = [1, 2, 3, 4, 5]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   317
  >>> a.pop()
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   318
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   319
  5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   320
  >>> a.pop(2)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   321
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   322
  3
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   323
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   324
The *pop* method changes the *List* in-place.
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   325
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   326
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   327
~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   328
remove
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   329
~~~~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   330
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   331
The *remove* method removes the first occurence of an element supplied as a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   332
parameter::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   333
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   334
  >>> a = [1, 2, 3, 4, 2, 5, 2]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   335
  >>> a.remove(2)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   336
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   337
  [1, 3, 4, 2, 5, 2]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   338
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   339
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   340
~~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   341
reverse
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   342
~~~~~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   343
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   344
The *reverse* method reverses elements in the list. It is important to note
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   345
here that *reverse* method changes the list in-place and doesn't return any
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   346
thing::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   347
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   348
  >>> a = ['guido', 'alex', 'tim']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   349
  >>> a.reverse()
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   350
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   351
  ['tim', 'alex', 'guido']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   352
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   353
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   354
~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   355
sort
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   356
~~~~
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   357
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   358
The *sort* method is used to sort the elements of the list. The *sort* method
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   359
also sorts in-place and does not return anything::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   360
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   361
  >>> a = [5, 1, 3, 7, 4]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   362
  >>> a.sort()
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   363
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   364
  [1, 3, 4, 5, 7]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   365
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   366
In addition to the sort method on a *List* object we can also use the built-in
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   367
**sorted** function. This function takes the *List* as a parameter and returns
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   368
a sorted copy of the list. However the original list is left intact::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   369
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   370
  >>> a = [5, 1, 3, 7, 4]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   371
  >>> b = sorted(a)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   372
  >>> b
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   373
  [1, 3, 4, 5, 7]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   374
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   375
  [5, 1, 3, 7, 4]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   376
6
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   377
12
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   378
Tuples
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   379
------
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   380
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   381
*Tuples* are sequences just like *Lists*, but they are immutable. In other
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   382
words *Tuples* provides a way to represent a group of items, where the group
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   383
of items cannot be changed in any way. The syntax of a *Tuple* is also very
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   384
similar to *List*. A *Tuple* is represented with the list of items, called
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   385
elements of the *Tuple* separated by comma, with the entire list being enclosed
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   386
in parenthesis. It is not compulsory to use parenthesis around a *Tuple* but
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   387
it may be necessary in some of the cases::
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   388
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   389
  >>> a = 1, 2, 3
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   390
  >>> a
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   391
  (1, 2, 3)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   392
  >>> b = 1,
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   393
  >>> b
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   394
  (1,)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   395
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   396
It is interesting to note the second example. Just a value followed by a comma
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   397
automatically makes that an element of a *Tuple* with only one element. It is
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   398
also important to note that, irrespective of input having a parenthesis, the
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   399
output always has a parenthesis.
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   400
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   401
The first example is also known as *Tuple packing*, because values are being
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   402
packed into a tuple. It is also possible to do *Tuple unpacking* which is more
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   403
interesting. It is better to understand that by example. Say we have a 
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   404
co-ordinate pair from which we need to separate x and y co-ordinates::
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   405
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   406
  >>> a = (1, 2)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   407
  >>> x, y = a
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   408
  >>> x
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   409
  1
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   410
  >>> y
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   411
  2
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   412
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   413
*Tuple unpacking* also has several other use-cases of which the most interesting
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   414
one is to swap the values of two variables. Using programming languages like C
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   415
would require anywhere around 10 lines of code and an extra temporary variable
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   416
to do this (including all the #include stuff). Python does it in the most
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   417
intuitive way in just one line. Say we want to swap the co-ordinates in the
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   418
above example::
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   419
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   420
  >>> x, y = y, x
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   421
  >>> x
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   422
  2
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   423
  >>> y
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   424
  1
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   425
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   426
Common Tuple Operations
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   427
~~~~~~~~~~~~~~~~~~~~~~~
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   428
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   429
There is no need to introduce all the *Tuple* operations again, since *Tuples*
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   430
support the following operations that *List* supports in exactly the same way:
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   431
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   432
  * Indexing
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   433
  * Concatenating
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   434
  * Slicing
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   435
  * Membership
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   436
  * Multiplication
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   437
  * Length, Maximum, Minimum
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   438
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   439
The following examples illustrate the above operations::
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   440
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   441
  >>> a = (1, 2, 3, 4, 5, 6)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   442
  >>> a[5]
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   443
  6
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   444
  >>> b = (7, 8, 9)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   445
  >>> a + b
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   446
  (1, 2, 3, 4, 5, 6, 7, 8, 9)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   447
  >>> a[3:5]
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   448
  (4, 5)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   449
  >>> 5 in a
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   450
  True
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   451
  >>> c = (1,)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   452
  >>> c * 5
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   453
  (1, 1, 1, 1, 1)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   454
  >>> len(a)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   455
  6
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   456
  >>> max(a)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   457
  6
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   458
  >>> min(a)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   459
  1
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   460
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   461
However the following *List* operations are not supported by *Tuples* because
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   462
*Tuples* cannot be changed once they are created:
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   463
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   464
  * Changing elements
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   465
  * Deleting elements
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   466
  * Assigning to slices
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   467
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   468
Similarity to *Lists* leads to the questions like, why not *Lists* only? Why do
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   469
we even want *Tuples*? Can we do the same with *Lists*? And the answer is **Yes**
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   470
we can do it, but *Tuples* are helpful at times, like we can return Tuples from
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   471
functions. They are also returned by some built-in functions and methods. And
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   472
also there are some use cases like co-ordinate among other things. So *Tuples*
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   473
are helpful.
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   474
46
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   475
Additional Syntax
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   476
-----------------
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   477
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   478
The following additional syntax are introduced to make it easier to operate on
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   479
*Lists*.
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   480
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   481
range()
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   482
~~~~~~~
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   483
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   484
The *range* function takes at least one argument and 2 additional optional
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   485
arguments. If two or more arguments are specified, the range function returns
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   486
a list of natural numbers starting from the first argument passed to it to the
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   487
second argument. The third argument, if specified is used as a step. Suppose
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   488
only one argument is specified, then *range* function returns a list of natural
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   489
numbers starting from 0 upto the argument specified::
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   490
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   491
  >>> range(5, 10, 2)
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   492
  [5, 7, 9]
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   493
  >>> range(2, 15)
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   494
  [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   495
  >>> range(12)
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   496
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   497
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   498
for
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   499
~~~
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   500
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   501
The **for** keyword is used as a part of the looping construct. Unlike for loops
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   502
in other languages, Python's for is used to iterate through the elements of 
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   503
sequences like *Lists*, *Tuples*, *Dictionaries*, etc. The syntax of the for loop
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   504
consists of **for**, followed by a variable to hold the individual or the current
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   505
element of the list during iteration and **in**, followed by the sequence and a
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   506
semicolon(':') The next line which is part of the **for** loop, i.e the statements
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   507
that are part of the loop should start with a new intend::
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   508
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   509
  >>> names = ['Guido', 'Alex', 'Tim']
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   510
  >>> for name in names:
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   511
  ...   print "Name =", name
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   512
  ... 
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   513
  Name = Guido
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   514
  Name = Alex
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   515
  Name = Tim
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   516
12
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   517
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   518
Conclusion
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   519
----------
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   520
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   521
This section on *Lists* and *Tuples* introduces almost all the necessary 
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   522
machinary required to work on *Lists* and *Tuples*. Topics like how to
46
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   523
use these data structures in bigger more useful programs will be introduced
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   524
in the subsequent chapters.
b4d0089294b9 Added new file and stubs for interim assessment.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
   525
12
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   526