SEESenv/web/html/ch3list_tuples.html
changeset 42 1f61ee485958
parent 41 e54725be4df6
child 43 134b87b382f5
--- a/SEESenv/web/html/ch3list_tuples.html	Tue Mar 02 17:07:14 2010 +0530
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,522 +0,0 @@
-<html>
-<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. list_tuples</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head>
-<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch3list_tuples">
-<div class="titlepage"></div>
-<div class="toc">
-<p><b>Table of Contents</b></p>
-<dl>
-<dt><span class="article"><a href="#id2896388">Lists and Tuples</a></span></dt>
-<dd><dl>
-<dt><span class="section"><a href="#id2706433">1. Lists</a></span></dt>
-<dd><dl>
-<dt><span class="section"><a href="#id2896192">1.1. Common List Operations</a></span></dt>
-<dt><span class="section"><a href="#id2947658">1.2. None, Empty Lists, and Initialization</a></span></dt>
-<dt><span class="section"><a href="#id2947698">1.3. Nested Lists</a></span></dt>
-<dt><span class="section"><a href="#id2947732">1.4. List Methods</a></span></dt>
-</dl></dd>
-<dt><span class="section"><a href="#id2948048">2. Tuples</a></span></dt>
-<dd><dl><dt><span class="section"><a href="#id2948145">2.1. Common Tuple Operations</a></span></dt></dl></dd>
-<dt><span class="section"><a href="#id2948295">3. Additional Syntax</a></span></dt>
-<dd><dl>
-<dt><span class="section"><a href="#id2948312">3.1. range()</a></span></dt>
-<dt><span class="section"><a href="#id2948344">3.2. for</a></span></dt>
-</dl></dd>
-<dt><span class="section"><a href="#id2948403">4. Conclusion</a></span></dt>
-</dl></dd>
-</dl>
-</div>
-<div class="article" title="Lists and Tuples">
-<div class="titlepage">
-<div><div><h2 class="title">
-<a name="id2896388"></a>Lists and Tuples</h2></div></div>
-<hr />
-</div>
-<div class="toc">
-<p><b>Table of Contents</b></p>
-<dl>
-<dt><span class="section"><a href="#id2706433">1. Lists</a></span></dt>
-<dd><dl>
-<dt><span class="section"><a href="#id2896192">1.1. Common List Operations</a></span></dt>
-<dt><span class="section"><a href="#id2947658">1.2. None, Empty Lists, and Initialization</a></span></dt>
-<dt><span class="section"><a href="#id2947698">1.3. Nested Lists</a></span></dt>
-<dt><span class="section"><a href="#id2947732">1.4. List Methods</a></span></dt>
-</dl></dd>
-<dt><span class="section"><a href="#id2948048">2. Tuples</a></span></dt>
-<dd><dl><dt><span class="section"><a href="#id2948145">2.1. Common Tuple Operations</a></span></dt></dl></dd>
-<dt><span class="section"><a href="#id2948295">3. Additional Syntax</a></span></dt>
-<dd><dl>
-<dt><span class="section"><a href="#id2948312">3.1. range()</a></span></dt>
-<dt><span class="section"><a href="#id2948344">3.2. for</a></span></dt>
-</dl></dd>
-<dt><span class="section"><a href="#id2948403">4. Conclusion</a></span></dt>
-</dl>
-</div>
-<div class="section" title="1.Lists">
-<div class="titlepage"><div><div><h2 class="title" style="clear: both">
-<a name="id2706433"></a>1.Lists</h2></div></div></div>
-<p id="ch3list_tuples_1">Python provides an intuitive way to represent a group items, called <span class="emphasis"><em>Lists</em></span>. The
-items of a <span class="emphasis"><em>List</em></span> are called its elements. Unlike C/C++, elements can be of any
-type. A <span class="emphasis"><em>List</em></span> is represented as a list of comma-sepated elements with square
-brackets around them:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [10, 'Python programming', 20.3523, 23, 3534534L]
-&gt;&gt;&gt; a
-[10, 'Python programming', 20.3523, 23, 3534534L]</pre>
-<div class="section" title="1.1.Common List Operations">
-<div class="titlepage"><div><div><h3 class="title">
-<a name="id2896192"></a>1.1.Common List Operations</h3></div></div></div>
-<p id="ch3list_tuples_2">The following are some of the most commonly used operations on <span class="emphasis"><em>Lists</em></span>.</p>
-<div class="section" title="1.1.1.Indexing">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2896236"></a>1.1.1.Indexing</h4></div></div></div>
-<p id="ch3list_tuples_3">Individual elements of a <span class="emphasis"><em>List</em></span> can be accessed using an index to the element.
-The indices start at 0. One can also access the elements of the <span class="emphasis"><em>List</em></span> in reverse
-using negative indices.:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a[1]
-'Python programming'
-&gt;&gt;&gt; a[-1]
-3534534L</pre>
-<p id="ch3list_tuples_4">It is important to note here that the last element of the <span class="emphasis"><em>List</em></span> has an index of
--1.</p>
-</div>
-<div class="section" title="1.1.2.Concatenating">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947372"></a>1.1.2.Concatenating</h4></div></div></div>
-<p id="ch3list_tuples_5">Two or more <span class="emphasis"><em>Lists</em></span> can be concatenated using the + operator:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a + ['foo', 12, 23.3432, 54]
-[10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54]
-&gt;&gt;&gt; [54, 75, 23] + ['write', 67, 'read']
-[54, 75, 23, 'write', 67, 'read']</pre>
-</div>
-<div class="section" title="1.1.3.Slicing">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947393"></a>1.1.3.Slicing</h4></div></div></div>
-<p id="ch3list_tuples_6">A <span class="emphasis"><em>List</em></span> can be sliced off to contain a subset of elements of the <span class="emphasis"><em>List</em></span>. Slicing
-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 <span class="emphasis"><em>List</em></span>.:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
-&gt;&gt;&gt; num[3:6]
-[4, 5, 6]
-&gt;&gt;&gt; num[0:1]
-[1]
-&gt;&gt;&gt; num[7:10]
-[7, 8, 9]</pre>
-<p id="ch3list_tuples_7">The last example showed how to access last 3 elements of the <span class="emphasis"><em>List</em></span>. There is a
-small catch here. The second index 10 actually refers to the 11th element of the
-<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 <span class="emphasis"><em>List</em></span>. But this can also be done in a much easier way using negative indices:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; num[-3:-1]
-[7, 8, 9]</pre>
-<p id="ch3list_tuples_8">Excluding the first index implies that the slice must start at the beginning of
-the <span class="emphasis"><em>List</em></span>, while excluding the second index includes all the elements till the
-end of the <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:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; num[:4]
-[1, 2, 3, 4]
-&gt;&gt;&gt; num[7:]
-[8, 9]
-&gt;&gt;&gt; num[-3:]
-[7, 8, 9]
-&gt;&gt;&gt; num[:]
-[1, 2, 3, 4, 5, 6, 7, 8, 9]
-&gt;&gt;&gt; num[4:9:3]
-[5, 8]
-&gt;&gt;&gt; num[3::2]
-[4, 6, 8]
-&gt;&gt;&gt; num[::4]
-[1, 5, 9]</pre>
-</div>
-<div class="section" title="1.1.4.Multiplication">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947472"></a>1.1.4.Multiplication</h4></div></div></div>
-<p id="ch3list_tuples_9">A <span class="emphasis"><em>List</em></span> can be multiplied with an integer to repeat itself:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; [20] * 5
-[20, 20, 20, 20, 20]
-&gt;&gt;&gt; [42, 'Python', 54] * 3
-[42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54]</pre>
-</div>
-<div class="section" title="1.1.5.Membership">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947497"></a>1.1.5.Membership</h4></div></div></div>
-<p id="ch3list_tuples_a"><span class="strong"><strong>in</strong></span> operator is used to find whether an element is part of the <span class="emphasis"><em>List</em></span>. It
-returns <span class="strong"><strong>True</strong></span> if the element is present in the <span class="emphasis"><em>List</em></span> or <span class="strong"><strong>False</strong></span> if it is not
-present. Since this operator returns a Boolean value it is called a Boolean
-operator:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
-&gt;&gt;&gt; 'Tim' in names
-True
-&gt;&gt;&gt; 'Adam' in names
-False</pre>
-</div>
-<div class="section" title="1.1.6.Length, Maximum and Minimum">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947539"></a>1.1.6.Length, Maximum and Minimum</h4></div></div></div>
-<p id="ch3list_tuples_b">Length of a <span class="emphasis"><em>List</em></span> can be found out using the len function. The max function
-returns the element with the largest value and the min function returns the
-element with the smallest value:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; num = [4, 1, 32, 12, 67, 34, 65]
-&gt;&gt;&gt; len(num)
-7
-&gt;&gt;&gt; max(num)
-67
-&gt;&gt;&gt; min(num)
-1</pre>
-</div>
-<div class="section" title="1.1.7.Changing Elements">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947564"></a>1.1.7.Changing Elements</h4></div></div></div>
-<p id="ch3list_tuples_c">Unlike Strings <span class="emphasis"><em>Lists</em></span> are mutable, i.e. elements of a <span class="emphasis"><em>List</em></span> can be manipulated:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [1, 3, 5, 7]
-&gt;&gt;&gt; a[2] = 9
-&gt;&gt;&gt; a
-[1, 3, 9, 7]</pre>
-</div>
-<div class="section" title="1.1.8.Deleting Elements">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947590"></a>1.1.8.Deleting Elements</h4></div></div></div>
-<p id="ch3list_tuples_d">An element or a slice of a <span class="emphasis"><em>List</em></span> can be deleted by using the <span class="strong"><strong>del</strong></span> statement:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [1, 3, 5, 7, 9, 11]
-&gt;&gt;&gt; del a[-2:]
-&gt;&gt;&gt; a
-[1, 3, 5, 7]
-&gt;&gt;&gt; del a[1]
-&gt;&gt;&gt; a
-[1, 5, 7]</pre>
-</div>
-<div class="section" title="1.1.9.Assign to Slices">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947617"></a>1.1.9.Assign to Slices</h4></div></div></div>
-<p id="ch3list_tuples_e">In the same way, values can be assigned to individual elements of the <span class="emphasis"><em>List</em></span>,
-a <span class="emphasis"><em>List</em></span> of elements can be assigned to a slice:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [2, 3, 4, 5]
-&gt;&gt;&gt; a[:2] = [0, 1]
-[0, 1, 4, 5]
-&gt;&gt;&gt; a[2:2] = [2, 3]
-&gt;&gt;&gt; a
-[0, 1, 2, 3, 4, 5]
-&gt;&gt;&gt; a[2:4] = []
-&gt;&gt;&gt; a
-[0, 1, 4, 5]</pre>
-<p id="ch3list_tuples_f">The last two examples should be particularly noted carefully. The last but one
-example insert elements or a list of elements into a <span class="emphasis"><em>List</em></span> and the last example
-deletes a list of elements from the <span class="emphasis"><em>List</em></span>.</p>
-</div>
-</div>
-<div class="section" title="1.2.None, Empty Lists, and Initialization">
-<div class="titlepage"><div><div><h3 class="title">
-<a name="id2947658"></a>1.2.None, Empty Lists, and Initialization</h3></div></div></div>
-<p id="ch3list_tuples_10">An <span class="emphasis"><em>Empty List</em></span> is a <span class="emphasis"><em>List</em></span> with no elements and is simply represented as
-[]. A <span class="emphasis"><em>None List</em></span> is one with all elements in it being <span class="strong"><strong>None</strong></span>. It serves
-the purpose having a container list of some fixed number of elements with
-no value:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = []
-&gt;&gt;&gt; a
-[]
-&gt;&gt;&gt; n = [None] * 10
-&gt;&gt;&gt; n
-[None, None, None, None, None, None, None, None, None, None]</pre>
-</div>
-<div class="section" title="1.3.Nested Lists">
-<div class="titlepage"><div><div><h3 class="title">
-<a name="id2947698"></a>1.3.Nested Lists</h3></div></div></div>
-<p id="ch3list_tuples_11">As mentioned earlier, a List can contain elements of any data type. This also
-implies a <span class="emphasis"><em>List</em></span> can have a <span class="emphasis"><em>Lists</em></span> themselves as its elements. These are
-called as <span class="emphasis"><em>Nested Lists</em></span>. There is no limit on the depth of the <span class="emphasis"><em>Nested Lists</em></span>:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7]</pre>
-</div>
-<div class="section" title="1.4.List Methods">
-<div class="titlepage"><div><div><h3 class="title">
-<a name="id2947732"></a>1.4.List Methods</h3></div></div></div>
-<p id="ch3list_tuples_12">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>
-<pre class="programlisting">
-object.method(arguments)</pre>
-<p id="ch3list_tuples_13">For now, it is enough to know that a list of elements is an object and
-so <span class="emphasis"><em>List</em></span> methods can be called upon them. Also some of the methods change
-the <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 <span class="emphasis"><em>List</em></span> methods.</p>
-<p id="ch3list_tuples_14">Some of the most commonly used <span class="emphasis"><em>List</em></span> methods are as follows:</p>
-<div class="section" title="1.4.1.append">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947779"></a>1.4.1.append</h4></div></div></div>
-<p id="ch3list_tuples_15">The <span class="emphasis"><em>append</em></span> method is used to append an object at the end of the list:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; prime = [2, 3, 5]
-&gt;&gt;&gt; prime.append(7)
-&gt;&gt;&gt; prime
-[2, 3, 5, 7]</pre>
-<p id="ch3list_tuples_16">It is important to note that append changes the <span class="emphasis"><em>List</em></span> in-place.</p>
-</div>
-<div class="section" title="1.4.2.count">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947808"></a>1.4.2.count</h4></div></div></div>
-<p id="ch3list_tuples_17">The <span class="emphasis"><em>count</em></span> method returns the number of occurences of a particular element
-in a list:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; [1, 4, 4, 9, 9, 9].count(9)
-3
-&gt;&gt;&gt; tlst = ['Python', 'is', 'a', 'beautiful', 'language']
-&gt;&gt;&gt; tlst.count('Python')
-1</pre>
-</div>
-<div class="section" title="1.4.3.extend">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947831"></a>1.4.3.extend</h4></div></div></div>
-<p id="ch3list_tuples_18">The <span class="emphasis"><em>extend</em></span> method extends the list on which it is called by the list supplied
-as argument to it:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [1, 2, 3]
-&gt;&gt;&gt; b = [4, 5, 6]
-&gt;&gt;&gt; a.extend(b)
-[1, 2, 3, 4, 5, 6]</pre>
-<p id="ch3list_tuples_19">This is an in-place method. This method is equivalent to using the + operator, but
-using the + operator returns a new list.</p>
-</div>
-<div class="section" title="1.4.4.index">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947859"></a>1.4.4.index</h4></div></div></div>
-<p id="ch3list_tuples_1a">The <span class="emphasis"><em>index</em></span> method returns the index position of the element in the list
-specified as argument:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [1, 2, 3, ,4, 5]
-&gt;&gt;&gt; a.index(4)
-3</pre>
-</div>
-<div class="section" title="1.4.5.insert">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947879"></a>1.4.5.insert</h4></div></div></div>
-<p id="ch3list_tuples_1b">The <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:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = ['Python', 'is', 'cool']
-&gt;&gt;&gt; a.insert(2, 'so')
-&gt;&gt;&gt; a
-['Python', 'is', 'so', 'cool']</pre>
-<p id="ch3list_tuples_1c">The <span class="emphasis"><em>insert</em></span> method changes the <span class="emphasis"><em>List</em></span> in-place.</p>
-</div>
-<div class="section" title="1.4.6.pop">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947913"></a>1.4.6.pop</h4></div></div></div>
-<p id="ch3list_tuples_1d">The <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
-<span class="emphasis"><em>pop</em></span> method, if not it removes the last element by default:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [1, 2, 3, 4, 5]
-&gt;&gt;&gt; a.pop()
-&gt;&gt;&gt; a
-5
-&gt;&gt;&gt; a.pop(2)
-&gt;&gt;&gt; a
-3</pre>
-<p id="ch3list_tuples_1e">The <span class="emphasis"><em>pop</em></span> method changes the <span class="emphasis"><em>List</em></span> in-place.</p>
-</div>
-<div class="section" title="1.4.7.remove">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947951"></a>1.4.7.remove</h4></div></div></div>
-<p id="ch3list_tuples_1f">The <span class="emphasis"><em>remove</em></span> method removes the first occurence of an element supplied as a
-parameter:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [1, 2, 3, 4, 2, 5, 2]
-&gt;&gt;&gt; a.remove(2)
-&gt;&gt;&gt; a
-[1, 3, 4, 2, 5, 2]</pre>
-</div>
-<div class="section" title="1.4.8.reverse">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947972"></a>1.4.8.reverse</h4></div></div></div>
-<p id="ch3list_tuples_20">The <span class="emphasis"><em>reverse</em></span> method reverses elements in the list. It is important to note
-here that <span class="emphasis"><em>reverse</em></span> method changes the list in-place and doesn't return any
-thing:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = ['guido', 'alex', 'tim']
-&gt;&gt;&gt; a.reverse()
-&gt;&gt;&gt; a
-['tim', 'alex', 'guido']</pre>
-</div>
-<div class="section" title="1.4.9.sort">
-<div class="titlepage"><div><div><h4 class="title">
-<a name="id2947998"></a>1.4.9.sort</h4></div></div></div>
-<p id="ch3list_tuples_21">The <span class="emphasis"><em>sort</em></span> method is used to sort the elements of the list. The <span class="emphasis"><em>sort</em></span> method
-also sorts in-place and does not return anything:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [5, 1, 3, 7, 4]
-&gt;&gt;&gt; a.sort()
-&gt;&gt;&gt; a
-[1, 3, 4, 5, 7]</pre>
-<p id="ch3list_tuples_22">In addition to the sort method on a <span class="emphasis"><em>List</em></span> object we can also use the built-in
-<span class="strong"><strong>sorted</strong></span> function. This function takes the <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:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = [5, 1, 3, 7, 4]
-&gt;&gt;&gt; b = sorted(a)
-&gt;&gt;&gt; b
-[1, 3, 4, 5, 7]
-&gt;&gt;&gt; a
-[5, 1, 3, 7, 4]</pre>
-</div>
-</div>
-</div>
-<div class="section" title="2.Tuples">
-<div class="titlepage"><div><div><h2 class="title" style="clear: both">
-<a name="id2948048"></a>2.Tuples</h2></div></div></div>
-<p id="ch3list_tuples_23"><span class="emphasis"><em>Tuples</em></span> are sequences just like <span class="emphasis"><em>Lists</em></span>, but they are immutable. In other
-words <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 <span class="emphasis"><em>Tuple</em></span> is also very
-similar to <span class="emphasis"><em>List</em></span>. A <span class="emphasis"><em>Tuple</em></span> is represented with the list of items, called
-elements of the <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 <span class="emphasis"><em>Tuple</em></span> but
-it may be necessary in some of the cases:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = 1, 2, 3
-&gt;&gt;&gt; a
-(1, 2, 3)
-&gt;&gt;&gt; b = 1,
-&gt;&gt;&gt; b
-(1,)</pre>
-<p id="ch3list_tuples_24">It is interesting to note the second example. Just a value followed by a comma
-automatically makes that an element of a <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.</p>
-<p id="ch3list_tuples_25">The first example is also known as <span class="emphasis"><em>Tuple packing</em></span>, because values are being
-packed into a tuple. It is also possible to do <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:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = (1, 2)
-&gt;&gt;&gt; x, y = a
-&gt;&gt;&gt; x
-1
-&gt;&gt;&gt; y
-2</pre>
-<p id="ch3list_tuples_26"><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:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; x, y = y, x
-&gt;&gt;&gt; x
-2
-&gt;&gt;&gt; y
-1</pre>
-<div class="section" title="2.1.Common Tuple Operations">
-<div class="titlepage"><div><div><h3 class="title">
-<a name="id2948145"></a>2.1.Common Tuple Operations</h3></div></div></div>
-<p id="ch3list_tuples_27">There is no need to introduce all the <span class="emphasis"><em>Tuple</em></span> operations again, since <span class="emphasis"><em>Tuples</em></span>
-support the following operations that <span class="emphasis"><em>List</em></span> supports in exactly the same way:</p>
-<div class="itemizedlist"><ul class="itemizedlist" type="*">
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_28">Indexing</p></li>
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_29">Concatenating</p></li>
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2a">Slicing</p></li>
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2b">Membership</p></li>
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2c">Multiplication</p></li>
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2d">Length, Maximum, Minimum</p></li>
-</ul></div>
-<p id="ch3list_tuples_2e">The following examples illustrate the above operations:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; a = (1, 2, 3, 4, 5, 6)
-&gt;&gt;&gt; a[5]
-6
-&gt;&gt;&gt; b = (7, 8, 9)
-&gt;&gt;&gt; a + b
-(1, 2, 3, 4, 5, 6, 7, 8, 9)
-&gt;&gt;&gt; a[3:5]
-(4, 5)
-&gt;&gt;&gt; 5 in a
-True
-&gt;&gt;&gt; c = (1,)
-&gt;&gt;&gt; c * 5
-(1, 1, 1, 1, 1)
-&gt;&gt;&gt; len(a)
-6
-&gt;&gt;&gt; max(a)
-6
-&gt;&gt;&gt; min(a)
-1</pre>
-<p id="ch3list_tuples_2f">However the following <span class="emphasis"><em>List</em></span> operations are not supported by <span class="emphasis"><em>Tuples</em></span> because
-<span class="emphasis"><em>Tuples</em></span> cannot be changed once they are created:</p>
-<div class="itemizedlist"><ul class="itemizedlist" type="*">
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_30">Changing elements</p></li>
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_31">Deleting elements</p></li>
-<li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_32">Assigning to slices</p></li>
-</ul></div>
-<p id="ch3list_tuples_33">Similarity to <span class="emphasis"><em>Lists</em></span> leads to the questions like, why not <span class="emphasis"><em>Lists</em></span> only? Why do
-we even want <span class="emphasis"><em>Tuples</em></span>? Can we do the same with <span class="emphasis"><em>Lists</em></span>? And the answer is <span class="strong"><strong>Yes</strong></span>
-we can do it, but <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 <span class="emphasis"><em>Tuples</em></span>
-are helpful.</p>
-</div>
-</div>
-<div class="section" title="3.Additional Syntax">
-<div class="titlepage"><div><div><h2 class="title" style="clear: both">
-<a name="id2948295"></a>3.Additional Syntax</h2></div></div></div>
-<p id="ch3list_tuples_34">The following additional syntax are introduced to make it easier to operate on
-<span class="emphasis"><em>Lists</em></span>.</p>
-<div class="section" title="3.1.range()">
-<div class="titlepage"><div><div><h3 class="title">
-<a name="id2948312"></a>3.1.range()</h3></div></div></div>
-<p id="ch3list_tuples_35">The <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 <span class="emphasis"><em>range</em></span> function returns a list of natural
-numbers starting from 0 upto the argument specified:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; range(5, 10, 2)
-[5, 7, 9]
-&gt;&gt;&gt; range(2, 15)
-[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
-&gt;&gt;&gt; range(12)
-[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]</pre>
-</div>
-<div class="section" title="3.2.for">
-<div class="titlepage"><div><div><h3 class="title">
-<a name="id2948344"></a>3.2.for</h3></div></div></div>
-<p id="ch3list_tuples_36">The <span class="strong"><strong>for</strong></span> keyword is used as a part of the looping construct. Unlike for loops
-in other languages, Python's for is used to iterate through the elements of
-sequences like <span class="emphasis"><em>Lists</em></span>, <span class="emphasis"><em>Tuples</em></span>, <span class="emphasis"><em>Dictionaries</em></span>, etc. The syntax of the for loop
-consists of <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 <span class="strong"><strong>in</strong></span>, followed by the sequence and a
-semicolon(':') The next line which is part of the <span class="strong"><strong>for</strong></span> loop, i.e the statements
-that are part of the loop should start with a new intend:</p>
-<pre class="programlisting">
-&gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
-&gt;&gt;&gt; for name in names:
-...   print "Name =", name
-...
-Name = Guido
-Name = Alex
-Name = Tim</pre>
-</div>
-</div>
-<div class="section" title="4.Conclusion">
-<div class="titlepage"><div><div><h2 class="title" style="clear: both">
-<a name="id2948403"></a>4.Conclusion</h2></div></div></div>
-<p id="ch3list_tuples_37">This section on <span class="emphasis"><em>Lists</em></span> and <span class="emphasis"><em>Tuples</em></span> introduces almost all the necessary
-machinary required to work on <span class="emphasis"><em>Lists</em></span> and <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.</p>
-</div>
-</div>
-</div></body>
-</html>
-