SEESenv/web/html/ch3list_tuples.html
changeset 28 514098969b11
parent 27 cb14131583c6
equal deleted inserted replaced
27:cb14131583c6 28:514098969b11
     1 <html>
     1 <html>
     2  <head>
     2 <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. list_tuples</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head>
     3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     3 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch3list_tuples">
     4   <title>
     4 <div class="titlepage"></div>
     5    Chapter. list_tuples
     5 <div class="toc">
     6   </title>
     6 <p><b>Table of Contents</b></p>
     7   <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
     7 <dl>
     8   <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" />
     8 <dt><span class="article"><a href="#id2896388">Lists and Tuples</a></span></dt>
     9   <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" />
     9 <dd><dl>
    10   <script type="text/javascript" src="/review/support/jquery-min.js">
    10 <dt><span class="section"><a href="#id2706433">1. Lists</a></span></dt>
    11   </script>
    11 <dd><dl>
    12   <script type="text/javascript" src="/review/support/form.js">
    12 <dt><span class="section"><a href="#id2896192">1.1. Common List Operations</a></span></dt>
    13   </script>
    13 <dt><span class="section"><a href="#id2947658">1.2. None, Empty Lists, and Initialization</a></span></dt>
    14   <script type="text/javascript" src="/review/support/hsbook.js">
    14 <dt><span class="section"><a href="#id2947698">1.3. Nested Lists</a></span></dt>
    15   </script>
    15 <dt><span class="section"><a href="#id2947732">1.4. List Methods</a></span></dt>
    16  </head>
    16 </dl></dd>
    17  <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
    17 <dt><span class="section"><a href="#id2948048">2. Tuples</a></span></dt>
    18   <div class="chapter" id="ch3list_tuples">
    18 <dd><dl><dt><span class="section"><a href="#id2948145">2.1. Common Tuple Operations</a></span></dt></dl></dd>
    19    <div class="titlepage">
    19 <dt><span class="section"><a href="#id2948295">3. Additional Syntax</a></span></dt>
    20    </div>
    20 <dd><dl>
    21    <div class="toc">
    21 <dt><span class="section"><a href="#id2948312">3.1. range()</a></span></dt>
    22     <p>
    22 <dt><span class="section"><a href="#id2948344">3.2. for</a></span></dt>
    23      <b>
    23 </dl></dd>
    24       Table of Contents
    24 <dt><span class="section"><a href="#id2948403">4. Conclusion</a></span></dt>
    25      </b>
    25 </dl></dd>
    26     </p>
    26 </dl>
    27     <dl>
    27 </div>
    28      <dt>
    28 <div class="article" title="Lists and Tuples">
    29       <span class="article">
    29 <div class="titlepage">
    30        <a href="#id2951820">
    30 <div><div><h2 class="title">
    31         Lists and Tuples
    31 <a name="id2896388"></a>Lists and Tuples</h2></div></div>
    32        </a>
    32 <hr />
    33       </span>
    33 </div>
    34      </dt>
    34 <div class="toc">
    35      <dd>
    35 <p><b>Table of Contents</b></p>
    36       <dl>
    36 <dl>
    37        <dt>
    37 <dt><span class="section"><a href="#id2706433">1. Lists</a></span></dt>
    38         <span class="section">
    38 <dd><dl>
    39          <a href="#id2761866">
    39 <dt><span class="section"><a href="#id2896192">1.1. Common List Operations</a></span></dt>
    40           1. Lists
    40 <dt><span class="section"><a href="#id2947658">1.2. None, Empty Lists, and Initialization</a></span></dt>
    41          </a>
    41 <dt><span class="section"><a href="#id2947698">1.3. Nested Lists</a></span></dt>
    42         </span>
    42 <dt><span class="section"><a href="#id2947732">1.4. List Methods</a></span></dt>
    43        </dt>
    43 </dl></dd>
    44        <dd>
    44 <dt><span class="section"><a href="#id2948048">2. Tuples</a></span></dt>
    45         <dl>
    45 <dd><dl><dt><span class="section"><a href="#id2948145">2.1. Common Tuple Operations</a></span></dt></dl></dd>
    46          <dt>
    46 <dt><span class="section"><a href="#id2948295">3. Additional Syntax</a></span></dt>
    47           <span class="section">
    47 <dd><dl>
    48            <a href="#id2951624">
    48 <dt><span class="section"><a href="#id2948312">3.1. range()</a></span></dt>
    49             1.1. Common List Operations
    49 <dt><span class="section"><a href="#id2948344">3.2. for</a></span></dt>
    50            </a>
    50 </dl></dd>
    51           </span>
    51 <dt><span class="section"><a href="#id2948403">4. Conclusion</a></span></dt>
    52          </dt>
    52 </dl>
    53          <dt>
    53 </div>
    54           <span class="section">
    54 <div class="section" title="1.Lists">
    55            <a href="#id3003090">
    55 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
    56             1.2. None, Empty Lists, and Initialization
    56 <a name="id2706433"></a>1.Lists</h2></div></div></div>
    57            </a>
    57 <p id="ch3list_tuples_1">Python provides an intuitive way to represent a group items, called <span class="emphasis"><em>Lists</em></span>. The
    58           </span>
    58 items of a <span class="emphasis"><em>List</em></span> are called its elements. Unlike C/C++, elements can be of any
    59          </dt>
    59 type. A <span class="emphasis"><em>List</em></span> is represented as a list of comma-sepated elements with square
    60          <dt>
    60 brackets around them:</p>
    61           <span class="section">
    61 <pre class="programlisting">
    62            <a href="#id3003130">
    62 &gt;&gt;&gt; a = [10, 'Python programming', 20.3523, 23, 3534534L]
    63             1.3. Nested Lists
    63 &gt;&gt;&gt; a
    64            </a>
    64 [10, 'Python programming', 20.3523, 23, 3534534L]</pre>
    65           </span>
    65 <div class="section" title="1.1.Common List Operations">
    66          </dt>
    66 <div class="titlepage"><div><div><h3 class="title">
    67          <dt>
    67 <a name="id2896192"></a>1.1.Common List Operations</h3></div></div></div>
    68           <span class="section">
    68 <p id="ch3list_tuples_2">The following are some of the most commonly used operations on <span class="emphasis"><em>Lists</em></span>.</p>
    69            <a href="#id3003165">
    69 <div class="section" title="1.1.1.Indexing">
    70             1.4. List Methods
    70 <div class="titlepage"><div><div><h4 class="title">
    71            </a>
    71 <a name="id2896236"></a>1.1.1.Indexing</h4></div></div></div>
    72           </span>
    72 <p id="ch3list_tuples_3">Individual elements of a <span class="emphasis"><em>List</em></span> can be accessed using an index to the element.
    73          </dt>
    73 The indices start at 0. One can also access the elements of the <span class="emphasis"><em>List</em></span> in reverse
    74         </dl>
    74 using negative indices.:</p>
    75        </dd>
    75 <pre class="programlisting">
    76        <dt>
    76 &gt;&gt;&gt; a[1]
    77         <span class="section">
       
    78          <a href="#id3003481">
       
    79           2. Tuples
       
    80          </a>
       
    81         </span>
       
    82        </dt>
       
    83        <dd>
       
    84         <dl>
       
    85          <dt>
       
    86           <span class="section">
       
    87            <a href="#id3003578">
       
    88             2.1. Common Tuple Operations
       
    89            </a>
       
    90           </span>
       
    91          </dt>
       
    92         </dl>
       
    93        </dd>
       
    94        <dt>
       
    95         <span class="section">
       
    96          <a href="#id3003727">
       
    97           3. Additional Syntax
       
    98          </a>
       
    99         </span>
       
   100        </dt>
       
   101        <dd>
       
   102         <dl>
       
   103          <dt>
       
   104           <span class="section">
       
   105            <a href="#id3003745">
       
   106             3.1. range()
       
   107            </a>
       
   108           </span>
       
   109          </dt>
       
   110          <dt>
       
   111           <span class="section">
       
   112            <a href="#id3003777">
       
   113             3.2. for
       
   114            </a>
       
   115           </span>
       
   116          </dt>
       
   117         </dl>
       
   118        </dd>
       
   119        <dt>
       
   120         <span class="section">
       
   121          <a href="#id3003836">
       
   122           4. Conclusion
       
   123          </a>
       
   124         </span>
       
   125        </dt>
       
   126       </dl>
       
   127      </dd>
       
   128     </dl>
       
   129    </div>
       
   130    <div class="article" title="Lists and Tuples">
       
   131     <div class="titlepage">
       
   132      <div>
       
   133       <div>
       
   134        <h2 class="title">
       
   135         <a name="id2951820">
       
   136         </a>
       
   137         Lists and Tuples
       
   138        </h2>
       
   139       </div>
       
   140      </div>
       
   141      <hr />
       
   142     </div>
       
   143     <div class="toc">
       
   144      <p>
       
   145       <b>
       
   146        Table of Contents
       
   147       </b>
       
   148      </p>
       
   149      <dl>
       
   150       <dt>
       
   151        <span class="section">
       
   152         <a href="#id2761866">
       
   153          1. Lists
       
   154         </a>
       
   155        </span>
       
   156       </dt>
       
   157       <dd>
       
   158        <dl>
       
   159         <dt>
       
   160          <span class="section">
       
   161           <a href="#id2951624">
       
   162            1.1. Common List Operations
       
   163           </a>
       
   164          </span>
       
   165         </dt>
       
   166         <dt>
       
   167          <span class="section">
       
   168           <a href="#id3003090">
       
   169            1.2. None, Empty Lists, and Initialization
       
   170           </a>
       
   171          </span>
       
   172         </dt>
       
   173         <dt>
       
   174          <span class="section">
       
   175           <a href="#id3003130">
       
   176            1.3. Nested Lists
       
   177           </a>
       
   178          </span>
       
   179         </dt>
       
   180         <dt>
       
   181          <span class="section">
       
   182           <a href="#id3003165">
       
   183            1.4. List Methods
       
   184           </a>
       
   185          </span>
       
   186         </dt>
       
   187        </dl>
       
   188       </dd>
       
   189       <dt>
       
   190        <span class="section">
       
   191         <a href="#id3003481">
       
   192          2. Tuples
       
   193         </a>
       
   194        </span>
       
   195       </dt>
       
   196       <dd>
       
   197        <dl>
       
   198         <dt>
       
   199          <span class="section">
       
   200           <a href="#id3003578">
       
   201            2.1. Common Tuple Operations
       
   202           </a>
       
   203          </span>
       
   204         </dt>
       
   205        </dl>
       
   206       </dd>
       
   207       <dt>
       
   208        <span class="section">
       
   209         <a href="#id3003727">
       
   210          3. Additional Syntax
       
   211         </a>
       
   212        </span>
       
   213       </dt>
       
   214       <dd>
       
   215        <dl>
       
   216         <dt>
       
   217          <span class="section">
       
   218           <a href="#id3003745">
       
   219            3.1. range()
       
   220           </a>
       
   221          </span>
       
   222         </dt>
       
   223         <dt>
       
   224          <span class="section">
       
   225           <a href="#id3003777">
       
   226            3.2. for
       
   227           </a>
       
   228          </span>
       
   229         </dt>
       
   230        </dl>
       
   231       </dd>
       
   232       <dt>
       
   233        <span class="section">
       
   234         <a href="#id3003836">
       
   235          4. Conclusion
       
   236         </a>
       
   237        </span>
       
   238       </dt>
       
   239      </dl>
       
   240     </div>
       
   241     <div class="section" title="1.Lists">
       
   242      <div class="titlepage">
       
   243       <div>
       
   244        <div>
       
   245         <h2 class="title" style="clear: both">
       
   246          <a name="id2761866">
       
   247          </a>
       
   248          1.Lists
       
   249         </h2>
       
   250        </div>
       
   251       </div>
       
   252      </div>
       
   253      <p id="ch3list_tuples_1">
       
   254       Python provides an intuitive way to represent a group items, called
       
   255       <span class="emphasis">
       
   256        <em>
       
   257         Lists
       
   258        </em>
       
   259       </span>
       
   260       . The
       
   261 items of a
       
   262       <span class="emphasis">
       
   263        <em>
       
   264         List
       
   265        </em>
       
   266       </span>
       
   267       are called its elements. Unlike C/C++, elements can be of any
       
   268 type. A
       
   269       <span class="emphasis">
       
   270        <em>
       
   271         List
       
   272        </em>
       
   273       </span>
       
   274       is represented as a list of comma-sepated elements with square
       
   275 brackets around them:
       
   276      </p>
       
   277      <pre class="programlisting">
       
   278       &gt;&gt;&gt; a = [10, 'Python programming', 20.3523, 23, 3534534L]
       
   279 &gt;&gt;&gt; a
       
   280 [10, 'Python programming', 20.3523, 23, 3534534L]
       
   281      </pre>
       
   282      <div class="section" title="1.1.Common List Operations">
       
   283       <div class="titlepage">
       
   284        <div>
       
   285         <div>
       
   286          <h3 class="title">
       
   287           <a name="id2951624">
       
   288           </a>
       
   289           1.1.Common List Operations
       
   290          </h3>
       
   291         </div>
       
   292        </div>
       
   293       </div>
       
   294       <p id="ch3list_tuples_2">
       
   295        The following are some of the most commonly used operations on
       
   296        <span class="emphasis">
       
   297         <em>
       
   298          Lists
       
   299         </em>
       
   300        </span>
       
   301        .
       
   302       </p>
       
   303       <div class="section" title="1.1.1.Indexing">
       
   304        <div class="titlepage">
       
   305         <div>
       
   306          <div>
       
   307           <h4 class="title">
       
   308            <a name="id2951669">
       
   309            </a>
       
   310            1.1.1.Indexing
       
   311           </h4>
       
   312          </div>
       
   313         </div>
       
   314        </div>
       
   315        <p id="ch3list_tuples_3">
       
   316         Individual elements of a
       
   317         <span class="emphasis">
       
   318          <em>
       
   319           List
       
   320          </em>
       
   321         </span>
       
   322         can be accessed using an index to the element.
       
   323 The indices start at 0. One can also access the elements of the
       
   324         <span class="emphasis">
       
   325          <em>
       
   326           List
       
   327          </em>
       
   328         </span>
       
   329         in reverse
       
   330 using negative indices.:
       
   331        </p>
       
   332        <pre class="programlisting">
       
   333         &gt;&gt;&gt; a[1]
       
   334 'Python programming'
    77 'Python programming'
   335 &gt;&gt;&gt; a[-1]
    78 &gt;&gt;&gt; a[-1]
   336 3534534L
    79 3534534L</pre>
   337        </pre>
    80 <p id="ch3list_tuples_4">It is important to note here that the last element of the <span class="emphasis"><em>List</em></span> has an index of
   338        <p id="ch3list_tuples_4">
    81 -1.</p>
   339         It is important to note here that the last element of the
    82 </div>
   340         <span class="emphasis">
    83 <div class="section" title="1.1.2.Concatenating">
   341          <em>
    84 <div class="titlepage"><div><div><h4 class="title">
   342           List
    85 <a name="id2947372"></a>1.1.2.Concatenating</h4></div></div></div>
   343          </em>
    86 <p id="ch3list_tuples_5">Two or more <span class="emphasis"><em>Lists</em></span> can be concatenated using the + operator:</p>
   344         </span>
    87 <pre class="programlisting">
   345         has an index of
    88 &gt;&gt;&gt; a + ['foo', 12, 23.3432, 54]
   346 -1.
       
   347        </p>
       
   348       </div>
       
   349       <div class="section" title="1.1.2.Concatenating">
       
   350        <div class="titlepage">
       
   351         <div>
       
   352          <div>
       
   353           <h4 class="title">
       
   354            <a name="id3002805">
       
   355            </a>
       
   356            1.1.2.Concatenating
       
   357           </h4>
       
   358          </div>
       
   359         </div>
       
   360        </div>
       
   361        <p id="ch3list_tuples_5">
       
   362         Two or more
       
   363         <span class="emphasis">
       
   364          <em>
       
   365           Lists
       
   366          </em>
       
   367         </span>
       
   368         can be concatenated using the + operator:
       
   369        </p>
       
   370        <pre class="programlisting">
       
   371         &gt;&gt;&gt; a + ['foo', 12, 23.3432, 54]
       
   372 [10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54]
    89 [10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54]
   373 &gt;&gt;&gt; [54, 75, 23] + ['write', 67, 'read']
    90 &gt;&gt;&gt; [54, 75, 23] + ['write', 67, 'read']
   374 [54, 75, 23, 'write', 67, 'read']
    91 [54, 75, 23, 'write', 67, 'read']</pre>
   375        </pre>
    92 </div>
   376       </div>
    93 <div class="section" title="1.1.3.Slicing">
   377       <div class="section" title="1.1.3.Slicing">
    94 <div class="titlepage"><div><div><h4 class="title">
   378        <div class="titlepage">
    95 <a name="id2947393"></a>1.1.3.Slicing</h4></div></div></div>
   379         <div>
    96 <p id="ch3list_tuples_6">A <span class="emphasis"><em>List</em></span> can be sliced off to contain a subset of elements of the <span class="emphasis"><em>List</em></span>. Slicing
   380          <div>
       
   381           <h4 class="title">
       
   382            <a name="id3002825">
       
   383            </a>
       
   384            1.1.3.Slicing
       
   385           </h4>
       
   386          </div>
       
   387         </div>
       
   388        </div>
       
   389        <p id="ch3list_tuples_6">
       
   390         A
       
   391         <span class="emphasis">
       
   392          <em>
       
   393           List
       
   394          </em>
       
   395         </span>
       
   396         can be sliced off to contain a subset of elements of the
       
   397         <span class="emphasis">
       
   398          <em>
       
   399           List
       
   400          </em>
       
   401         </span>
       
   402         . Slicing
       
   403 can be done by using two indices separated by a colon, where the first index is
    97 can be done by using two indices separated by a colon, where the first index is
   404 inclusive and the second index is exclusive. The resulting slice is also a
    98 inclusive and the second index is exclusive. The resulting slice is also a <span class="emphasis"><em>List</em></span>.:</p>
   405         <span class="emphasis">
    99 <pre class="programlisting">
   406          <em>
   100 &gt;&gt;&gt; num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
   407           List
       
   408          </em>
       
   409         </span>
       
   410         .:
       
   411        </p>
       
   412        <pre class="programlisting">
       
   413         &gt;&gt;&gt; num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
       
   414 &gt;&gt;&gt; num[3:6]
   101 &gt;&gt;&gt; num[3:6]
   415 [4, 5, 6]
   102 [4, 5, 6]
   416 &gt;&gt;&gt; num[0:1]
   103 &gt;&gt;&gt; num[0:1]
   417 [1]
   104 [1]
   418 &gt;&gt;&gt; num[7:10]
   105 &gt;&gt;&gt; num[7:10]
   419 [7, 8, 9]
   106 [7, 8, 9]</pre>
   420        </pre>
   107 <p id="ch3list_tuples_7">The last example showed how to access last 3 elements of the <span class="emphasis"><em>List</em></span>. There is a
   421        <p id="ch3list_tuples_7">
       
   422         The last example showed how to access last 3 elements of the
       
   423         <span class="emphasis">
       
   424          <em>
       
   425           List
       
   426          </em>
       
   427         </span>
       
   428         . There is a
       
   429 small catch here. The second index 10 actually refers to the 11th element of the
   108 small catch here. The second index 10 actually refers to the 11th element of the
   430         <span class="emphasis">
   109 <span class="emphasis"><em>List</em></span> which is still valid, even though it doesn't exist because the second
   431          <em>
       
   432           List
       
   433          </em>
       
   434         </span>
       
   435         which is still valid, even though it doesn't exist because the second
       
   436 index is exclusive and tells the Python interpreter to get the last element of
   110 index is exclusive and tells the Python interpreter to get the last element of
   437 the
   111 the <span class="emphasis"><em>List</em></span>. But this can also be done in a much easier way using negative indices:</p>
   438         <span class="emphasis">
   112 <pre class="programlisting">
   439          <em>
   113 &gt;&gt;&gt; num[-3:-1]
   440           List
   114 [7, 8, 9]</pre>
   441          </em>
   115 <p id="ch3list_tuples_8">Excluding the first index implies that the slice must start at the beginning of
   442         </span>
   116 the <span class="emphasis"><em>List</em></span>, while excluding the second index includes all the elements till the
   443         . But this can also be done in a much easier way using negative indices:
   117 end of the <span class="emphasis"><em>List</em></span>. A third parameter to a slice, which is implicitly taken as 1
   444        </p>
       
   445        <pre class="programlisting">
       
   446         &gt;&gt;&gt; num[-3:-1]
       
   447 [7, 8, 9]
       
   448        </pre>
       
   449        <p id="ch3list_tuples_8">
       
   450         Excluding the first index implies that the slice must start at the beginning of
       
   451 the
       
   452         <span class="emphasis">
       
   453          <em>
       
   454           List
       
   455          </em>
       
   456         </span>
       
   457         , while excluding the second index includes all the elements till the
       
   458 end of the
       
   459         <span class="emphasis">
       
   460          <em>
       
   461           List
       
   462          </em>
       
   463         </span>
       
   464         . A third parameter to a slice, which is implicitly taken as 1
       
   465 is the step of the slice. It is specified as a value which follows a colon after
   118 is the step of the slice. It is specified as a value which follows a colon after
   466 the second index:
   119 the second index:</p>
   467        </p>
   120 <pre class="programlisting">
   468        <pre class="programlisting">
   121 &gt;&gt;&gt; num[:4]
   469         &gt;&gt;&gt; num[:4]
       
   470 [1, 2, 3, 4]
   122 [1, 2, 3, 4]
   471 &gt;&gt;&gt; num[7:]
   123 &gt;&gt;&gt; num[7:]
   472 [8, 9]
   124 [8, 9]
   473 &gt;&gt;&gt; num[-3:]
   125 &gt;&gt;&gt; num[-3:]
   474 [7, 8, 9]
   126 [7, 8, 9]
   477 &gt;&gt;&gt; num[4:9:3]
   129 &gt;&gt;&gt; num[4:9:3]
   478 [5, 8]
   130 [5, 8]
   479 &gt;&gt;&gt; num[3::2]
   131 &gt;&gt;&gt; num[3::2]
   480 [4, 6, 8]
   132 [4, 6, 8]
   481 &gt;&gt;&gt; num[::4]
   133 &gt;&gt;&gt; num[::4]
   482 [1, 5, 9]
   134 [1, 5, 9]</pre>
   483        </pre>
   135 </div>
   484       </div>
   136 <div class="section" title="1.1.4.Multiplication">
   485       <div class="section" title="1.1.4.Multiplication">
   137 <div class="titlepage"><div><div><h4 class="title">
   486        <div class="titlepage">
   138 <a name="id2947472"></a>1.1.4.Multiplication</h4></div></div></div>
   487         <div>
   139 <p id="ch3list_tuples_9">A <span class="emphasis"><em>List</em></span> can be multiplied with an integer to repeat itself:</p>
   488          <div>
   140 <pre class="programlisting">
   489           <h4 class="title">
   141 &gt;&gt;&gt; [20] * 5
   490            <a name="id3002905">
       
   491            </a>
       
   492            1.1.4.Multiplication
       
   493           </h4>
       
   494          </div>
       
   495         </div>
       
   496        </div>
       
   497        <p id="ch3list_tuples_9">
       
   498         A
       
   499         <span class="emphasis">
       
   500          <em>
       
   501           List
       
   502          </em>
       
   503         </span>
       
   504         can be multiplied with an integer to repeat itself:
       
   505        </p>
       
   506        <pre class="programlisting">
       
   507         &gt;&gt;&gt; [20] * 5
       
   508 [20, 20, 20, 20, 20]
   142 [20, 20, 20, 20, 20]
   509 &gt;&gt;&gt; [42, 'Python', 54] * 3
   143 &gt;&gt;&gt; [42, 'Python', 54] * 3
   510 [42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54]
   144 [42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54]</pre>
   511        </pre>
   145 </div>
   512       </div>
   146 <div class="section" title="1.1.5.Membership">
   513       <div class="section" title="1.1.5.Membership">
   147 <div class="titlepage"><div><div><h4 class="title">
   514        <div class="titlepage">
   148 <a name="id2947497"></a>1.1.5.Membership</h4></div></div></div>
   515         <div>
   149 <p id="ch3list_tuples_a"><span class="strong"><strong>in</strong></span> operator is used to find whether an element is part of the <span class="emphasis"><em>List</em></span>. It
   516          <div>
   150 returns <span class="strong"><strong>True</strong></span> if the element is present in the <span class="emphasis"><em>List</em></span> or <span class="strong"><strong>False</strong></span> if it is not
   517           <h4 class="title">
       
   518            <a name="id3002929">
       
   519            </a>
       
   520            1.1.5.Membership
       
   521           </h4>
       
   522          </div>
       
   523         </div>
       
   524        </div>
       
   525        <p id="ch3list_tuples_a">
       
   526         <span class="strong">
       
   527          <strong>
       
   528           in
       
   529          </strong>
       
   530         </span>
       
   531         operator is used to find whether an element is part of the
       
   532         <span class="emphasis">
       
   533          <em>
       
   534           List
       
   535          </em>
       
   536         </span>
       
   537         . It
       
   538 returns
       
   539         <span class="strong">
       
   540          <strong>
       
   541           True
       
   542          </strong>
       
   543         </span>
       
   544         if the element is present in the
       
   545         <span class="emphasis">
       
   546          <em>
       
   547           List
       
   548          </em>
       
   549         </span>
       
   550         or
       
   551         <span class="strong">
       
   552          <strong>
       
   553           False
       
   554          </strong>
       
   555         </span>
       
   556         if it is not
       
   557 present. Since this operator returns a Boolean value it is called a Boolean
   151 present. Since this operator returns a Boolean value it is called a Boolean
   558 operator:
   152 operator:</p>
   559        </p>
   153 <pre class="programlisting">
   560        <pre class="programlisting">
   154 &gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
   561         &gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
       
   562 &gt;&gt;&gt; 'Tim' in names
   155 &gt;&gt;&gt; 'Tim' in names
   563 True
   156 True
   564 &gt;&gt;&gt; 'Adam' in names
   157 &gt;&gt;&gt; 'Adam' in names
   565 False
   158 False</pre>
   566        </pre>
   159 </div>
   567       </div>
   160 <div class="section" title="1.1.6.Length, Maximum and Minimum">
   568       <div class="section" title="1.1.6.Length, Maximum and Minimum">
   161 <div class="titlepage"><div><div><h4 class="title">
   569        <div class="titlepage">
   162 <a name="id2947539"></a>1.1.6.Length, Maximum and Minimum</h4></div></div></div>
   570         <div>
   163 <p id="ch3list_tuples_b">Length of a <span class="emphasis"><em>List</em></span> can be found out using the len function. The max function
   571          <div>
       
   572           <h4 class="title">
       
   573            <a name="id3002972">
       
   574            </a>
       
   575            1.1.6.Length, Maximum and Minimum
       
   576           </h4>
       
   577          </div>
       
   578         </div>
       
   579        </div>
       
   580        <p id="ch3list_tuples_b">
       
   581         Length of a
       
   582         <span class="emphasis">
       
   583          <em>
       
   584           List
       
   585          </em>
       
   586         </span>
       
   587         can be found out using the len function. The max function
       
   588 returns the element with the largest value and the min function returns the
   164 returns the element with the largest value and the min function returns the
   589 element with the smallest value:
   165 element with the smallest value:</p>
   590        </p>
   166 <pre class="programlisting">
   591        <pre class="programlisting">
   167 &gt;&gt;&gt; num = [4, 1, 32, 12, 67, 34, 65]
   592         &gt;&gt;&gt; num = [4, 1, 32, 12, 67, 34, 65]
       
   593 &gt;&gt;&gt; len(num)
   168 &gt;&gt;&gt; len(num)
   594 7
   169 7
   595 &gt;&gt;&gt; max(num)
   170 &gt;&gt;&gt; max(num)
   596 67
   171 67
   597 &gt;&gt;&gt; min(num)
   172 &gt;&gt;&gt; min(num)
   598 1
   173 1</pre>
   599        </pre>
   174 </div>
   600       </div>
   175 <div class="section" title="1.1.7.Changing Elements">
   601       <div class="section" title="1.1.7.Changing Elements">
   176 <div class="titlepage"><div><div><h4 class="title">
   602        <div class="titlepage">
   177 <a name="id2947564"></a>1.1.7.Changing Elements</h4></div></div></div>
   603         <div>
   178 <p id="ch3list_tuples_c">Unlike Strings <span class="emphasis"><em>Lists</em></span> are mutable, i.e. elements of a <span class="emphasis"><em>List</em></span> can be manipulated:</p>
   604          <div>
   179 <pre class="programlisting">
   605           <h4 class="title">
   180 &gt;&gt;&gt; a = [1, 3, 5, 7]
   606            <a name="id3002997">
       
   607            </a>
       
   608            1.1.7.Changing Elements
       
   609           </h4>
       
   610          </div>
       
   611         </div>
       
   612        </div>
       
   613        <p id="ch3list_tuples_c">
       
   614         Unlike Strings
       
   615         <span class="emphasis">
       
   616          <em>
       
   617           Lists
       
   618          </em>
       
   619         </span>
       
   620         are mutable, i.e. elements of a
       
   621         <span class="emphasis">
       
   622          <em>
       
   623           List
       
   624          </em>
       
   625         </span>
       
   626         can be manipulated:
       
   627        </p>
       
   628        <pre class="programlisting">
       
   629         &gt;&gt;&gt; a = [1, 3, 5, 7]
       
   630 &gt;&gt;&gt; a[2] = 9
   181 &gt;&gt;&gt; a[2] = 9
   631 &gt;&gt;&gt; a
   182 &gt;&gt;&gt; a
   632 [1, 3, 9, 7]
   183 [1, 3, 9, 7]</pre>
   633        </pre>
   184 </div>
   634       </div>
   185 <div class="section" title="1.1.8.Deleting Elements">
   635       <div class="section" title="1.1.8.Deleting Elements">
   186 <div class="titlepage"><div><div><h4 class="title">
   636        <div class="titlepage">
   187 <a name="id2947590"></a>1.1.8.Deleting Elements</h4></div></div></div>
   637         <div>
   188 <p id="ch3list_tuples_d">An element or a slice of a <span class="emphasis"><em>List</em></span> can be deleted by using the <span class="strong"><strong>del</strong></span> statement:</p>
   638          <div>
   189 <pre class="programlisting">
   639           <h4 class="title">
   190 &gt;&gt;&gt; a = [1, 3, 5, 7, 9, 11]
   640            <a name="id3003022">
       
   641            </a>
       
   642            1.1.8.Deleting Elements
       
   643           </h4>
       
   644          </div>
       
   645         </div>
       
   646        </div>
       
   647        <p id="ch3list_tuples_d">
       
   648         An element or a slice of a
       
   649         <span class="emphasis">
       
   650          <em>
       
   651           List
       
   652          </em>
       
   653         </span>
       
   654         can be deleted by using the
       
   655         <span class="strong">
       
   656          <strong>
       
   657           del
       
   658          </strong>
       
   659         </span>
       
   660         statement:
       
   661        </p>
       
   662        <pre class="programlisting">
       
   663         &gt;&gt;&gt; a = [1, 3, 5, 7, 9, 11]
       
   664 &gt;&gt;&gt; del a[-2:]
   191 &gt;&gt;&gt; del a[-2:]
   665 &gt;&gt;&gt; a
   192 &gt;&gt;&gt; a
   666 [1, 3, 5, 7]
   193 [1, 3, 5, 7]
   667 &gt;&gt;&gt; del a[1]
   194 &gt;&gt;&gt; del a[1]
   668 &gt;&gt;&gt; a
   195 &gt;&gt;&gt; a
   669 [1, 5, 7]
   196 [1, 5, 7]</pre>
   670        </pre>
   197 </div>
   671       </div>
   198 <div class="section" title="1.1.9.Assign to Slices">
   672       <div class="section" title="1.1.9.Assign to Slices">
   199 <div class="titlepage"><div><div><h4 class="title">
   673        <div class="titlepage">
   200 <a name="id2947617"></a>1.1.9.Assign to Slices</h4></div></div></div>
   674         <div>
   201 <p id="ch3list_tuples_e">In the same way, values can be assigned to individual elements of the <span class="emphasis"><em>List</em></span>,
   675          <div>
   202 a <span class="emphasis"><em>List</em></span> of elements can be assigned to a slice:</p>
   676           <h4 class="title">
   203 <pre class="programlisting">
   677            <a name="id3003050">
   204 &gt;&gt;&gt; a = [2, 3, 4, 5]
   678            </a>
       
   679            1.1.9.Assign to Slices
       
   680           </h4>
       
   681          </div>
       
   682         </div>
       
   683        </div>
       
   684        <p id="ch3list_tuples_e">
       
   685         In the same way, values can be assigned to individual elements of the
       
   686         <span class="emphasis">
       
   687          <em>
       
   688           List
       
   689          </em>
       
   690         </span>
       
   691         ,
       
   692 a
       
   693         <span class="emphasis">
       
   694          <em>
       
   695           List
       
   696          </em>
       
   697         </span>
       
   698         of elements can be assigned to a slice:
       
   699        </p>
       
   700        <pre class="programlisting">
       
   701         &gt;&gt;&gt; a = [2, 3, 4, 5]
       
   702 &gt;&gt;&gt; a[:2] = [0, 1]
   205 &gt;&gt;&gt; a[:2] = [0, 1]
   703 [0, 1, 4, 5]
   206 [0, 1, 4, 5]
   704 &gt;&gt;&gt; a[2:2] = [2, 3]
   207 &gt;&gt;&gt; a[2:2] = [2, 3]
   705 &gt;&gt;&gt; a
   208 &gt;&gt;&gt; a
   706 [0, 1, 2, 3, 4, 5]
   209 [0, 1, 2, 3, 4, 5]
   707 &gt;&gt;&gt; a[2:4] = []
   210 &gt;&gt;&gt; a[2:4] = []
   708 &gt;&gt;&gt; a
   211 &gt;&gt;&gt; a
   709 [0, 1, 4, 5]
   212 [0, 1, 4, 5]</pre>
   710        </pre>
   213 <p id="ch3list_tuples_f">The last two examples should be particularly noted carefully. The last but one
   711        <p id="ch3list_tuples_f">
   214 example insert elements or a list of elements into a <span class="emphasis"><em>List</em></span> and the last example
   712         The last two examples should be particularly noted carefully. The last but one
   215 deletes a list of elements from the <span class="emphasis"><em>List</em></span>.</p>
   713 example insert elements or a list of elements into a
   216 </div>
   714         <span class="emphasis">
   217 </div>
   715          <em>
   218 <div class="section" title="1.2.None, Empty Lists, and Initialization">
   716           List
   219 <div class="titlepage"><div><div><h3 class="title">
   717          </em>
   220 <a name="id2947658"></a>1.2.None, Empty Lists, and Initialization</h3></div></div></div>
   718         </span>
   221 <p id="ch3list_tuples_10">An <span class="emphasis"><em>Empty List</em></span> is a <span class="emphasis"><em>List</em></span> with no elements and is simply represented as
   719         and the last example
   222 []. A <span class="emphasis"><em>None List</em></span> is one with all elements in it being <span class="strong"><strong>None</strong></span>. It serves
   720 deletes a list of elements from the
       
   721         <span class="emphasis">
       
   722          <em>
       
   723           List
       
   724          </em>
       
   725         </span>
       
   726         .
       
   727        </p>
       
   728       </div>
       
   729      </div>
       
   730      <div class="section" title="1.2.None, Empty Lists, and Initialization">
       
   731       <div class="titlepage">
       
   732        <div>
       
   733         <div>
       
   734          <h3 class="title">
       
   735           <a name="id3003090">
       
   736           </a>
       
   737           1.2.None, Empty Lists, and Initialization
       
   738          </h3>
       
   739         </div>
       
   740        </div>
       
   741       </div>
       
   742       <p id="ch3list_tuples_10">
       
   743        An
       
   744        <span class="emphasis">
       
   745         <em>
       
   746          Empty List
       
   747         </em>
       
   748        </span>
       
   749        is a
       
   750        <span class="emphasis">
       
   751         <em>
       
   752          List
       
   753         </em>
       
   754        </span>
       
   755        with no elements and is simply represented as
       
   756 []. A
       
   757        <span class="emphasis">
       
   758         <em>
       
   759          None List
       
   760         </em>
       
   761        </span>
       
   762        is one with all elements in it being
       
   763        <span class="strong">
       
   764         <strong>
       
   765          None
       
   766         </strong>
       
   767        </span>
       
   768        . It serves
       
   769 the purpose having a container list of some fixed number of elements with
   223 the purpose having a container list of some fixed number of elements with
   770 no value:
   224 no value:</p>
   771       </p>
   225 <pre class="programlisting">
   772       <pre class="programlisting">
   226 &gt;&gt;&gt; a = []
   773        &gt;&gt;&gt; a = []
       
   774 &gt;&gt;&gt; a
   227 &gt;&gt;&gt; a
   775 []
   228 []
   776 &gt;&gt;&gt; n = [None] * 10
   229 &gt;&gt;&gt; n = [None] * 10
   777 &gt;&gt;&gt; n
   230 &gt;&gt;&gt; n
   778 [None, None, None, None, None, None, None, None, None, None]
   231 [None, None, None, None, None, None, None, None, None, None]</pre>
   779       </pre>
   232 </div>
   780      </div>
   233 <div class="section" title="1.3.Nested Lists">
   781      <div class="section" title="1.3.Nested Lists">
   234 <div class="titlepage"><div><div><h3 class="title">
   782       <div class="titlepage">
   235 <a name="id2947698"></a>1.3.Nested Lists</h3></div></div></div>
   783        <div>
   236 <p id="ch3list_tuples_11">As mentioned earlier, a List can contain elements of any data type. This also
   784         <div>
   237 implies a <span class="emphasis"><em>List</em></span> can have a <span class="emphasis"><em>Lists</em></span> themselves as its elements. These are
   785          <h3 class="title">
   238 called as <span class="emphasis"><em>Nested Lists</em></span>. There is no limit on the depth of the <span class="emphasis"><em>Nested Lists</em></span>:</p>
   786           <a name="id3003130">
   239 <pre class="programlisting">
   787           </a>
   240 &gt;&gt;&gt; a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7]</pre>
   788           1.3.Nested Lists
   241 </div>
   789          </h3>
   242 <div class="section" title="1.4.List Methods">
   790         </div>
   243 <div class="titlepage"><div><div><h3 class="title">
   791        </div>
   244 <a name="id2947732"></a>1.4.List Methods</h3></div></div></div>
   792       </div>
   245 <p id="ch3list_tuples_12">A method is a function that is coupled to an object. More about objects
   793       <p id="ch3list_tuples_11">
       
   794        As mentioned earlier, a List can contain elements of any data type. This also
       
   795 implies a
       
   796        <span class="emphasis">
       
   797         <em>
       
   798          List
       
   799         </em>
       
   800        </span>
       
   801        can have a
       
   802        <span class="emphasis">
       
   803         <em>
       
   804          Lists
       
   805         </em>
       
   806        </span>
       
   807        themselves as its elements. These are
       
   808 called as
       
   809        <span class="emphasis">
       
   810         <em>
       
   811          Nested Lists
       
   812         </em>
       
   813        </span>
       
   814        . There is no limit on the depth of the
       
   815        <span class="emphasis">
       
   816         <em>
       
   817          Nested Lists
       
   818         </em>
       
   819        </span>
       
   820        :
       
   821       </p>
       
   822       <pre class="programlisting">
       
   823        &gt;&gt;&gt; a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7]
       
   824       </pre>
       
   825      </div>
       
   826      <div class="section" title="1.4.List Methods">
       
   827       <div class="titlepage">
       
   828        <div>
       
   829         <div>
       
   830          <h3 class="title">
       
   831           <a name="id3003165">
       
   832           </a>
       
   833           1.4.List Methods
       
   834          </h3>
       
   835         </div>
       
   836        </div>
       
   837       </div>
       
   838       <p id="ch3list_tuples_12">
       
   839        A method is a function that is coupled to an object. More about objects
       
   840 and its methods are discussed in Advanced Python module. In general, a
   246 and its methods are discussed in Advanced Python module. In general, a
   841 method is called like:
   247 method is called like:</p>
   842       </p>
   248 <pre class="programlisting">
   843       <pre class="programlisting">
   249 object.method(arguments)</pre>
   844        object.method(arguments)
   250 <p id="ch3list_tuples_13">For now, it is enough to know that a list of elements is an object and
   845       </pre>
   251 so <span class="emphasis"><em>List</em></span> methods can be called upon them. Also some of the methods change
   846       <p id="ch3list_tuples_13">
   252 the <span class="emphasis"><em>List</em></span> in-place, meaning it modifies the existing list instead of creating
   847        For now, it is enough to know that a list of elements is an object and
       
   848 so
       
   849        <span class="emphasis">
       
   850         <em>
       
   851          List
       
   852         </em>
       
   853        </span>
       
   854        methods can be called upon them. Also some of the methods change
       
   855 the
       
   856        <span class="emphasis">
       
   857         <em>
       
   858          List
       
   859         </em>
       
   860        </span>
       
   861        in-place, meaning it modifies the existing list instead of creating
       
   862 a new one, while other methods don't. It must be noted as we run through
   253 a new one, while other methods don't. It must be noted as we run through
   863 the
   254 the <span class="emphasis"><em>List</em></span> methods.</p>
   864        <span class="emphasis">
   255 <p id="ch3list_tuples_14">Some of the most commonly used <span class="emphasis"><em>List</em></span> methods are as follows:</p>
   865         <em>
   256 <div class="section" title="1.4.1.append">
   866          List
   257 <div class="titlepage"><div><div><h4 class="title">
   867         </em>
   258 <a name="id2947779"></a>1.4.1.append</h4></div></div></div>
   868        </span>
   259 <p id="ch3list_tuples_15">The <span class="emphasis"><em>append</em></span> method is used to append an object at the end of the list:</p>
   869        methods.
   260 <pre class="programlisting">
   870       </p>
   261 &gt;&gt;&gt; prime = [2, 3, 5]
   871       <p id="ch3list_tuples_14">
       
   872        Some of the most commonly used
       
   873        <span class="emphasis">
       
   874         <em>
       
   875          List
       
   876         </em>
       
   877        </span>
       
   878        methods are as follows:
       
   879       </p>
       
   880       <div class="section" title="1.4.1.append">
       
   881        <div class="titlepage">
       
   882         <div>
       
   883          <div>
       
   884           <h4 class="title">
       
   885            <a name="id3003212">
       
   886            </a>
       
   887            1.4.1.append
       
   888           </h4>
       
   889          </div>
       
   890         </div>
       
   891        </div>
       
   892        <p id="ch3list_tuples_15">
       
   893         The
       
   894         <span class="emphasis">
       
   895          <em>
       
   896           append
       
   897          </em>
       
   898         </span>
       
   899         method is used to append an object at the end of the list:
       
   900        </p>
       
   901        <pre class="programlisting">
       
   902         &gt;&gt;&gt; prime = [2, 3, 5]
       
   903 &gt;&gt;&gt; prime.append(7)
   262 &gt;&gt;&gt; prime.append(7)
   904 &gt;&gt;&gt; prime
   263 &gt;&gt;&gt; prime
   905 [2, 3, 5, 7]
   264 [2, 3, 5, 7]</pre>
   906        </pre>
   265 <p id="ch3list_tuples_16">It is important to note that append changes the <span class="emphasis"><em>List</em></span> in-place.</p>
   907        <p id="ch3list_tuples_16">
   266 </div>
   908         It is important to note that append changes the
   267 <div class="section" title="1.4.2.count">
   909         <span class="emphasis">
   268 <div class="titlepage"><div><div><h4 class="title">
   910          <em>
   269 <a name="id2947808"></a>1.4.2.count</h4></div></div></div>
   911           List
   270 <p id="ch3list_tuples_17">The <span class="emphasis"><em>count</em></span> method returns the number of occurences of a particular element
   912          </em>
   271 in a list:</p>
   913         </span>
   272 <pre class="programlisting">
   914         in-place.
   273 &gt;&gt;&gt; [1, 4, 4, 9, 9, 9].count(9)
   915        </p>
       
   916       </div>
       
   917       <div class="section" title="1.4.2.count">
       
   918        <div class="titlepage">
       
   919         <div>
       
   920          <div>
       
   921           <h4 class="title">
       
   922            <a name="id3003241">
       
   923            </a>
       
   924            1.4.2.count
       
   925           </h4>
       
   926          </div>
       
   927         </div>
       
   928        </div>
       
   929        <p id="ch3list_tuples_17">
       
   930         The
       
   931         <span class="emphasis">
       
   932          <em>
       
   933           count
       
   934          </em>
       
   935         </span>
       
   936         method returns the number of occurences of a particular element
       
   937 in a list:
       
   938        </p>
       
   939        <pre class="programlisting">
       
   940         &gt;&gt;&gt; [1, 4, 4, 9, 9, 9].count(9)
       
   941 3
   274 3
   942 &gt;&gt;&gt; tlst = ['Python', 'is', 'a', 'beautiful', 'language']
   275 &gt;&gt;&gt; tlst = ['Python', 'is', 'a', 'beautiful', 'language']
   943 &gt;&gt;&gt; tlst.count('Python')
   276 &gt;&gt;&gt; tlst.count('Python')
   944 1
   277 1</pre>
   945        </pre>
   278 </div>
   946       </div>
   279 <div class="section" title="1.4.3.extend">
   947       <div class="section" title="1.4.3.extend">
   280 <div class="titlepage"><div><div><h4 class="title">
   948        <div class="titlepage">
   281 <a name="id2947831"></a>1.4.3.extend</h4></div></div></div>
   949         <div>
   282 <p id="ch3list_tuples_18">The <span class="emphasis"><em>extend</em></span> method extends the list on which it is called by the list supplied
   950          <div>
   283 as argument to it:</p>
   951           <h4 class="title">
   284 <pre class="programlisting">
   952            <a name="id3003264">
   285 &gt;&gt;&gt; a = [1, 2, 3]
   953            </a>
       
   954            1.4.3.extend
       
   955           </h4>
       
   956          </div>
       
   957         </div>
       
   958        </div>
       
   959        <p id="ch3list_tuples_18">
       
   960         The
       
   961         <span class="emphasis">
       
   962          <em>
       
   963           extend
       
   964          </em>
       
   965         </span>
       
   966         method extends the list on which it is called by the list supplied
       
   967 as argument to it:
       
   968        </p>
       
   969        <pre class="programlisting">
       
   970         &gt;&gt;&gt; a = [1, 2, 3]
       
   971 &gt;&gt;&gt; b = [4, 5, 6]
   286 &gt;&gt;&gt; b = [4, 5, 6]
   972 &gt;&gt;&gt; a.extend(b)
   287 &gt;&gt;&gt; a.extend(b)
   973 [1, 2, 3, 4, 5, 6]
   288 [1, 2, 3, 4, 5, 6]</pre>
   974        </pre>
   289 <p id="ch3list_tuples_19">This is an in-place method. This method is equivalent to using the + operator, but
   975        <p id="ch3list_tuples_19">
   290 using the + operator returns a new list.</p>
   976         This is an in-place method. This method is equivalent to using the + operator, but
   291 </div>
   977 using the + operator returns a new list.
   292 <div class="section" title="1.4.4.index">
   978        </p>
   293 <div class="titlepage"><div><div><h4 class="title">
   979       </div>
   294 <a name="id2947859"></a>1.4.4.index</h4></div></div></div>
   980       <div class="section" title="1.4.4.index">
   295 <p id="ch3list_tuples_1a">The <span class="emphasis"><em>index</em></span> method returns the index position of the element in the list
   981        <div class="titlepage">
   296 specified as argument:</p>
   982         <div>
   297 <pre class="programlisting">
   983          <div>
   298 &gt;&gt;&gt; a = [1, 2, 3, ,4, 5]
   984           <h4 class="title">
       
   985            <a name="id3003292">
       
   986            </a>
       
   987            1.4.4.index
       
   988           </h4>
       
   989          </div>
       
   990         </div>
       
   991        </div>
       
   992        <p id="ch3list_tuples_1a">
       
   993         The
       
   994         <span class="emphasis">
       
   995          <em>
       
   996           index
       
   997          </em>
       
   998         </span>
       
   999         method returns the index position of the element in the list
       
  1000 specified as argument:
       
  1001        </p>
       
  1002        <pre class="programlisting">
       
  1003         &gt;&gt;&gt; a = [1, 2, 3, ,4, 5]
       
  1004 &gt;&gt;&gt; a.index(4)
   299 &gt;&gt;&gt; a.index(4)
  1005 3
   300 3</pre>
  1006        </pre>
   301 </div>
  1007       </div>
   302 <div class="section" title="1.4.5.insert">
  1008       <div class="section" title="1.4.5.insert">
   303 <div class="titlepage"><div><div><h4 class="title">
  1009        <div class="titlepage">
   304 <a name="id2947879"></a>1.4.5.insert</h4></div></div></div>
  1010         <div>
   305 <p id="ch3list_tuples_1b">The <span class="emphasis"><em>insert</em></span> method is used to insert an element specified as the second
  1011          <div>
   306 argument to the list at the position specified by the first argument:</p>
  1012           <h4 class="title">
   307 <pre class="programlisting">
  1013            <a name="id3003311">
   308 &gt;&gt;&gt; a = ['Python', 'is', 'cool']
  1014            </a>
       
  1015            1.4.5.insert
       
  1016           </h4>
       
  1017          </div>
       
  1018         </div>
       
  1019        </div>
       
  1020        <p id="ch3list_tuples_1b">
       
  1021         The
       
  1022         <span class="emphasis">
       
  1023          <em>
       
  1024           insert
       
  1025          </em>
       
  1026         </span>
       
  1027         method is used to insert an element specified as the second
       
  1028 argument to the list at the position specified by the first argument:
       
  1029        </p>
       
  1030        <pre class="programlisting">
       
  1031         &gt;&gt;&gt; a = ['Python', 'is', 'cool']
       
  1032 &gt;&gt;&gt; a.insert(2, 'so')
   309 &gt;&gt;&gt; a.insert(2, 'so')
  1033 &gt;&gt;&gt; a
   310 &gt;&gt;&gt; a
  1034 ['Python', 'is', 'so', 'cool']
   311 ['Python', 'is', 'so', 'cool']</pre>
  1035        </pre>
   312 <p id="ch3list_tuples_1c">The <span class="emphasis"><em>insert</em></span> method changes the <span class="emphasis"><em>List</em></span> in-place.</p>
  1036        <p id="ch3list_tuples_1c">
   313 </div>
  1037         The
   314 <div class="section" title="1.4.6.pop">
  1038         <span class="emphasis">
   315 <div class="titlepage"><div><div><h4 class="title">
  1039          <em>
   316 <a name="id2947913"></a>1.4.6.pop</h4></div></div></div>
  1040           insert
   317 <p id="ch3list_tuples_1d">The <span class="emphasis"><em>pop</em></span> method removes an element from the list. The index position
  1041          </em>
       
  1042         </span>
       
  1043         method changes the
       
  1044         <span class="emphasis">
       
  1045          <em>
       
  1046           List
       
  1047          </em>
       
  1048         </span>
       
  1049         in-place.
       
  1050        </p>
       
  1051       </div>
       
  1052       <div class="section" title="1.4.6.pop">
       
  1053        <div class="titlepage">
       
  1054         <div>
       
  1055          <div>
       
  1056           <h4 class="title">
       
  1057            <a name="id3003346">
       
  1058            </a>
       
  1059            1.4.6.pop
       
  1060           </h4>
       
  1061          </div>
       
  1062         </div>
       
  1063        </div>
       
  1064        <p id="ch3list_tuples_1d">
       
  1065         The
       
  1066         <span class="emphasis">
       
  1067          <em>
       
  1068           pop
       
  1069          </em>
       
  1070         </span>
       
  1071         method removes an element from the list. The index position
       
  1072 of the element to be removed can be specified as an argument to the
   318 of the element to be removed can be specified as an argument to the
  1073         <span class="emphasis">
   319 <span class="emphasis"><em>pop</em></span> method, if not it removes the last element by default:</p>
  1074          <em>
   320 <pre class="programlisting">
  1075           pop
   321 &gt;&gt;&gt; a = [1, 2, 3, 4, 5]
  1076          </em>
       
  1077         </span>
       
  1078         method, if not it removes the last element by default:
       
  1079        </p>
       
  1080        <pre class="programlisting">
       
  1081         &gt;&gt;&gt; a = [1, 2, 3, 4, 5]
       
  1082 &gt;&gt;&gt; a.pop()
   322 &gt;&gt;&gt; a.pop()
  1083 &gt;&gt;&gt; a
   323 &gt;&gt;&gt; a
  1084 5
   324 5
  1085 &gt;&gt;&gt; a.pop(2)
   325 &gt;&gt;&gt; a.pop(2)
  1086 &gt;&gt;&gt; a
   326 &gt;&gt;&gt; a
  1087 3
   327 3</pre>
  1088        </pre>
   328 <p id="ch3list_tuples_1e">The <span class="emphasis"><em>pop</em></span> method changes the <span class="emphasis"><em>List</em></span> in-place.</p>
  1089        <p id="ch3list_tuples_1e">
   329 </div>
  1090         The
   330 <div class="section" title="1.4.7.remove">
  1091         <span class="emphasis">
   331 <div class="titlepage"><div><div><h4 class="title">
  1092          <em>
   332 <a name="id2947951"></a>1.4.7.remove</h4></div></div></div>
  1093           pop
   333 <p id="ch3list_tuples_1f">The <span class="emphasis"><em>remove</em></span> method removes the first occurence of an element supplied as a
  1094          </em>
   334 parameter:</p>
  1095         </span>
   335 <pre class="programlisting">
  1096         method changes the
   336 &gt;&gt;&gt; a = [1, 2, 3, 4, 2, 5, 2]
  1097         <span class="emphasis">
       
  1098          <em>
       
  1099           List
       
  1100          </em>
       
  1101         </span>
       
  1102         in-place.
       
  1103        </p>
       
  1104       </div>
       
  1105       <div class="section" title="1.4.7.remove">
       
  1106        <div class="titlepage">
       
  1107         <div>
       
  1108          <div>
       
  1109           <h4 class="title">
       
  1110            <a name="id3003383">
       
  1111            </a>
       
  1112            1.4.7.remove
       
  1113           </h4>
       
  1114          </div>
       
  1115         </div>
       
  1116        </div>
       
  1117        <p id="ch3list_tuples_1f">
       
  1118         The
       
  1119         <span class="emphasis">
       
  1120          <em>
       
  1121           remove
       
  1122          </em>
       
  1123         </span>
       
  1124         method removes the first occurence of an element supplied as a
       
  1125 parameter:
       
  1126        </p>
       
  1127        <pre class="programlisting">
       
  1128         &gt;&gt;&gt; a = [1, 2, 3, 4, 2, 5, 2]
       
  1129 &gt;&gt;&gt; a.remove(2)
   337 &gt;&gt;&gt; a.remove(2)
  1130 &gt;&gt;&gt; a
   338 &gt;&gt;&gt; a
  1131 [1, 3, 4, 2, 5, 2]
   339 [1, 3, 4, 2, 5, 2]</pre>
  1132        </pre>
   340 </div>
  1133       </div>
   341 <div class="section" title="1.4.8.reverse">
  1134       <div class="section" title="1.4.8.reverse">
   342 <div class="titlepage"><div><div><h4 class="title">
  1135        <div class="titlepage">
   343 <a name="id2947972"></a>1.4.8.reverse</h4></div></div></div>
  1136         <div>
   344 <p id="ch3list_tuples_20">The <span class="emphasis"><em>reverse</em></span> method reverses elements in the list. It is important to note
  1137          <div>
   345 here that <span class="emphasis"><em>reverse</em></span> method changes the list in-place and doesn't return any
  1138           <h4 class="title">
   346 thing:</p>
  1139            <a name="id3003405">
   347 <pre class="programlisting">
  1140            </a>
   348 &gt;&gt;&gt; a = ['guido', 'alex', 'tim']
  1141            1.4.8.reverse
       
  1142           </h4>
       
  1143          </div>
       
  1144         </div>
       
  1145        </div>
       
  1146        <p id="ch3list_tuples_20">
       
  1147         The
       
  1148         <span class="emphasis">
       
  1149          <em>
       
  1150           reverse
       
  1151          </em>
       
  1152         </span>
       
  1153         method reverses elements in the list. It is important to note
       
  1154 here that
       
  1155         <span class="emphasis">
       
  1156          <em>
       
  1157           reverse
       
  1158          </em>
       
  1159         </span>
       
  1160         method changes the list in-place and doesn't return any
       
  1161 thing:
       
  1162        </p>
       
  1163        <pre class="programlisting">
       
  1164         &gt;&gt;&gt; a = ['guido', 'alex', 'tim']
       
  1165 &gt;&gt;&gt; a.reverse()
   349 &gt;&gt;&gt; a.reverse()
  1166 &gt;&gt;&gt; a
   350 &gt;&gt;&gt; a
  1167 ['tim', 'alex', 'guido']
   351 ['tim', 'alex', 'guido']</pre>
  1168        </pre>
   352 </div>
  1169       </div>
   353 <div class="section" title="1.4.9.sort">
  1170       <div class="section" title="1.4.9.sort">
   354 <div class="titlepage"><div><div><h4 class="title">
  1171        <div class="titlepage">
   355 <a name="id2947998"></a>1.4.9.sort</h4></div></div></div>
  1172         <div>
   356 <p id="ch3list_tuples_21">The <span class="emphasis"><em>sort</em></span> method is used to sort the elements of the list. The <span class="emphasis"><em>sort</em></span> method
  1173          <div>
   357 also sorts in-place and does not return anything:</p>
  1174           <h4 class="title">
   358 <pre class="programlisting">
  1175            <a name="id3003431">
   359 &gt;&gt;&gt; a = [5, 1, 3, 7, 4]
  1176            </a>
       
  1177            1.4.9.sort
       
  1178           </h4>
       
  1179          </div>
       
  1180         </div>
       
  1181        </div>
       
  1182        <p id="ch3list_tuples_21">
       
  1183         The
       
  1184         <span class="emphasis">
       
  1185          <em>
       
  1186           sort
       
  1187          </em>
       
  1188         </span>
       
  1189         method is used to sort the elements of the list. The
       
  1190         <span class="emphasis">
       
  1191          <em>
       
  1192           sort
       
  1193          </em>
       
  1194         </span>
       
  1195         method
       
  1196 also sorts in-place and does not return anything:
       
  1197        </p>
       
  1198        <pre class="programlisting">
       
  1199         &gt;&gt;&gt; a = [5, 1, 3, 7, 4]
       
  1200 &gt;&gt;&gt; a.sort()
   360 &gt;&gt;&gt; a.sort()
  1201 &gt;&gt;&gt; a
   361 &gt;&gt;&gt; a
  1202 [1, 3, 4, 5, 7]
   362 [1, 3, 4, 5, 7]</pre>
  1203        </pre>
   363 <p id="ch3list_tuples_22">In addition to the sort method on a <span class="emphasis"><em>List</em></span> object we can also use the built-in
  1204        <p id="ch3list_tuples_22">
   364 <span class="strong"><strong>sorted</strong></span> function. This function takes the <span class="emphasis"><em>List</em></span> as a parameter and returns
  1205         In addition to the sort method on a
   365 a sorted copy of the list. However the original list is left intact:</p>
  1206         <span class="emphasis">
   366 <pre class="programlisting">
  1207          <em>
   367 &gt;&gt;&gt; a = [5, 1, 3, 7, 4]
  1208           List
       
  1209          </em>
       
  1210         </span>
       
  1211         object we can also use the built-in
       
  1212         <span class="strong">
       
  1213          <strong>
       
  1214           sorted
       
  1215          </strong>
       
  1216         </span>
       
  1217         function. This function takes the
       
  1218         <span class="emphasis">
       
  1219          <em>
       
  1220           List
       
  1221          </em>
       
  1222         </span>
       
  1223         as a parameter and returns
       
  1224 a sorted copy of the list. However the original list is left intact:
       
  1225        </p>
       
  1226        <pre class="programlisting">
       
  1227         &gt;&gt;&gt; a = [5, 1, 3, 7, 4]
       
  1228 &gt;&gt;&gt; b = sorted(a)
   368 &gt;&gt;&gt; b = sorted(a)
  1229 &gt;&gt;&gt; b
   369 &gt;&gt;&gt; b
  1230 [1, 3, 4, 5, 7]
   370 [1, 3, 4, 5, 7]
  1231 &gt;&gt;&gt; a
   371 &gt;&gt;&gt; a
  1232 [5, 1, 3, 7, 4]
   372 [5, 1, 3, 7, 4]</pre>
  1233        </pre>
   373 </div>
  1234       </div>
   374 </div>
  1235      </div>
   375 </div>
  1236     </div>
   376 <div class="section" title="2.Tuples">
  1237     <div class="section" title="2.Tuples">
   377 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1238      <div class="titlepage">
   378 <a name="id2948048"></a>2.Tuples</h2></div></div></div>
  1239       <div>
   379 <p id="ch3list_tuples_23"><span class="emphasis"><em>Tuples</em></span> are sequences just like <span class="emphasis"><em>Lists</em></span>, but they are immutable. In other
  1240        <div>
   380 words <span class="emphasis"><em>Tuples</em></span> provides a way to represent a group of items, where the group
  1241         <h2 class="title" style="clear: both">
   381 of items cannot be changed in any way. The syntax of a <span class="emphasis"><em>Tuple</em></span> is also very
  1242          <a name="id3003481">
   382 similar to <span class="emphasis"><em>List</em></span>. A <span class="emphasis"><em>Tuple</em></span> is represented with the list of items, called
  1243          </a>
   383 elements of the <span class="emphasis"><em>Tuple</em></span> separated by comma, with the entire list being enclosed
  1244          2.Tuples
   384 in parenthesis. It is not compulsory to use parenthesis around a <span class="emphasis"><em>Tuple</em></span> but
  1245         </h2>
   385 it may be necessary in some of the cases:</p>
  1246        </div>
   386 <pre class="programlisting">
  1247       </div>
   387 &gt;&gt;&gt; a = 1, 2, 3
  1248      </div>
       
  1249      <p id="ch3list_tuples_23">
       
  1250       <span class="emphasis">
       
  1251        <em>
       
  1252         Tuples
       
  1253        </em>
       
  1254       </span>
       
  1255       are sequences just like
       
  1256       <span class="emphasis">
       
  1257        <em>
       
  1258         Lists
       
  1259        </em>
       
  1260       </span>
       
  1261       , but they are immutable. In other
       
  1262 words
       
  1263       <span class="emphasis">
       
  1264        <em>
       
  1265         Tuples
       
  1266        </em>
       
  1267       </span>
       
  1268       provides a way to represent a group of items, where the group
       
  1269 of items cannot be changed in any way. The syntax of a
       
  1270       <span class="emphasis">
       
  1271        <em>
       
  1272         Tuple
       
  1273        </em>
       
  1274       </span>
       
  1275       is also very
       
  1276 similar to
       
  1277       <span class="emphasis">
       
  1278        <em>
       
  1279         List
       
  1280        </em>
       
  1281       </span>
       
  1282       . A
       
  1283       <span class="emphasis">
       
  1284        <em>
       
  1285         Tuple
       
  1286        </em>
       
  1287       </span>
       
  1288       is represented with the list of items, called
       
  1289 elements of the
       
  1290       <span class="emphasis">
       
  1291        <em>
       
  1292         Tuple
       
  1293        </em>
       
  1294       </span>
       
  1295       separated by comma, with the entire list being enclosed
       
  1296 in parenthesis. It is not compulsory to use parenthesis around a
       
  1297       <span class="emphasis">
       
  1298        <em>
       
  1299         Tuple
       
  1300        </em>
       
  1301       </span>
       
  1302       but
       
  1303 it may be necessary in some of the cases:
       
  1304      </p>
       
  1305      <pre class="programlisting">
       
  1306       &gt;&gt;&gt; a = 1, 2, 3
       
  1307 &gt;&gt;&gt; a
   388 &gt;&gt;&gt; a
  1308 (1, 2, 3)
   389 (1, 2, 3)
  1309 &gt;&gt;&gt; b = 1,
   390 &gt;&gt;&gt; b = 1,
  1310 &gt;&gt;&gt; b
   391 &gt;&gt;&gt; b
  1311 (1,)
   392 (1,)</pre>
  1312      </pre>
   393 <p id="ch3list_tuples_24">It is interesting to note the second example. Just a value followed by a comma
  1313      <p id="ch3list_tuples_24">
   394 automatically makes that an element of a <span class="emphasis"><em>Tuple</em></span> with only one element. It is
  1314       It is interesting to note the second example. Just a value followed by a comma
       
  1315 automatically makes that an element of a
       
  1316       <span class="emphasis">
       
  1317        <em>
       
  1318         Tuple
       
  1319        </em>
       
  1320       </span>
       
  1321       with only one element. It is
       
  1322 also important to note that, irrespective of input having a parenthesis, the
   395 also important to note that, irrespective of input having a parenthesis, the
  1323 output always has a parenthesis.
   396 output always has a parenthesis.</p>
  1324      </p>
   397 <p id="ch3list_tuples_25">The first example is also known as <span class="emphasis"><em>Tuple packing</em></span>, because values are being
  1325      <p id="ch3list_tuples_25">
   398 packed into a tuple. It is also possible to do <span class="emphasis"><em>Tuple unpacking</em></span> which is more
  1326       The first example is also known as
       
  1327       <span class="emphasis">
       
  1328        <em>
       
  1329         Tuple packing
       
  1330        </em>
       
  1331       </span>
       
  1332       , because values are being
       
  1333 packed into a tuple. It is also possible to do
       
  1334       <span class="emphasis">
       
  1335        <em>
       
  1336         Tuple unpacking
       
  1337        </em>
       
  1338       </span>
       
  1339       which is more
       
  1340 interesting. It is better to understand that by example. Say we have a
   399 interesting. It is better to understand that by example. Say we have a
  1341 co-ordinate pair from which we need to separate x and y co-ordinates:
   400 co-ordinate pair from which we need to separate x and y co-ordinates:</p>
  1342      </p>
   401 <pre class="programlisting">
  1343      <pre class="programlisting">
   402 &gt;&gt;&gt; a = (1, 2)
  1344       &gt;&gt;&gt; a = (1, 2)
       
  1345 &gt;&gt;&gt; x, y = a
   403 &gt;&gt;&gt; x, y = a
  1346 &gt;&gt;&gt; x
   404 &gt;&gt;&gt; x
  1347 1
   405 1
  1348 &gt;&gt;&gt; y
   406 &gt;&gt;&gt; y
  1349 2
   407 2</pre>
  1350      </pre>
   408 <p id="ch3list_tuples_26"><span class="emphasis"><em>Tuple unpacking</em></span> also has several other use-cases of which the most interesting
  1351      <p id="ch3list_tuples_26">
       
  1352       <span class="emphasis">
       
  1353        <em>
       
  1354         Tuple unpacking
       
  1355        </em>
       
  1356       </span>
       
  1357       also has several other use-cases of which the most interesting
       
  1358 one is to swap the values of two variables. Using programming languages like C
   409 one is to swap the values of two variables. Using programming languages like C
  1359 would require anywhere around 10 lines of code and an extra temporary variable
   410 would require anywhere around 10 lines of code and an extra temporary variable
  1360 to do this (including all the #include stuff). Python does it in the most
   411 to do this (including all the #include stuff). Python does it in the most
  1361 intuitive way in just one line. Say we want to swap the co-ordinates in the
   412 intuitive way in just one line. Say we want to swap the co-ordinates in the
  1362 above example:
   413 above example:</p>
  1363      </p>
   414 <pre class="programlisting">
  1364      <pre class="programlisting">
   415 &gt;&gt;&gt; x, y = y, x
  1365       &gt;&gt;&gt; x, y = y, x
       
  1366 &gt;&gt;&gt; x
   416 &gt;&gt;&gt; x
  1367 2
   417 2
  1368 &gt;&gt;&gt; y
   418 &gt;&gt;&gt; y
  1369 1
   419 1</pre>
  1370      </pre>
   420 <div class="section" title="2.1.Common Tuple Operations">
  1371      <div class="section" title="2.1.Common Tuple Operations">
   421 <div class="titlepage"><div><div><h3 class="title">
  1372       <div class="titlepage">
   422 <a name="id2948145"></a>2.1.Common Tuple Operations</h3></div></div></div>
  1373        <div>
   423 <p id="ch3list_tuples_27">There is no need to introduce all the <span class="emphasis"><em>Tuple</em></span> operations again, since <span class="emphasis"><em>Tuples</em></span>
  1374         <div>
   424 support the following operations that <span class="emphasis"><em>List</em></span> supports in exactly the same way:</p>
  1375          <h3 class="title">
   425 <div class="itemizedlist"><ul class="itemizedlist" type="*">
  1376           <a name="id3003578">
   426 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_28">Indexing</p></li>
  1377           </a>
   427 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_29">Concatenating</p></li>
  1378           2.1.Common Tuple Operations
   428 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2a">Slicing</p></li>
  1379          </h3>
   429 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2b">Membership</p></li>
  1380         </div>
   430 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2c">Multiplication</p></li>
  1381        </div>
   431 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_2d">Length, Maximum, Minimum</p></li>
  1382       </div>
   432 </ul></div>
  1383       <p id="ch3list_tuples_27">
   433 <p id="ch3list_tuples_2e">The following examples illustrate the above operations:</p>
  1384        There is no need to introduce all the
   434 <pre class="programlisting">
  1385        <span class="emphasis">
   435 &gt;&gt;&gt; a = (1, 2, 3, 4, 5, 6)
  1386         <em>
       
  1387          Tuple
       
  1388         </em>
       
  1389        </span>
       
  1390        operations again, since
       
  1391        <span class="emphasis">
       
  1392         <em>
       
  1393          Tuples
       
  1394         </em>
       
  1395        </span>
       
  1396        support the following operations that
       
  1397        <span class="emphasis">
       
  1398         <em>
       
  1399          List
       
  1400         </em>
       
  1401        </span>
       
  1402        supports in exactly the same way:
       
  1403       </p>
       
  1404       <div class="itemizedlist">
       
  1405        <ul class="itemizedlist" type="*">
       
  1406         <li class="listitem" style="list-style-type: *">
       
  1407          <p id="ch3list_tuples_28">
       
  1408           Indexing
       
  1409          </p>
       
  1410         </li>
       
  1411         <li class="listitem" style="list-style-type: *">
       
  1412          <p id="ch3list_tuples_29">
       
  1413           Concatenating
       
  1414          </p>
       
  1415         </li>
       
  1416         <li class="listitem" style="list-style-type: *">
       
  1417          <p id="ch3list_tuples_2a">
       
  1418           Slicing
       
  1419          </p>
       
  1420         </li>
       
  1421         <li class="listitem" style="list-style-type: *">
       
  1422          <p id="ch3list_tuples_2b">
       
  1423           Membership
       
  1424          </p>
       
  1425         </li>
       
  1426         <li class="listitem" style="list-style-type: *">
       
  1427          <p id="ch3list_tuples_2c">
       
  1428           Multiplication
       
  1429          </p>
       
  1430         </li>
       
  1431         <li class="listitem" style="list-style-type: *">
       
  1432          <p id="ch3list_tuples_2d">
       
  1433           Length, Maximum, Minimum
       
  1434          </p>
       
  1435         </li>
       
  1436        </ul>
       
  1437       </div>
       
  1438       <p id="ch3list_tuples_2e">
       
  1439        The following examples illustrate the above operations:
       
  1440       </p>
       
  1441       <pre class="programlisting">
       
  1442        &gt;&gt;&gt; a = (1, 2, 3, 4, 5, 6)
       
  1443 &gt;&gt;&gt; a[5]
   436 &gt;&gt;&gt; a[5]
  1444 6
   437 6
  1445 &gt;&gt;&gt; b = (7, 8, 9)
   438 &gt;&gt;&gt; b = (7, 8, 9)
  1446 &gt;&gt;&gt; a + b
   439 &gt;&gt;&gt; a + b
  1447 (1, 2, 3, 4, 5, 6, 7, 8, 9)
   440 (1, 2, 3, 4, 5, 6, 7, 8, 9)
  1455 &gt;&gt;&gt; len(a)
   448 &gt;&gt;&gt; len(a)
  1456 6
   449 6
  1457 &gt;&gt;&gt; max(a)
   450 &gt;&gt;&gt; max(a)
  1458 6
   451 6
  1459 &gt;&gt;&gt; min(a)
   452 &gt;&gt;&gt; min(a)
  1460 1
   453 1</pre>
  1461       </pre>
   454 <p id="ch3list_tuples_2f">However the following <span class="emphasis"><em>List</em></span> operations are not supported by <span class="emphasis"><em>Tuples</em></span> because
  1462       <p id="ch3list_tuples_2f">
   455 <span class="emphasis"><em>Tuples</em></span> cannot be changed once they are created:</p>
  1463        However the following
   456 <div class="itemizedlist"><ul class="itemizedlist" type="*">
  1464        <span class="emphasis">
   457 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_30">Changing elements</p></li>
  1465         <em>
   458 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_31">Deleting elements</p></li>
  1466          List
   459 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_32">Assigning to slices</p></li>
  1467         </em>
   460 </ul></div>
  1468        </span>
   461 <p id="ch3list_tuples_33">Similarity to <span class="emphasis"><em>Lists</em></span> leads to the questions like, why not <span class="emphasis"><em>Lists</em></span> only? Why do
  1469        operations are not supported by
   462 we even want <span class="emphasis"><em>Tuples</em></span>? Can we do the same with <span class="emphasis"><em>Lists</em></span>? And the answer is <span class="strong"><strong>Yes</strong></span>
  1470        <span class="emphasis">
   463 we can do it, but <span class="emphasis"><em>Tuples</em></span> are helpful at times, like we can return Tuples from
  1471         <em>
       
  1472          Tuples
       
  1473         </em>
       
  1474        </span>
       
  1475        because
       
  1476        <span class="emphasis">
       
  1477         <em>
       
  1478          Tuples
       
  1479         </em>
       
  1480        </span>
       
  1481        cannot be changed once they are created:
       
  1482       </p>
       
  1483       <div class="itemizedlist">
       
  1484        <ul class="itemizedlist" type="*">
       
  1485         <li class="listitem" style="list-style-type: *">
       
  1486          <p id="ch3list_tuples_30">
       
  1487           Changing elements
       
  1488          </p>
       
  1489         </li>
       
  1490         <li class="listitem" style="list-style-type: *">
       
  1491          <p id="ch3list_tuples_31">
       
  1492           Deleting elements
       
  1493          </p>
       
  1494         </li>
       
  1495         <li class="listitem" style="list-style-type: *">
       
  1496          <p id="ch3list_tuples_32">
       
  1497           Assigning to slices
       
  1498          </p>
       
  1499         </li>
       
  1500        </ul>
       
  1501       </div>
       
  1502       <p id="ch3list_tuples_33">
       
  1503        Similarity to
       
  1504        <span class="emphasis">
       
  1505         <em>
       
  1506          Lists
       
  1507         </em>
       
  1508        </span>
       
  1509        leads to the questions like, why not
       
  1510        <span class="emphasis">
       
  1511         <em>
       
  1512          Lists
       
  1513         </em>
       
  1514        </span>
       
  1515        only? Why do
       
  1516 we even want
       
  1517        <span class="emphasis">
       
  1518         <em>
       
  1519          Tuples
       
  1520         </em>
       
  1521        </span>
       
  1522        ? Can we do the same with
       
  1523        <span class="emphasis">
       
  1524         <em>
       
  1525          Lists
       
  1526         </em>
       
  1527        </span>
       
  1528        ? And the answer is
       
  1529        <span class="strong">
       
  1530         <strong>
       
  1531          Yes
       
  1532         </strong>
       
  1533        </span>
       
  1534        we can do it, but
       
  1535        <span class="emphasis">
       
  1536         <em>
       
  1537          Tuples
       
  1538         </em>
       
  1539        </span>
       
  1540        are helpful at times, like we can return Tuples from
       
  1541 functions. They are also returned by some built-in functions and methods. And
   464 functions. They are also returned by some built-in functions and methods. And
  1542 also there are some use cases like co-ordinate among other things. So
   465 also there are some use cases like co-ordinate among other things. So <span class="emphasis"><em>Tuples</em></span>
  1543        <span class="emphasis">
   466 are helpful.</p>
  1544         <em>
   467 </div>
  1545          Tuples
   468 </div>
  1546         </em>
   469 <div class="section" title="3.Additional Syntax">
  1547        </span>
   470 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1548        are helpful.
   471 <a name="id2948295"></a>3.Additional Syntax</h2></div></div></div>
  1549       </p>
   472 <p id="ch3list_tuples_34">The following additional syntax are introduced to make it easier to operate on
  1550      </div>
   473 <span class="emphasis"><em>Lists</em></span>.</p>
  1551     </div>
   474 <div class="section" title="3.1.range()">
  1552     <div class="section" title="3.Additional Syntax">
   475 <div class="titlepage"><div><div><h3 class="title">
  1553      <div class="titlepage">
   476 <a name="id2948312"></a>3.1.range()</h3></div></div></div>
  1554       <div>
   477 <p id="ch3list_tuples_35">The <span class="emphasis"><em>range</em></span> function takes at least one argument and 2 additional optional
  1555        <div>
       
  1556         <h2 class="title" style="clear: both">
       
  1557          <a name="id3003727">
       
  1558          </a>
       
  1559          3.Additional Syntax
       
  1560         </h2>
       
  1561        </div>
       
  1562       </div>
       
  1563      </div>
       
  1564      <p id="ch3list_tuples_34">
       
  1565       The following additional syntax are introduced to make it easier to operate on
       
  1566       <span class="emphasis">
       
  1567        <em>
       
  1568         Lists
       
  1569        </em>
       
  1570       </span>
       
  1571       .
       
  1572      </p>
       
  1573      <div class="section" title="3.1.range()">
       
  1574       <div class="titlepage">
       
  1575        <div>
       
  1576         <div>
       
  1577          <h3 class="title">
       
  1578           <a name="id3003745">
       
  1579           </a>
       
  1580           3.1.range()
       
  1581          </h3>
       
  1582         </div>
       
  1583        </div>
       
  1584       </div>
       
  1585       <p id="ch3list_tuples_35">
       
  1586        The
       
  1587        <span class="emphasis">
       
  1588         <em>
       
  1589          range
       
  1590         </em>
       
  1591        </span>
       
  1592        function takes at least one argument and 2 additional optional
       
  1593 arguments. If two or more arguments are specified, the range function returns
   478 arguments. If two or more arguments are specified, the range function returns
  1594 a list of natural numbers starting from the first argument passed to it to the
   479 a list of natural numbers starting from the first argument passed to it to the
  1595 second argument. The third argument, if specified is used as a step. Suppose
   480 second argument. The third argument, if specified is used as a step. Suppose
  1596 only one argument is specified, then
   481 only one argument is specified, then <span class="emphasis"><em>range</em></span> function returns a list of natural
  1597        <span class="emphasis">
   482 numbers starting from 0 upto the argument specified:</p>
  1598         <em>
   483 <pre class="programlisting">
  1599          range
   484 &gt;&gt;&gt; range(5, 10, 2)
  1600         </em>
       
  1601        </span>
       
  1602        function returns a list of natural
       
  1603 numbers starting from 0 upto the argument specified:
       
  1604       </p>
       
  1605       <pre class="programlisting">
       
  1606        &gt;&gt;&gt; range(5, 10, 2)
       
  1607 [5, 7, 9]
   485 [5, 7, 9]
  1608 &gt;&gt;&gt; range(2, 15)
   486 &gt;&gt;&gt; range(2, 15)
  1609 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
   487 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
  1610 &gt;&gt;&gt; range(12)
   488 &gt;&gt;&gt; range(12)
  1611 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
   489 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]</pre>
  1612       </pre>
   490 </div>
  1613      </div>
   491 <div class="section" title="3.2.for">
  1614      <div class="section" title="3.2.for">
   492 <div class="titlepage"><div><div><h3 class="title">
  1615       <div class="titlepage">
   493 <a name="id2948344"></a>3.2.for</h3></div></div></div>
  1616        <div>
   494 <p id="ch3list_tuples_36">The <span class="strong"><strong>for</strong></span> keyword is used as a part of the looping construct. Unlike for loops
  1617         <div>
       
  1618          <h3 class="title">
       
  1619           <a name="id3003777">
       
  1620           </a>
       
  1621           3.2.for
       
  1622          </h3>
       
  1623         </div>
       
  1624        </div>
       
  1625       </div>
       
  1626       <p id="ch3list_tuples_36">
       
  1627        The
       
  1628        <span class="strong">
       
  1629         <strong>
       
  1630          for
       
  1631         </strong>
       
  1632        </span>
       
  1633        keyword is used as a part of the looping construct. Unlike for loops
       
  1634 in other languages, Python's for is used to iterate through the elements of
   495 in other languages, Python's for is used to iterate through the elements of
  1635 sequences like
   496 sequences like <span class="emphasis"><em>Lists</em></span>, <span class="emphasis"><em>Tuples</em></span>, <span class="emphasis"><em>Dictionaries</em></span>, etc. The syntax of the for loop
  1636        <span class="emphasis">
   497 consists of <span class="strong"><strong>for</strong></span>, followed by a variable to hold the individual or the current
  1637         <em>
   498 element of the list during iteration and <span class="strong"><strong>in</strong></span>, followed by the sequence and a
  1638          Lists
   499 semicolon(':') The next line which is part of the <span class="strong"><strong>for</strong></span> loop, i.e the statements
  1639         </em>
   500 that are part of the loop should start with a new intend:</p>
  1640        </span>
   501 <pre class="programlisting">
  1641        ,
   502 &gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
  1642        <span class="emphasis">
       
  1643         <em>
       
  1644          Tuples
       
  1645         </em>
       
  1646        </span>
       
  1647        ,
       
  1648        <span class="emphasis">
       
  1649         <em>
       
  1650          Dictionaries
       
  1651         </em>
       
  1652        </span>
       
  1653        , etc. The syntax of the for loop
       
  1654 consists of
       
  1655        <span class="strong">
       
  1656         <strong>
       
  1657          for
       
  1658         </strong>
       
  1659        </span>
       
  1660        , followed by a variable to hold the individual or the current
       
  1661 element of the list during iteration and
       
  1662        <span class="strong">
       
  1663         <strong>
       
  1664          in
       
  1665         </strong>
       
  1666        </span>
       
  1667        , followed by the sequence and a
       
  1668 semicolon(':') The next line which is part of the
       
  1669        <span class="strong">
       
  1670         <strong>
       
  1671          for
       
  1672         </strong>
       
  1673        </span>
       
  1674        loop, i.e the statements
       
  1675 that are part of the loop should start with a new intend:
       
  1676       </p>
       
  1677       <pre class="programlisting">
       
  1678        &gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
       
  1679 &gt;&gt;&gt; for name in names:
   503 &gt;&gt;&gt; for name in names:
  1680 ...   print "Name =", name
   504 ...   print "Name =", name
  1681 ...
   505 ...
  1682 Name = Guido
   506 Name = Guido
  1683 Name = Alex
   507 Name = Alex
  1684 Name = Tim
   508 Name = Tim</pre>
  1685       </pre>
   509 </div>
  1686      </div>
   510 </div>
  1687     </div>
   511 <div class="section" title="4.Conclusion">
  1688     <div class="section" title="4.Conclusion">
   512 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1689      <div class="titlepage">
   513 <a name="id2948403"></a>4.Conclusion</h2></div></div></div>
  1690       <div>
   514 <p id="ch3list_tuples_37">This section on <span class="emphasis"><em>Lists</em></span> and <span class="emphasis"><em>Tuples</em></span> introduces almost all the necessary
  1691        <div>
   515 machinary required to work on <span class="emphasis"><em>Lists</em></span> and <span class="emphasis"><em>Tuples</em></span>. Topics like how to
  1692         <h2 class="title" style="clear: both">
       
  1693          <a name="id3003836">
       
  1694          </a>
       
  1695          4.Conclusion
       
  1696         </h2>
       
  1697        </div>
       
  1698       </div>
       
  1699      </div>
       
  1700      <p id="ch3list_tuples_37">
       
  1701       This section on
       
  1702       <span class="emphasis">
       
  1703        <em>
       
  1704         Lists
       
  1705        </em>
       
  1706       </span>
       
  1707       and
       
  1708       <span class="emphasis">
       
  1709        <em>
       
  1710         Tuples
       
  1711        </em>
       
  1712       </span>
       
  1713       introduces almost all the necessary
       
  1714 machinary required to work on
       
  1715       <span class="emphasis">
       
  1716        <em>
       
  1717         Lists
       
  1718        </em>
       
  1719       </span>
       
  1720       and
       
  1721       <span class="emphasis">
       
  1722        <em>
       
  1723         Tuples
       
  1724        </em>
       
  1725       </span>
       
  1726       . Topics like how to
       
  1727 use these data structures in bigger more useful programs will be introduced
   516 use these data structures in bigger more useful programs will be introduced
  1728 in the subsequent chapters.
   517 in the subsequent chapters.</p>
  1729      </p>
   518 </div>
  1730     </div>
   519 </div>
  1731    </div>
   520 </div></body>
  1732   </div>
       
  1733  </body>
       
  1734 </html>
   521 </html>
       
   522