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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Lists and Tuples</title>
<link rel="stylesheet" href="hgbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
<link rel="home" href="index.html" title="Chapter 1. List and Tuples">
<link rel="up" href="index.html" title="Chapter 1. List and Tuples">
<link rel="prev" href="index.html" title="Chapter 1. List and Tuples">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Lists and Tuples</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="index.html">Prev</a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> </td>
</tr>
</table>
<hr>
</div>
<div class="article" title="Lists and Tuples">
<div class="titlepage">
<div><div><h2 class="title">
<a name="id2609714"></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="ar01.html#common-list-operations">1. Common List Operations</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="ar01.html#indexing">1.1. Indexing</a></span></dt>
<dt><span class="section"><a href="ar01.html#concatenating">1.2. Concatenating</a></span></dt>
<dt><span class="section"><a href="ar01.html#slicing">1.3. Slicing</a></span></dt>
<dt><span class="section"><a href="ar01.html#multiplication">1.4. Multiplication</a></span></dt>
<dt><span class="section"><a href="ar01.html#membership">1.5. Membership</a></span></dt>
<dt><span class="section"><a href="ar01.html#length-maximum-and-minimum">1.6. Length, Maximum and Minimum</a></span></dt>
<dt><span class="section"><a href="ar01.html#changing-elements">1.7. Changing Elements</a></span></dt>
<dt><span class="section"><a href="ar01.html#deleting-elements">1.8. Deleting Elements</a></span></dt>
<dt><span class="section"><a href="ar01.html#assign-to-slices">1.9. Assign to Slices</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="ar01.html#none-empty-lists-and-initialization">2. None, Empty Lists, and Initialization</a></span></dt>
<dt><span class="section"><a href="ar01.html#nested-lists">3. Nested Lists</a></span></dt>
<dt><span class="section"><a href="ar01.html#list-methods">4. List Methods</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="ar01.html#append">4.1. append</a></span></dt>
<dt><span class="section"><a href="ar01.html#count">4.2. count</a></span></dt>
<dt><span class="section"><a href="ar01.html#extend">4.3. extend</a></span></dt>
<dt><span class="section"><a href="ar01.html#index">4.4. index</a></span></dt>
<dt><span class="section"><a href="ar01.html#insert">4.5. insert</a></span></dt>
<dt><span class="section"><a href="ar01.html#pop">4.6. pop</a></span></dt>
<dt><span class="section"><a href="ar01.html#remove">4.7. remove</a></span></dt>
<dt><span class="section"><a href="ar01.html#reverse">4.8. reverse</a></span></dt>
<dt><span class="section"><a href="ar01.html#sort">4.9. sort</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="ar01.html#tuples">5. Tuples</a></span></dt>
<dd><dl><dt><span class="section"><a href="ar01.html#common-tuple-operations">5.1. Common Tuple Operations</a></span></dt></dl></dd>
<dt><span class="section"><a href="ar01.html#additional-syntax">6. Additional Syntax</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="ar01.html#range">6.1. range()</a></span></dt>
<dt><span class="section"><a href="ar01.html#for">6.2. for</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="ar01.html#conclusion">7. Conclusion</a></span></dt>
</dl>
</div>Lists
  <p><a name="x_1"></a>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. Common List Operations">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="common-list-operations"></a>1. Common List Operations</h2></div></div></div>
<p><a name="x_2"></a>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. Indexing">
<div class="titlepage"><div><div><h3 class="title">
<a name="indexing"></a>1.1. Indexing</h3></div></div></div>
<p><a name="x_3"></a>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><a name="x_4"></a>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.2. Concatenating">
<div class="titlepage"><div><div><h3 class="title">
<a name="concatenating"></a>1.2. Concatenating</h3></div></div></div>
<p><a name="x_5"></a>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.3. Slicing">
<div class="titlepage"><div><div><h3 class="title">
<a name="slicing"></a>1.3. Slicing</h3></div></div></div>
<p><a name="x_6"></a>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><a name="x_7"></a>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><a name="x_8"></a>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.4. Multiplication">
<div class="titlepage"><div><div><h3 class="title">
<a name="multiplication"></a>1.4. Multiplication</h3></div></div></div>
<p><a name="x_9"></a>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.5. Membership">
<div class="titlepage"><div><div><h3 class="title">
<a name="membership"></a>1.5. Membership</h3></div></div></div>
<p><a name="x_a"></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.6. Length, Maximum and Minimum">
<div class="titlepage"><div><div><h3 class="title">
<a name="length-maximum-and-minimum"></a>1.6. Length, Maximum and Minimum</h3></div></div></div>
<p><a name="x_b"></a>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.7. Changing Elements">
<div class="titlepage"><div><div><h3 class="title">
<a name="changing-elements"></a>1.7. Changing Elements</h3></div></div></div>
<p><a name="x_c"></a>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.8. Deleting Elements">
<div class="titlepage"><div><div><h3 class="title">
<a name="deleting-elements"></a>1.8. Deleting Elements</h3></div></div></div>
<p><a name="x_d"></a>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.9. Assign to Slices">
<div class="titlepage"><div><div><h3 class="title">
<a name="assign-to-slices"></a>1.9. Assign to Slices</h3></div></div></div>
<p><a name="x_e"></a>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><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 
        <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="2. None, Empty Lists, and Initialization">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="none-empty-lists-and-initialization"></a>2. None, Empty Lists, and Initialization</h2></div></div></div>
