web/html/backup/ch02-list_tuples.html
author amit@thunder
Mon, 25 Jan 2010 18:56:45 +0530
changeset 0 8083d21c0020
permissions -rwxr-xr-x
The first commit of all the required files for the review app
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
<html>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     2
<head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     4
<title>Lists and Tuples</title>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     5
<link rel="stylesheet" href="hgbook.css" type="text/css">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     6
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     7
<link rel="home" href="index.html" title="Chapter 1. List and Tuples">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
<link rel="up" href="index.html" title="Chapter 1. List and Tuples">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
<link rel="prev" href="index.html" title="Chapter 1. List and Tuples">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
</head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    12
<div class="navheader">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    13
<table width="100%" summary="Navigation header">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
<tr><th colspan="3" align="center">Lists and Tuples</th></tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
<td width="20%" align="left">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
<a accesskey="p" href="index.html">Prev</a> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
<th width="60%" align="center"> </th>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
<td width="20%" align="right"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    20
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
</table>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    23
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
<div class="article" title="Lists and Tuples">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
<div class="titlepage">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
<div><div><h2 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
<a name="id2609714"></a>Lists and Tuples</h2></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
<div class="toc">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
<p><b>Table of Contents</b></p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    32
<dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
<dt><span class="section"><a href="ar01.html#common-list-operations">1. Common List Operations</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
<dd><dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    35
<dt><span class="section"><a href="ar01.html#indexing">1.1. Indexing</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
<dt><span class="section"><a href="ar01.html#concatenating">1.2. Concatenating</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
<dt><span class="section"><a href="ar01.html#slicing">1.3. Slicing</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
<dt><span class="section"><a href="ar01.html#multiplication">1.4. Multiplication</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    39
<dt><span class="section"><a href="ar01.html#membership">1.5. Membership</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    40
<dt><span class="section"><a href="ar01.html#length-maximum-and-minimum">1.6. Length, Maximum and Minimum</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    41
<dt><span class="section"><a href="ar01.html#changing-elements">1.7. Changing Elements</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    42
<dt><span class="section"><a href="ar01.html#deleting-elements">1.8. Deleting Elements</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    43
<dt><span class="section"><a href="ar01.html#assign-to-slices">1.9. Assign to Slices</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    44
</dl></dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    45
<dt><span class="section"><a href="ar01.html#none-empty-lists-and-initialization">2. None, Empty Lists, and Initialization</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    46
<dt><span class="section"><a href="ar01.html#nested-lists">3. Nested Lists</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    47
<dt><span class="section"><a href="ar01.html#list-methods">4. List Methods</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    48
<dd><dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    49
<dt><span class="section"><a href="ar01.html#append">4.1. append</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    50
<dt><span class="section"><a href="ar01.html#count">4.2. count</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    51
<dt><span class="section"><a href="ar01.html#extend">4.3. extend</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    52
<dt><span class="section"><a href="ar01.html#index">4.4. index</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    53
<dt><span class="section"><a href="ar01.html#insert">4.5. insert</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    54
<dt><span class="section"><a href="ar01.html#pop">4.6. pop</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    55
<dt><span class="section"><a href="ar01.html#remove">4.7. remove</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    56
<dt><span class="section"><a href="ar01.html#reverse">4.8. reverse</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    57
<dt><span class="section"><a href="ar01.html#sort">4.9. sort</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    58
</dl></dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    59
<dt><span class="section"><a href="ar01.html#tuples">5. Tuples</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    60
<dd><dl><dt><span class="section"><a href="ar01.html#common-tuple-operations">5.1. Common Tuple Operations</a></span></dt></dl></dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    61
<dt><span class="section"><a href="ar01.html#additional-syntax">6. Additional Syntax</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    62
<dd><dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    63
<dt><span class="section"><a href="ar01.html#range">6.1. range()</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    64
<dt><span class="section"><a href="ar01.html#for">6.2. for</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    65
</dl></dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    66
<dt><span class="section"><a href="ar01.html#conclusion">7. Conclusion</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    67
</dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    68
</div>Lists
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    69
  <p><a name="x_1"></a>Python provides an intuitive way to represent a group items, called 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    70
    <span class="emphasis"><em>Lists</em></span>. The items of a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    71
    <span class="emphasis"><em>List</em></span> are called its elements. Unlike C/C++, elements can be of any type. A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    72
    <span class="emphasis"><em>List</em></span> is represented as a list of comma-sepated elements with square brackets around them:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    73
  </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    74
