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