<p><a name="x_10"></a>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="3. Nested Lists">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="nested-lists"></a>3. Nested Lists</h2></div></div></div>
<p><a name="x_11"></a>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="4. List Methods">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="list-methods"></a>4. List Methods</h2></div></div></div>
<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>
<pre class="programlisting">object.method(arguments)

</pre>
<p><a name="x_13"></a>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><a name="x_14"></a>Some of the most commonly used 
      <span class="emphasis"><em>List</em></span> methods are as follows:
    </p>
<div class="section" title="4.1. append">
<div class="titlepage"><div><div><h3 class="title">
<a name="append"></a>4.1. append</h3></div></div></div>
<p><a name="x_15"></a>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><a name="x_16"></a>It is important to note that append changes the 
        <span class="emphasis"><em>List</em></span> in-place.
      </p>
</div>
<div class="section" title="4.2. count">
<div class="titlepage"><div><div><h3 class="title">
<a name="count"></a>4.2. count</h3></div></div></div>
<p><a name="x_17"></a>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="4.3. extend">
<div class="titlepage"><div><div><h3 class="title">
<a name="extend"></a>4.3. extend</h3></div></div></div>
<p><a name="x_18"></a>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><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>
</div>
<div class="section" title="4.4. index">
<div class="titlepage"><div><div><h3 class="title">
<a name="index"></a>4.4. index</h3></div></div></div>
<p><a name="x_1a"></a>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="4.5. insert">
<div class="titlepage"><div><div><h3 class="title">
<a name="insert"></a>4.5. insert</h3></div></div></div>
<p><a name="x_1b"></a>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><a name="x_1c"></a>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="4.6. pop">
<div class="titlepage"><div><div><h3 class="title">
<a name="pop"></a>4.6. pop</h3></div></div></div>
<p><a name="x_1d"></a>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><a name="x_1e"></a>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="4.7. remove">
<div class="titlepage"><div><div><h3 class="title">
<a name="remove"></a>4.7. remove</h3></div></div></div>
<p><a name="x_1f"></a>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="4.8. reverse">
<div class="titlepage"><div><div><h3 class="title">
<a name="reverse"></a>4.8. reverse</h3></div></div></div>
<p><a name="x_20"></a>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="4.9. sort">
<div class="titlepage"><div><div><h3 class="title">
<a name="sort"></a>4.9. sort</h3></div></div></div>
<p><a name="x_21"></a>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><a name="x_22"></a>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 class="section" title="5. Tuples">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="tuples"></a>5. Tuples</h2></div></div></div>
<p><a name="x_23"></a>
      <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><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 
      <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><a name="x_25"></a>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><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:
    </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="5.1. Common Tuple Operations">
<div class="titlepage"><div><div><h3 class="title">
<a name="common-tuple-operations"></a>5.1. Common Tuple Operations</h3></div></div></div>
<p><a name="x_27"></a>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="blockquote"><blockquote class="blockquote"><div class="itemizedlist"><ul class="itemizedlist" type="*">
<li class="listitem" style="list-style-type: *"><p><a name="x_28"></a>Indexing</p></li>
<li class="listitem" style="list-style-type: *"><p><a name="x_29"></a>Concatenating</p></li>
<li class="listitem" style="list-style-type: *"><p><a name="x_2a"></a>Slicing</p></li>
<li class="listitem" style="list-style-type: *"><p><a name="x_2b"></a>Membership</p></li>
<li class="listitem" style="list-style-type: *"><p><a name="x_2c"></a>Multiplication</p></li>
<li class="listitem" style="list-style-type: *"><p><a name="x_2d"></a>Length, Maximum, Minimum</p></li>
</ul></div></blockquote></div>
<p><a name="x_2e"></a>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><a name="x_2f"></a>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="blockquote"><blockquote class="blockquote"><div class="itemizedlist"><ul class="itemizedlist" type="*">
<li class="listitem" style="list-style-type: *"><p><a name="x_30"></a>Changing elements</p></li>
<li class="listitem" style="list-style-type: *"><p><a name="x_31"></a>Deleting elements</p></li>
<li class="listitem" style="list-style-type: *"><p><a name="x_32"></a>Assigning to slices</p></li>
</ul></div></blockquote></div>
<p><a name="x_33"></a>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="6. Additional Syntax">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="additional-syntax"></a>6. Additional Syntax</h2></div></div></div>
<p><a name="x_34"></a>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="6.1. range()">
<div class="titlepage"><div><div><h3 class="title">
<a name="range"></a>6.1. range()</h3></div></div></div>
<p><a name="x_35"></a>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="6.2. for">
<div class="titlepage"><div><div><h3 class="title">
<a name="for"></a>6.2. for</h3></div></div></div>
<p><a name="x_36"></a>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="7. Conclusion">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="conclusion"></a>7. Conclusion</h2></div></div></div>
<p><a name="x_37"></a>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 class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="index.html">Prev</a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> </td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 1. List and Tuples </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> </td>
</tr>
</table>
</div>
</body>
</html>