basic_python/list_tuples.rst
author Madhusudan.C.S <madhusudancs@gmail.com>
Sat, 22 Aug 2009 20:25:24 +0530
changeset 12 ea1bc776f495
parent 11 edd18b1f5cb8
child 46 b4d0089294b9
permissions -rw-r--r--
Added a sub-section on Tuples.
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
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    40
~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
Concatenating
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
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
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
    45
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
  >>> a + ['foo', 12, 23.3432, 54]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
  [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
    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
  [54, 75, 23, 'write', 67, 'read']
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
  
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    52
~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
Slicing
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
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
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
    57
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
    58
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
    59
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
  >>> 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
    61
  >>> num[3:6]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
  [4, 5, 6]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
  >>> num[0:1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
  [1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
  >>> num[7:10]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
  [7, 8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
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
    69
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
    70
*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
    71
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
    72
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
    73
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
  >>> num[-3:-1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    75
  [7, 8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    76
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    77
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
    78
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
    79
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
    80
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
    81
the second index::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
  >>> num[:4]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
  [1, 2, 3, 4]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
  >>> num[7:]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
  [8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
  >>> num[-3:]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
  [7, 8, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
  >>> num[:]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
  [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
    91
  >>> num[4:9:3]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
  [5, 8]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    93
  >>> num[3::2]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    94
  [4, 6, 8]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    95
  >>> num[::4]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    96
  [1, 5, 9]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    97
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    98
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
    99
~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   100
Multiplication
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   101
~~~~~~~~~~~~~~
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   103
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   104
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
   105
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
  >>> [20] * 5
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   107
  [20, 20, 20, 20, 20]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
  >>> [42, 'Python', 54] * 3
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   109
  [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
   110
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   111
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   112
~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   113
Membership
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
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   116
**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
   117
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
   118
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
   119
operator::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   120
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   121
  >>> names = ['Guido', 'Alex', 'Tim']
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   122
  >>> 'Tim' in names
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   123
  True
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   124
  >>> 'Adam' in names
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   125
  False
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   126
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   127
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   128
~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   129
Length, Maximum and Minimum
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
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   132
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
   133
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
   134
element with the smallest value::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   135
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   136
  >>> 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
   137
  >>> len(num)
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   138
  7
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   139
  >>> max(num)
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   140
  67
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   141
  >>> min(num)
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   142
  1
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   143
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   144
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   145
~~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   146
Changing Elements
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
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   149
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
   150
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   151
  >>> a = [1, 3, 5, 7]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   152
  >>> a[2] = 9
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   153
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   154
  [1, 3, 9, 7]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   155
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   156
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   157
~~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   158
Deleting Elements
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
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   161
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
   162
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   163
  >>> 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
   164
  >>> del a[-2:]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   165
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   166
  [1, 3, 5, 7]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   167
  >>> del a[1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   168
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   169
  [1, 5, 7]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   170
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   171
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   172
~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   173
Assign to Slices
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
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   176
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
   177
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
   178
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   179
  >>> a = [2, 3, 4, 5]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   180
  >>> a[:2] = [0, 1]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   181
  [0, 1, 4, 5]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   182
  >>> a[2:2] = [2, 3]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   183
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   184
  [0, 1, 2, 3, 4, 5]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   185
  >>> a[2:4] = []
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   186
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   187
  [0, 1, 4, 5]
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   188
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   189
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
   190
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
   191
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
   192
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   193
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   194
None, Empty Lists, and Initialization
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   195
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   196
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   197
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
   198
[]. 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
   199
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
   200
no value::
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   201
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
  >>> a
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   204
  []
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   205
  >>> n = [None] * 10
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   206
  >>> n
000a414fc3b7 Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   207
  [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
   208
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   209
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   210
Nested Lists
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   211
~~~~~~~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   212
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   213
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
   214
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
   215
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
   216
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   217
  >>> 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
   218
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   219
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   220
List Methods
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   221
~~~~~~~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   222
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   223
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
   224
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
   225
method is called like::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   226
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   227
  object.method(arguments)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   228
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   229
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
   230
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
   231
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
   232
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
   233
the *List* methods.
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   234
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   235
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
   236
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   237
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   238
~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   239
append
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   242
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
   243
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   244
  >>> prime = [2, 3, 5]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   245
  >>> prime.append(7)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   246
  >>> prime
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   247
  [2, 3, 5, 7]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   248
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   249
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
   250
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   251
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   252
~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   253
count
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   256
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
   257
in a list::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   258
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   259
  >>> [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
   260
  3
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   261
  >>> 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
   262
  >>> tlst.count('Python')
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   263
  1
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   264
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   265
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   266
~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   267
extend
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   270
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
   271
as argument to it::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   272
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   273
  >>> a = [1, 2, 3]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   274
  >>> b = [4, 5, 6]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   275
  >>> a.extend(b)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   276
  [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
   277
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   278
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
   279
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
   280
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   281
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   282
~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   283
index
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   286
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
   287
specified as argument::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   288
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   289
  >>> 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
   290
  >>> a.index(4)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   291
  3
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   292
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   293
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   294
~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   295
insert
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   298
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
   299
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
   300
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   301
  >>> a = ['Python', 'is', 'cool']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   302
  >>> a.insert(2, 'so')
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   303
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   304
  ['Python', 'is', 'so', 'cool']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   305
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   306
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
   307
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   308
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   309
~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   310
pop
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   313
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
   314
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
   315
*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
   316
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   317
  >>> 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
   318
  >>> a.pop()
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   319
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   320
  5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   321
  >>> a.pop(2)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   322
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   323
  3
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   324
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   325
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
   326
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   327
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   328
~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   329
remove
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   332
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
   333
parameter::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   334
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   335
  >>> 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
   336
  >>> a.remove(2)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   337
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   338
  [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
   339
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   340
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   341
~~~~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   342
reverse
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   345
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
   346
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
   347
thing::
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   348
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   349
  >>> a = ['guido', 'alex', 'tim']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   350
  >>> a.reverse()
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   351
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   352
  ['tim', 'alex', 'guido']
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   353
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   354
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   355
~~~~
5
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   356
sort
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
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   359
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
   360
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
   361
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   362
  >>> 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
   363
  >>> a.sort()
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   364
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   365
  [1, 3, 4, 5, 7]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   366
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   367
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
   368
**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
   369
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
   370
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   371
  >>> 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
   372
  >>> b = sorted(a)
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   373
  >>> b
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   374
  [1, 3, 4, 5, 7]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   375
  >>> a
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   376
  [5, 1, 3, 7, 4]
dbc118349011 Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 4
diff changeset
   377
6
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   378
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   379
List Comprehensions
11
edd18b1f5cb8 Pushed the current subsections one level down.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 6
diff changeset
   380
~~~~~~~~~~~~~~~~~~~
6
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   381
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   382
List Comprehension is a convenvience utility provided by Python. It is a 
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   383
syntatic sugar to create *Lists*. Using *List Comprehensions* one can create
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   384
*Lists* from other type of sequential data structures or other *Lists* itself.
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   385
The syntax of *List Comprehensions* consists of a square brackets to indicate
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   386
the result is a *List* within which we include at least one **for** clause and
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   387
multiple **if** clauses. It will be more clear with an example::
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   388
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   389
  >>> num = [1, 2, 3]
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   390
  >>> sq = [x*x for x in num]
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   391
  >>> sq
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   392
  [1, 4, 9]
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   393
  >>> all_num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   394
  >>> even = [x for x in all_num if x%2 == 0]
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   395
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   396
The syntax used here is very clear from the way it is written. It can be 
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   397
translated into english as, "for each element x in the list all_num, 
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   398
if remainder of x divided by 2 is 0, add x to the list."
6921d82a80db Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 5
diff changeset
   399
12
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
Tuples
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   402
------
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   403
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   404
*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
   405
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
   406
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
   407
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
   408
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
   409
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
   410
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
   411
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   412
  >>> a = 1, 2, 3
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   413
  >>> a
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   414
  (1, 2, 3)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   415
  >>> b = 1,
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   416
  >>> b
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   417
  (1,)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   418
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   419
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
   420
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
   421
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
   422
output always has a parenthesis.
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   423
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   424
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
   425
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
   426
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
   427
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
   428
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   429
  >>> a = (1, 2)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   430
  >>> x, y = a
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   431
  >>> x
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   432
  1
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   433
  >>> y
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   434
  2
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   435
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   436
*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
   437
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
   438
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
   439
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
   440
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
   441
above example::
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   442
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   443
  >>> x, y = y, x
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   444
  >>> x
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   445
  2
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   446
  >>> y
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   447
  1
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   448
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   449
Common Tuple Operations
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   450
~~~~~~~~~~~~~~~~~~~~~~~
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   451
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   452
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
   453
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
   454
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   455
  * Indexing
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   456
  * Concatenating
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   457
  * Slicing
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   458
  * Membership
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   459
  * Multiplication
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   460
  * Length, Maximum, Minimum
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   461
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   462
The following examples illustrate the above operations::
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
  >>> a = (1, 2, 3, 4, 5, 6)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   465
  >>> a[5]
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   466
  6
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   467
  >>> b = (7, 8, 9)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   468
  >>> a + b
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   469
  (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
   470
  >>> a[3:5]
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   471
  (4, 5)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   472
  >>> 5 in a
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   473
  True
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   474
  >>> c = (1,)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   475
  >>> c * 5
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   476
  (1, 1, 1, 1, 1)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   477
  >>> len(a)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   478
  6
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   479
  >>> max(a)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   480
  6
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   481
  >>> min(a)
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   482
  1
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   483
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   484
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
   485
*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
   486
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   487
  * Changing elements
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   488
  * Deleting elements
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   489
  * Assigning to slices
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   490
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   491
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
   492
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
   493
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
   494
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
   495
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
   496
are helpful.
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   497
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   498
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   499
Conclusion
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   500
----------
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   501
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   502
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
   503
machinary required to work on *Lists* and *Tuples*. Topics like how to
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   504
iterate through these data structures will be introduced in the later
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   505
sections.
ea1bc776f495 Added a sub-section on Tuples.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 11
diff changeset
   506