<pre class="programlisting">&gt;&gt;&gt; a = [10, 'Python programming', 20.3523, 23, 3534534L]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    75
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    76
[10, 'Python programming', 20.3523, 23, 3534534L]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    77
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    78
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    79
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    80
<div class="section" title="1. Common List Operations">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    81
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    82
<a name="common-list-operations"></a>1. Common List Operations</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    83
<p><a name="x_2"></a>The following are some of the most commonly used operations on 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    84
      <span class="emphasis"><em>Lists</em></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    85
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    86
<div class="section" title="1.1. Indexing">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    87
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    88
<a name="indexing"></a>1.1. Indexing</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    89
<p><a name="x_3"></a>Individual elements of a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    90
        <span class="emphasis"><em>List</em></span> can be accessed using an index to the element. The indices start at 0. One can also access the elements of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    91
        <span class="emphasis"><em>List</em></span> in reverse using negative indices.:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    92
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    93
<pre class="programlisting">&gt;&gt;&gt; a[1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    94
'Python programming'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    95
&gt;&gt;&gt; a[-1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    96
3534534L
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    97
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    98
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    99
<p><a name="x_4"></a>It is important to note here that the last element of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   100
        <span class="emphasis"><em>List</em></span> has an index of -1.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   101
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   102
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   103
<div class="section" title="1.2. Concatenating">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   104
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   105
<a name="concatenating"></a>1.2. Concatenating</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   106
<p><a name="x_5"></a>Two or more 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   107
        <span class="emphasis"><em>Lists</em></span> can be concatenated using the + operator:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   108
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   109
<pre class="programlisting">&gt;&gt;&gt; a + ['foo', 12, 23.3432, 54]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   110
[10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   111
&gt;&gt;&gt; [54, 75, 23] + ['write', 67, 'read']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   112
[54, 75, 23, 'write', 67, 'read']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   113
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   114
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   115
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   116
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   117
<div class="section" title="1.3. Slicing">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   118
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   119
<a name="slicing"></a>1.3. Slicing</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   120
<p><a name="x_6"></a>A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   121
        <span class="emphasis"><em>List</em></span> can be sliced off to contain a subset of elements of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   122
        <span class="emphasis"><em>List</em></span>. Slicing can be done by using two indices separated by a colon, where the first index is inclusive and the second index is exclusive. The resulting slice is also a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   123
        <span class="emphasis"><em>List</em></span>.:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   124
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   125
<pre class="programlisting">&gt;&gt;&gt; num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   126
&gt;&gt;&gt; num[3:6]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   127
[4, 5, 6]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   128
&gt;&gt;&gt; num[0:1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   129
[1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   130
&gt;&gt;&gt; num[7:10]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   131
[7, 8, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   132
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   133
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   134
<p><a name="x_7"></a>The last example showed how to access last 3 elements of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   135
        <span class="emphasis"><em>List</em></span>. There is a small catch here. The second index 10 actually refers to the 11th element of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   136
        <span class="emphasis"><em>List</em></span> which is still valid, even though it doesn't exist because the second index is exclusive and tells the Python interpreter to get the last element of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   137
        <span class="emphasis"><em>List</em></span>. But this can also be done in a much easier way using negative indices:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   138
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   139
<pre class="programlisting">&gt;&gt;&gt; num[-3:-1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   140
[7, 8, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   141
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   142
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   143
<p><a name="x_8"></a>Excluding the first index implies that the slice must start at the beginning of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   144
        <span class="emphasis"><em>List</em></span>, while excluding the second index includes all the elements till the end of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   145
        <span class="emphasis"><em>List</em></span>. A third parameter to a slice, which is implicitly taken as 1 is the step of the slice. It is specified as a value which follows a colon after the second index:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   146
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   147
<pre class="programlisting">&gt;&gt;&gt; num[:4]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   148
[1, 2, 3, 4]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   149
&gt;&gt;&gt; num[7:]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   150
[8, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   151
&gt;&gt;&gt; num[-3:]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   152
[7, 8, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   153
&gt;&gt;&gt; num[:]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   154
[1, 2, 3, 4, 5, 6, 7, 8, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   155
&gt;&gt;&gt; num[4:9:3]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   156
[5, 8]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   157
&gt;&gt;&gt; num[3::2]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   158
[4, 6, 8]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   159
&gt;&gt;&gt; num[::4]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   160
[1, 5, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   161
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   162
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   163
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   164
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   165
<div class="section" title="1.4. Multiplication">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   166
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   167
<a name="multiplication"></a>1.4. Multiplication</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   168
<p><a name="x_9"></a>A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   169
        <span class="emphasis"><em>List</em></span> can be multiplied with an integer to repeat itself:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   170
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   171
<pre class="programlisting">&gt;&gt;&gt; [20] * 5
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   172
[20, 20, 20, 20, 20]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   173
&gt;&gt;&gt; [42, 'Python', 54] * 3
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   174
[42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   175
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   176
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   177
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   178
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   179
<div class="section" title="1.5. Membership">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   180
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   181
<a name="membership"></a>1.5. Membership</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   182
<p><a name="x_a"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   183
        <span class="strong"><strong>in</strong></span> operator is used to find whether an element is part of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   184
        <span class="emphasis"><em>List</em></span>. It returns 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   185
        <span class="strong"><strong>True</strong></span> if the element is present in the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   186
        <span class="emphasis"><em>List</em></span> or 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   187
        <span class="strong"><strong>False</strong></span> if it is not present. Since this operator returns a Boolean value it is called a Boolean operator:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   188
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   189
<pre class="programlisting">&gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   190
&gt;&gt;&gt; 'Tim' in names
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   191
True
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   192
&gt;&gt;&gt; 'Adam' in names
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   193
False
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   194
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   195
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   196
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   197
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   198
<div class="section" title="1.6. Length, Maximum and Minimum">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   199
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   200
<a name="length-maximum-and-minimum"></a>1.6. Length, Maximum and Minimum</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   201
<p><a name="x_b"></a>Length of a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   202
        <span class="emphasis"><em>List</em></span> can be found out using the len function. The max function returns the element with the largest value and the min function returns the element with the smallest value:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   203
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   204
<pre class="programlisting">&gt;&gt;&gt; num = [4, 1, 32, 12, 67, 34, 65]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   205
&gt;&gt;&gt; len(num)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   206
7
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   207
&gt;&gt;&gt; max(num)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   208
67
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   209
&gt;&gt;&gt; min(num)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   210
1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   211
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   212
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   213
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   214
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   215
<div class="section" title="1.7. Changing Elements">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   216
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   217
<a name="changing-elements"></a>1.7. Changing Elements</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   218
<p><a name="x_c"></a>Unlike Strings 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   219
        <span class="emphasis"><em>Lists</em></span> are mutable, i.e. elements of a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   220
        <span class="emphasis"><em>List</em></span> can be manipulated:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   221
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   222
<pre class="programlisting">&gt;&gt;&gt; a = [1, 3, 5, 7]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   223
&gt;&gt;&gt; a[2] = 9
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   224
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   225
[1, 3, 9, 7]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   226
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   227
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   228
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   229
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   230
<div class="section" title="1.8. Deleting Elements">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   231
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   232
<a name="deleting-elements"></a>1.8. Deleting Elements</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   233
<p><a name="x_d"></a>An element or a slice of a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   234
        <span class="emphasis"><em>List</em></span> can be deleted by using the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   235
        <span class="strong"><strong>del</strong></span> statement:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   236
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   237
<pre class="programlisting">&gt;&gt;&gt; a = [1, 3, 5, 7, 9, 11]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   238
&gt;&gt;&gt; del a[-2:]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   239
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   240
[1, 3, 5, 7]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   241
&gt;&gt;&gt; del a[1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   242
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   243
[1, 5, 7]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   244
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   245
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   246
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   247
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   248
<div class="section" title="1.9. Assign to Slices">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   249
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   250
<a name="assign-to-slices"></a>1.9. Assign to Slices</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   251
<p><a name="x_e"></a>In the same way, values can be assigned to individual elements of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   252
        <span class="emphasis"><em>List</em></span>, a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   253
        <span class="emphasis"><em>List</em></span> of elements can be assigned to a slice:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   254
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   255
<pre class="programlisting">&gt;&gt;&gt; a = [2, 3, 4, 5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   256
&gt;&gt;&gt; a[:2] = [0, 1]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   257
[0, 1, 4, 5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   258
&gt;&gt;&gt; a[2:2] = [2, 3]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   259
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   260
[0, 1, 2, 3, 4, 5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   261
&gt;&gt;&gt; a[2:4] = []
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   262
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   263
[0, 1, 4, 5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   264
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   265
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   266
<p><a name="x_f"></a>The last two examples should be particularly noted carefully. The last but one example insert elements or a list of elements into a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   267
        <span class="emphasis"><em>List</em></span> and the last example deletes a list of elements from the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   268
        <span class="emphasis"><em>List</em></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   269
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   270
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   271
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   272
<div class="section" title="2. None, Empty Lists, and Initialization">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   273
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   274
<a name="none-empty-lists-and-initialization"></a>2. None, Empty Lists, and Initialization</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   275
<p><a name="x_10"></a>An 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   276
      <span class="emphasis"><em>Empty List</em></span> is a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   277
      <span class="emphasis"><em>List</em></span> with no elements and is simply represented as []. A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   278
      <span class="emphasis"><em>None List</em></span> is one with all elements in it being 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   279
      <span class="strong"><strong>None</strong></span>. It serves the purpose having a container list of some fixed number of elements with no value:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   280
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   281
<pre class="programlisting">&gt;&gt;&gt; a = []
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   282
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   283
[]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   284
&gt;&gt;&gt; n = [None] * 10
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   285
&gt;&gt;&gt; n
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   286
[None, None, None, None, None, None, None, None, None, None]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   287
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   288
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   289
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   290
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   291
<div class="section" title="3. Nested Lists">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   292
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   293
<a name="nested-lists"></a>3. Nested Lists</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   294
<p><a name="x_11"></a>As mentioned earlier, a List can contain elements of any data type. This also implies a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   295
      <span class="emphasis"><em>List</em></span> can have a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   296
      <span class="emphasis"><em>Lists</em></span> themselves as its elements. These are called as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   297
      <span class="emphasis"><em>Nested Lists</em></span>. There is no limit on the depth of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   298
      <span class="emphasis"><em>Nested Lists</em></span>:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   299
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   300
<pre class="programlisting">&gt;&gt;&gt; a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   301
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   302
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   303
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   304
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   305
<div class="section" title="4. List Methods">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   306
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   307
<a name="list-methods"></a>4. List Methods</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   308
<p><a name="x_12"></a>A method is a function that is coupled to an object. More about objects and its methods are discussed in Advanced Python module. In general, a method is called like:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   309
<pre class="programlisting">object.method(arguments)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   310
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   311
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   312
<p><a name="x_13"></a>For now, it is enough to know that a list of elements is an object and so 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   313
      <span class="emphasis"><em>List</em></span> methods can be called upon them. Also some of the methods change the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   314
      <span class="emphasis"><em>List</em></span> in-place, meaning it modifies the existing list instead of creating a new one, while other methods don't. It must be noted as we run through the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   315
      <span class="emphasis"><em>List</em></span> methods.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   316
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   317
<p><a name="x_14"></a>Some of the most commonly used 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   318
      <span class="emphasis"><em>List</em></span> methods are as follows:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   319
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   320
<div class="section" title="4.1. append">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   321
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   322
<a name="append"></a>4.1. append</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   323
<p><a name="x_15"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   324
        <span class="emphasis"><em>append</em></span> method is used to append an object at the end of the list:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   325
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   326
<pre class="programlisting">&gt;&gt;&gt; prime = [2, 3, 5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   327
&gt;&gt;&gt; prime.append(7)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   328
&gt;&gt;&gt; prime
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   329
[2, 3, 5, 7]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   330
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   331
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   332
<p><a name="x_16"></a>It is important to note that append changes the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   333
        <span class="emphasis"><em>List</em></span> in-place.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   334
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   335
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   336
<div class="section" title="4.2. count">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   337
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   338
<a name="count"></a>4.2. count</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   339
<p><a name="x_17"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   340
        <span class="emphasis"><em>count</em></span> method returns the number of occurences of a particular element in a list:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   341
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   342
<pre class="programlisting">&gt;&gt;&gt; [1, 4, 4, 9, 9, 9].count(9)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   343
3
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   344
&gt;&gt;&gt; tlst = ['Python', 'is', 'a', 'beautiful', 'language']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   345
&gt;&gt;&gt; tlst.count('Python')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   346
1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   347
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   348
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   349
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   350
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   351
<div class="section" title="4.3. extend">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   352
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   353
<a name="extend"></a>4.3. extend</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   354
<p><a name="x_18"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   355
        <span class="emphasis"><em>extend</em></span> method extends the list on which it is called by the list supplied as argument to it:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   356
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   357
<pre class="programlisting">&gt;&gt;&gt; a = [1, 2, 3]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   358
&gt;&gt;&gt; b = [4, 5, 6]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   359
&gt;&gt;&gt; a.extend(b)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   360
[1, 2, 3, 4, 5, 6]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   361
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   362
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   363
<p><a name="x_19"></a>This is an in-place method. This method is equivalent to using the + operator, but using the + operator returns a new list.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   364
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   365
<div class="section" title="4.4. index">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   366
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   367
<a name="index"></a>4.4. index</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   368
<p><a name="x_1a"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   369
        <span class="emphasis"><em>index</em></span> method returns the index position of the element in the list specified as argument:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   370
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   371
<pre class="programlisting">&gt;&gt;&gt; a = [1, 2, 3, ,4, 5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   372
&gt;&gt;&gt; a.index(4)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   373
3
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   374
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   375
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   376
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   377
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   378
<div class="section" title="4.5. insert">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   379
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   380
<a name="insert"></a>4.5. insert</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   381
<p><a name="x_1b"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   382
        <span class="emphasis"><em>insert</em></span> method is used to insert an element specified as the second argument to the list at the position specified by the first argument:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   383
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   384
<pre class="programlisting">&gt;&gt;&gt; a = ['Python', 'is', 'cool']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   385
&gt;&gt;&gt; a.insert(2, 'so')
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   386
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   387
['Python', 'is', 'so', 'cool']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   388
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   389
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   390
<p><a name="x_1c"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   391
        <span class="emphasis"><em>insert</em></span> method changes the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   392
        <span class="emphasis"><em>List</em></span> in-place.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   393
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   394
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   395
<div class="section" title="4.6. pop">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   396
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   397
<a name="pop"></a>4.6. pop</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   398
<p><a name="x_1d"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   399
        <span class="emphasis"><em>pop</em></span> method removes an element from the list. The index position of the element to be removed can be specified as an argument to the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   400
        <span class="emphasis"><em>pop</em></span> method, if not it removes the last element by default:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   401
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   402
<pre class="programlisting">&gt;&gt;&gt; a = [1, 2, 3, 4, 5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   403
&gt;&gt;&gt; a.pop()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   404
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   405
5
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   406
&gt;&gt;&gt; a.pop(2)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   407
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   408
3
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   409
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   410
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   411
<p><a name="x_1e"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   412
        <span class="emphasis"><em>pop</em></span> method changes the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   413
        <span class="emphasis"><em>List</em></span> in-place.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   414
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   415
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   416
<div class="section" title="4.7. remove">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   417
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   418
<a name="remove"></a>4.7. remove</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   419
<p><a name="x_1f"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   420
        <span class="emphasis"><em>remove</em></span> method removes the first occurence of an element supplied as a parameter:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   421
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   422
<pre class="programlisting">&gt;&gt;&gt; a = [1, 2, 3, 4, 2, 5, 2]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   423
&gt;&gt;&gt; a.remove(2)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   424
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   425
[1, 3, 4, 2, 5, 2]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   426
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   427
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   428
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   429
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   430
<div class="section" title="4.8. reverse">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   431
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   432
<a name="reverse"></a>4.8. reverse</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   433
<p><a name="x_20"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   434
        <span class="emphasis"><em>reverse</em></span> method reverses elements in the list. It is important to note here that 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   435
        <span class="emphasis"><em>reverse</em></span> method changes the list in-place and doesn't return any thing:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   436
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   437
<pre class="programlisting">&gt;&gt;&gt; a = ['guido', 'alex', 'tim']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   438
&gt;&gt;&gt; a.reverse()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   439
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   440
['tim', 'alex', 'guido']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   441
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   442
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   443
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   444
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   445
<div class="section" title="4.9. sort">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   446
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   447
<a name="sort"></a>4.9. sort</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   448
<p><a name="x_21"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   449
        <span class="emphasis"><em>sort</em></span> method is used to sort the elements of the list. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   450
        <span class="emphasis"><em>sort</em></span> method also sorts in-place and does not return anything:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   451
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   452
<pre class="programlisting">&gt;&gt;&gt; a = [5, 1, 3, 7, 4]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   453
&gt;&gt;&gt; a.sort()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   454
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   455
[1, 3, 4, 5, 7]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   456
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   457
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   458
<p><a name="x_22"></a>In addition to the sort method on a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   459
        <span class="emphasis"><em>List</em></span> object we can also use the built-in 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   460
        <span class="strong"><strong>sorted</strong></span> function. This function takes the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   461
        <span class="emphasis"><em>List</em></span> as a parameter and returns a sorted copy of the list. However the original list is left intact:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   462
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   463
<pre class="programlisting">&gt;&gt;&gt; a = [5, 1, 3, 7, 4]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   464
&gt;&gt;&gt; b = sorted(a)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   465
&gt;&gt;&gt; b
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   466
[1, 3, 4, 5, 7]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   467
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   468
[5, 1, 3, 7, 4]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   469
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   470
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   471
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   472
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   473
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   474
<div class="section" title="5. Tuples">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   475
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   476
<a name="tuples"></a>5. Tuples</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   477
<p><a name="x_23"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   478
      <span class="emphasis"><em>Tuples</em></span> are sequences just like 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   479
      <span class="emphasis"><em>Lists</em></span>, but they are immutable. In other words 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   480
      <span class="emphasis"><em>Tuples</em></span> provides a way to represent a group of items, where the group of items cannot be changed in any way. The syntax of a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   481
      <span class="emphasis"><em>Tuple</em></span> is also very similar to 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   482
      <span class="emphasis"><em>List</em></span>. A 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   483
      <span class="emphasis"><em>Tuple</em></span> is represented with the list of items, called elements of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   484
      <span class="emphasis"><em>Tuple</em></span> separated by comma, with the entire list being enclosed in parenthesis. It is not compulsory to use parenthesis around a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   485
      <span class="emphasis"><em>Tuple</em></span> but it may be necessary in some of the cases:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   486
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   487
<pre class="programlisting">&gt;&gt;&gt; a = 1, 2, 3
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   488
&gt;&gt;&gt; a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   489
(1, 2, 3)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   490
&gt;&gt;&gt; b = 1,
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   491
&gt;&gt;&gt; b
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   492
(1,)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   493
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   494
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   495
<p><a name="x_24"></a>It is interesting to note the second example. Just a value followed by a comma automatically makes that an element of a 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   496
      <span class="emphasis"><em>Tuple</em></span> with only one element. It is also important to note that, irrespective of input having a parenthesis, the output always has a parenthesis.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   497
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   498
<p><a name="x_25"></a>The first example is also known as 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   499
      <span class="emphasis"><em>Tuple packing</em></span>, because values are being packed into a tuple. It is also possible to do 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   500
      <span class="emphasis"><em>Tuple unpacking</em></span> which is more interesting. It is better to understand that by example. Say we have a co-ordinate pair from which we need to separate x and y co-ordinates:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   501
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   502
<pre class="programlisting">&gt;&gt;&gt; a = (1, 2)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   503
&gt;&gt;&gt; x, y = a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   504
&gt;&gt;&gt; x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   505
1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   506
&gt;&gt;&gt; y
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   507
2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   508
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   509
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   510
<p><a name="x_26"></a>      <span class="emphasis"><em>Tuple unpacking</em></span> also has several other use-cases of which the most interesting one is to swap the values of two variables. Using programming languages like C would require anywhere around 10 lines of code and an extra temporary variable to do this (including all the #include stuff). Python does it in the most intuitive way in just one line. Say we want to swap the co-ordinates in the above example:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   511
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   512
<pre class="programlisting">&gt;&gt;&gt; x, y = y, x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   513
&gt;&gt;&gt; x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   514
2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   515
&gt;&gt;&gt; y
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   516
1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   517
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   518
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   519
<div class="section" title="5.1. Common Tuple Operations">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   520
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   521
<a name="common-tuple-operations"></a>5.1. Common Tuple Operations</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   522
<p><a name="x_27"></a>There is no need to introduce all the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   523
        <span class="emphasis"><em>Tuple</em></span> operations again, since 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   524
        <span class="emphasis"><em>Tuples</em></span> support the following operations that 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   525
        <span class="emphasis"><em>List</em></span> supports in exactly the same way:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   526
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   527
<div class="blockquote"><blockquote class="blockquote"><div class="itemizedlist"><ul class="itemizedlist" type="*">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   528
<li class="listitem" style="list-style-type: *"><p><a name="x_28"></a>Indexing</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   529
<li class="listitem" style="list-style-type: *"><p><a name="x_29"></a>Concatenating</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   530
<li class="listitem" style="list-style-type: *"><p><a name="x_2a"></a>Slicing</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   531
<li class="listitem" style="list-style-type: *"><p><a name="x_2b"></a>Membership</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   532
<li class="listitem" style="list-style-type: *"><p><a name="x_2c"></a>Multiplication</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   533
<li class="listitem" style="list-style-type: *"><p><a name="x_2d"></a>Length, Maximum, Minimum</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   534
</ul></div></blockquote></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   535
<p><a name="x_2e"></a>The following examples illustrate the above operations:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   536
<pre class="programlisting">&gt;&gt;&gt; a = (1, 2, 3, 4, 5, 6)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   537
&gt;&gt;&gt; a[5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   538
6
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   539
&gt;&gt;&gt; b = (7, 8, 9)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   540
&gt;&gt;&gt; a + b
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   541
(1, 2, 3, 4, 5, 6, 7, 8, 9)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   542
&gt;&gt;&gt; a[3:5]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   543
(4, 5)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   544
&gt;&gt;&gt; 5 in a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   545
True
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   546
&gt;&gt;&gt; c = (1,)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   547
&gt;&gt;&gt; c * 5
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   548
(1, 1, 1, 1, 1)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   549
&gt;&gt;&gt; len(a)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   550
6
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   551
&gt;&gt;&gt; max(a)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   552
6
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   553
&gt;&gt;&gt; min(a)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   554
1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   555
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   556
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   557
<p><a name="x_2f"></a>However the following 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   558
        <span class="emphasis"><em>List</em></span> operations are not supported by 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   559
        <span class="emphasis"><em>Tuples</em></span> because 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   560
        <span class="emphasis"><em>Tuples</em></span> cannot be changed once they are created:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   561
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   562
<div class="blockquote"><blockquote class="blockquote"><div class="itemizedlist"><ul class="itemizedlist" type="*">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   563
<li class="listitem" style="list-style-type: *"><p><a name="x_30"></a>Changing elements</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   564
<li class="listitem" style="list-style-type: *"><p><a name="x_31"></a>Deleting elements</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   565
<li class="listitem" style="list-style-type: *"><p><a name="x_32"></a>Assigning to slices</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   566
</ul></div></blockquote></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   567
<p><a name="x_33"></a>Similarity to 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   568
        <span class="emphasis"><em>Lists</em></span> leads to the questions like, why not 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   569
        <span class="emphasis"><em>Lists</em></span> only? Why do we even want 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   570
        <span class="emphasis"><em>Tuples</em></span>? Can we do the same with 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   571
        <span class="emphasis"><em>Lists</em></span>? And the answer is 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   572
        <span class="strong"><strong>Yes</strong></span> we can do it, but 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   573
        <span class="emphasis"><em>Tuples</em></span> are helpful at times, like we can return Tuples from functions. They are also returned by some built-in functions and methods. And also there are some use cases like co-ordinate among other things. So 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   574
        <span class="emphasis"><em>Tuples</em></span> are helpful.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   575
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   576
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   577
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   578
<div class="section" title="6. Additional Syntax">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   579
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   580
<a name="additional-syntax"></a>6. Additional Syntax</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   581
<p><a name="x_34"></a>The following additional syntax are introduced to make it easier to operate on 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   582
      <span class="emphasis"><em>Lists</em></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   583
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   584
<div class="section" title="6.1. range()">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   585
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   586
<a name="range"></a>6.1. range()</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   587
<p><a name="x_35"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   588
        <span class="emphasis"><em>range</em></span> function takes at least one argument and 2 additional optional arguments. If two or more arguments are specified, the range function returns a list of natural numbers starting from the first argument passed to it to the second argument. The third argument, if specified is used as a step. Suppose only one argument is specified, then 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   589
        <span class="emphasis"><em>range</em></span> function returns a list of natural numbers starting from 0 upto the argument specified:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   590
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   591
<pre class="programlisting">&gt;&gt;&gt; range(5, 10, 2)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   592
[5, 7, 9]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   593
&gt;&gt;&gt; range(2, 15)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   594
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   595
&gt;&gt;&gt; range(12)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   596
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   597
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   598
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   599
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   600
<div class="section" title="6.2. for">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   601
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   602
<a name="for"></a>6.2. for</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   603
<p><a name="x_36"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   604
        <span class="strong"><strong>for</strong></span> keyword is used as a part of the looping construct. Unlike for loops in other languages, Python's for is used to iterate through the elements of sequences like 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   605
        <span class="emphasis"><em>Lists</em></span>, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   606
        <span class="emphasis"><em>Tuples</em></span>, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   607
        <span class="emphasis"><em>Dictionaries</em></span>, etc. The syntax of the for loop consists of 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   608
        <span class="strong"><strong>for</strong></span>, followed by a variable to hold the individual or the current element of the list during iteration and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   609
        <span class="strong"><strong>in</strong></span>, followed by the sequence and a semicolon(':') The next line which is part of the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   610
        <span class="strong"><strong>for</strong></span> loop, i.e the statements that are part of the loop should start with a new intend:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   611
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   612
<pre class="programlisting">&gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   613
&gt;&gt;&gt; for name in names:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   614
...   print "Name =", name
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   615
... 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   616
Name = Guido
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   617
Name = Alex
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   618
Name = Tim
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   619
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   620
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   621
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   622
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   623
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   624
<div class="section" title="7. Conclusion">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   625
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   626
<a name="conclusion"></a>7. Conclusion</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   627
<p><a name="x_37"></a>This section on 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   628
      <span class="emphasis"><em>Lists</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   629
      <span class="emphasis"><em>Tuples</em></span> introduces almost all the necessary machinary required to work on 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   630
      <span class="emphasis"><em>Lists</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   631
      <span class="emphasis"><em>Tuples</em></span>. Topics like how to use these data structures in bigger more useful programs will be introduced in the subsequent chapters.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   632
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   633
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   634
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   635
<div class="navfooter">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   636
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   637
<table width="100%" summary="Navigation footer">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   638
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   639
<td width="40%" align="left">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   640
<a accesskey="p" href="index.html">Prev</a> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   641
<td width="20%" align="center"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   642
<td width="40%" align="right"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   643
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   644
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   645
<td width="40%" align="left" valign="top">Chapter 1. List and Tuples </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   646
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   647
<td width="40%" align="right" valign="top"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   648
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   649
</table>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   650
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   651
</body>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   652
</html>