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