SEESenv/web/html/ch3list_tuples.html
changeset 27 cb14131583c6
child 28 514098969b11
equal deleted inserted replaced
26:1846ab4ebdda 27:cb14131583c6
       
     1 <html>
       
     2  <head>
       
     3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       
     4   <title>
       
     5    Chapter. list_tuples
       
     6   </title>
       
     7   <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
       
     8   <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" />
       
     9   <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" />
       
    10   <script type="text/javascript" src="/review/support/jquery-min.js">
       
    11   </script>
       
    12   <script type="text/javascript" src="/review/support/form.js">
       
    13   </script>
       
    14   <script type="text/javascript" src="/review/support/hsbook.js">
       
    15   </script>
       
    16  </head>
       
    17  <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
       
    18   <div class="chapter" id="ch3list_tuples">
       
    19    <div class="titlepage">
       
    20    </div>
       
    21    <div class="toc">
       
    22     <p>
       
    23      <b>
       
    24       Table of Contents
       
    25      </b>
       
    26     </p>
       
    27     <dl>
       
    28      <dt>
       
    29       <span class="article">
       
    30        <a href="#id2951820">
       
    31         Lists and Tuples
       
    32        </a>
       
    33       </span>
       
    34      </dt>
       
    35      <dd>
       
    36       <dl>
       
    37        <dt>
       
    38         <span class="section">
       
    39          <a href="#id2761866">
       
    40           1. Lists
       
    41          </a>
       
    42         </span>
       
    43        </dt>
       
    44        <dd>
       
    45         <dl>
       
    46          <dt>
       
    47           <span class="section">
       
    48            <a href="#id2951624">
       
    49             1.1. Common List Operations
       
    50            </a>
       
    51           </span>
       
    52          </dt>
       
    53          <dt>
       
    54           <span class="section">
       
    55            <a href="#id3003090">
       
    56             1.2. None, Empty Lists, and Initialization
       
    57            </a>
       
    58           </span>
       
    59          </dt>
       
    60          <dt>
       
    61           <span class="section">
       
    62            <a href="#id3003130">
       
    63             1.3. Nested Lists
       
    64            </a>
       
    65           </span>
       
    66          </dt>
       
    67          <dt>
       
    68           <span class="section">
       
    69            <a href="#id3003165">
       
    70             1.4. List Methods
       
    71            </a>
       
    72           </span>
       
    73          </dt>
       
    74         </dl>
       
    75        </dd>
       
    76        <dt>
       
    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'
       
   335 &gt;&gt;&gt; a[-1]
       
   336 3534534L
       
   337        </pre>
       
   338        <p id="ch3list_tuples_4">
       
   339         It is important to note here that the last element of the
       
   340         <span class="emphasis">
       
   341          <em>
       
   342           List
       
   343          </em>
       
   344         </span>
       
   345         has an index of
       
   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]
       
   373 &gt;&gt;&gt; [54, 75, 23] + ['write', 67, 'read']
       
   374 [54, 75, 23, 'write', 67, 'read']
       
   375        </pre>
       
   376       </div>
       
   377       <div class="section" title="1.1.3.Slicing">
       
   378        <div class="titlepage">
       
   379         <div>
       
   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
       
   404 inclusive and the second index is exclusive. The resulting slice is also a
       
   405         <span class="emphasis">
       
   406          <em>
       
   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]
       
   415 [4, 5, 6]
       
   416 &gt;&gt;&gt; num[0:1]
       
   417 [1]
       
   418 &gt;&gt;&gt; num[7:10]
       
   419 [7, 8, 9]
       
   420        </pre>
       
   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
       
   430         <span class="emphasis">
       
   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
       
   437 the
       
   438         <span class="emphasis">
       
   439          <em>
       
   440           List
       
   441          </em>
       
   442         </span>
       
   443         . But this can also be done in a much easier way using negative indices:
       
   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
       
   466 the second index:
       
   467        </p>
       
   468        <pre class="programlisting">
       
   469         &gt;&gt;&gt; num[:4]
       
   470 [1, 2, 3, 4]
       
   471 &gt;&gt;&gt; num[7:]
       
   472 [8, 9]
       
   473 &gt;&gt;&gt; num[-3:]
       
   474 [7, 8, 9]
       
   475 &gt;&gt;&gt; num[:]
       
   476 [1, 2, 3, 4, 5, 6, 7, 8, 9]
       
   477 &gt;&gt;&gt; num[4:9:3]
       
   478 [5, 8]
       
   479 &gt;&gt;&gt; num[3::2]
       
   480 [4, 6, 8]
       
   481 &gt;&gt;&gt; num[::4]
       
   482 [1, 5, 9]
       
   483        </pre>
       
   484       </div>
       
   485       <div class="section" title="1.1.4.Multiplication">
       
   486        <div class="titlepage">
       
   487         <div>
       
   488          <div>
       
   489           <h4 class="title">
       
   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]
       
   509 &gt;&gt;&gt; [42, 'Python', 54] * 3
       
   510 [42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54]
       
   511        </pre>
       
   512       </div>
       
   513       <div class="section" title="1.1.5.Membership">
       
   514        <div class="titlepage">
       
   515         <div>
       
   516          <div>
       
   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
       
   558 operator:
       
   559        </p>
       
   560        <pre class="programlisting">
       
   561         &gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
       
   562 &gt;&gt;&gt; 'Tim' in names
       
   563 True
       
   564 &gt;&gt;&gt; 'Adam' in names
       
   565 False
       
   566        </pre>
       
   567       </div>
       
   568       <div class="section" title="1.1.6.Length, Maximum and Minimum">
       
   569        <div class="titlepage">
       
   570         <div>
       
   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
       
   589 element with the smallest value:
       
   590        </p>
       
   591        <pre class="programlisting">
       
   592         &gt;&gt;&gt; num = [4, 1, 32, 12, 67, 34, 65]
       
   593 &gt;&gt;&gt; len(num)
       
   594 7
       
   595 &gt;&gt;&gt; max(num)
       
   596 67
       
   597 &gt;&gt;&gt; min(num)
       
   598 1
       
   599        </pre>
       
   600       </div>
       
   601       <div class="section" title="1.1.7.Changing Elements">
       
   602        <div class="titlepage">
       
   603         <div>
       
   604          <div>
       
   605           <h4 class="title">
       
   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
       
   631 &gt;&gt;&gt; a
       
   632 [1, 3, 9, 7]
       
   633        </pre>
       
   634       </div>
       
   635       <div class="section" title="1.1.8.Deleting Elements">
       
   636        <div class="titlepage">
       
   637         <div>
       
   638          <div>
       
   639           <h4 class="title">
       
   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:]
       
   665 &gt;&gt;&gt; a
       
   666 [1, 3, 5, 7]
       
   667 &gt;&gt;&gt; del a[1]
       
   668 &gt;&gt;&gt; a
       
   669 [1, 5, 7]
       
   670        </pre>
       
   671       </div>
       
   672       <div class="section" title="1.1.9.Assign to Slices">
       
   673        <div class="titlepage">
       
   674         <div>
       
   675          <div>
       
   676           <h4 class="title">
       
   677            <a name="id3003050">
       
   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]
       
   703 [0, 1, 4, 5]
       
   704 &gt;&gt;&gt; a[2:2] = [2, 3]
       
   705 &gt;&gt;&gt; a
       
   706 [0, 1, 2, 3, 4, 5]
       
   707 &gt;&gt;&gt; a[2:4] = []
       
   708 &gt;&gt;&gt; a
       
   709 [0, 1, 4, 5]
       
   710        </pre>
       
   711        <p id="ch3list_tuples_f">
       
   712         The last two examples should be particularly noted carefully. The last but one
       
   713 example insert elements or a list of elements into a
       
   714         <span class="emphasis">
       
   715          <em>
       
   716           List
       
   717          </em>
       
   718         </span>
       
   719         and the last example
       
   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
       
   770 no value:
       
   771       </p>
       
   772       <pre class="programlisting">
       
   773        &gt;&gt;&gt; a = []
       
   774 &gt;&gt;&gt; a
       
   775 []
       
   776 &gt;&gt;&gt; n = [None] * 10
       
   777 &gt;&gt;&gt; n
       
   778 [None, None, None, None, None, None, None, None, None, None]
       
   779       </pre>
       
   780      </div>
       
   781      <div class="section" title="1.3.Nested Lists">
       
   782       <div class="titlepage">
       
   783        <div>
       
   784         <div>
       
   785          <h3 class="title">
       
   786           <a name="id3003130">
       
   787           </a>
       
   788           1.3.Nested Lists
       
   789          </h3>
       
   790         </div>
       
   791        </div>
       
   792       </div>
       
   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
       
   841 method is called like:
       
   842       </p>
       
   843       <pre class="programlisting">
       
   844        object.method(arguments)
       
   845       </pre>
       
   846       <p id="ch3list_tuples_13">
       
   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
       
   863 the
       
   864        <span class="emphasis">
       
   865         <em>
       
   866          List
       
   867         </em>
       
   868        </span>
       
   869        methods.
       
   870       </p>
       
   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)
       
   904 &gt;&gt;&gt; prime
       
   905 [2, 3, 5, 7]
       
   906        </pre>
       
   907        <p id="ch3list_tuples_16">
       
   908         It is important to note that append changes the
       
   909         <span class="emphasis">
       
   910          <em>
       
   911           List
       
   912          </em>
       
   913         </span>
       
   914         in-place.
       
   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
       
   942 &gt;&gt;&gt; tlst = ['Python', 'is', 'a', 'beautiful', 'language']
       
   943 &gt;&gt;&gt; tlst.count('Python')
       
   944 1
       
   945        </pre>
       
   946       </div>
       
   947       <div class="section" title="1.4.3.extend">
       
   948        <div class="titlepage">
       
   949         <div>
       
   950          <div>
       
   951           <h4 class="title">
       
   952            <a name="id3003264">
       
   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]
       
   972 &gt;&gt;&gt; a.extend(b)
       
   973 [1, 2, 3, 4, 5, 6]
       
   974        </pre>
       
   975        <p id="ch3list_tuples_19">
       
   976         This is an in-place method. This method is equivalent to using the + operator, but
       
   977 using the + operator returns a new list.
       
   978        </p>
       
   979       </div>
       
   980       <div class="section" title="1.4.4.index">
       
   981        <div class="titlepage">
       
   982         <div>
       
   983          <div>
       
   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)
       
  1005 3
       
  1006        </pre>
       
  1007       </div>
       
  1008       <div class="section" title="1.4.5.insert">
       
  1009        <div class="titlepage">
       
  1010         <div>
       
  1011          <div>
       
  1012           <h4 class="title">
       
  1013            <a name="id3003311">
       
  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')
       
  1033 &gt;&gt;&gt; a
       
  1034 ['Python', 'is', 'so', 'cool']
       
  1035        </pre>
       
  1036        <p id="ch3list_tuples_1c">
       
  1037         The
       
  1038         <span class="emphasis">
       
  1039          <em>
       
  1040           insert
       
  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
       
  1073         <span class="emphasis">
       
  1074          <em>
       
  1075           pop
       
  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()
       
  1083 &gt;&gt;&gt; a
       
  1084 5
       
  1085 &gt;&gt;&gt; a.pop(2)
       
  1086 &gt;&gt;&gt; a
       
  1087 3
       
  1088        </pre>
       
  1089        <p id="ch3list_tuples_1e">
       
  1090         The
       
  1091         <span class="emphasis">
       
  1092          <em>
       
  1093           pop
       
  1094          </em>
       
  1095         </span>
       
  1096         method changes the
       
  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)
       
  1130 &gt;&gt;&gt; a
       
  1131 [1, 3, 4, 2, 5, 2]
       
  1132        </pre>
       
  1133       </div>
       
  1134       <div class="section" title="1.4.8.reverse">
       
  1135        <div class="titlepage">
       
  1136         <div>
       
  1137          <div>
       
  1138           <h4 class="title">
       
  1139            <a name="id3003405">
       
  1140            </a>
       
  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()
       
  1166 &gt;&gt;&gt; a
       
  1167 ['tim', 'alex', 'guido']
       
  1168        </pre>
       
  1169       </div>
       
  1170       <div class="section" title="1.4.9.sort">
       
  1171        <div class="titlepage">
       
  1172         <div>
       
  1173          <div>
       
  1174           <h4 class="title">
       
  1175            <a name="id3003431">
       
  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()
       
  1201 &gt;&gt;&gt; a
       
  1202 [1, 3, 4, 5, 7]
       
  1203        </pre>
       
  1204        <p id="ch3list_tuples_22">
       
  1205         In addition to the sort method on a
       
  1206         <span class="emphasis">
       
  1207          <em>
       
  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)
       
  1229 &gt;&gt;&gt; b
       
  1230 [1, 3, 4, 5, 7]
       
  1231 &gt;&gt;&gt; a
       
  1232 [5, 1, 3, 7, 4]
       
  1233        </pre>
       
  1234       </div>
       
  1235      </div>
       
  1236     </div>
       
  1237     <div class="section" title="2.Tuples">
       
  1238      <div class="titlepage">
       
  1239       <div>
       
  1240        <div>
       
  1241         <h2 class="title" style="clear: both">
       
  1242          <a name="id3003481">
       
  1243          </a>
       
  1244          2.Tuples
       
  1245         </h2>
       
  1246        </div>
       
  1247       </div>
       
  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
       
  1308 (1, 2, 3)
       
  1309 &gt;&gt;&gt; b = 1,
       
  1310 &gt;&gt;&gt; b
       
  1311 (1,)
       
  1312      </pre>
       
  1313      <p id="ch3list_tuples_24">
       
  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
       
  1323 output always has a parenthesis.
       
  1324      </p>
       
  1325      <p id="ch3list_tuples_25">
       
  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
       
  1341 co-ordinate pair from which we need to separate x and y co-ordinates:
       
  1342      </p>
       
  1343      <pre class="programlisting">
       
  1344       &gt;&gt;&gt; a = (1, 2)
       
  1345 &gt;&gt;&gt; x, y = a
       
  1346 &gt;&gt;&gt; x
       
  1347 1
       
  1348 &gt;&gt;&gt; y
       
  1349 2
       
  1350      </pre>
       
  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
       
  1359 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
       
  1361 intuitive way in just one line. Say we want to swap the co-ordinates in the
       
  1362 above example:
       
  1363      </p>
       
  1364      <pre class="programlisting">
       
  1365       &gt;&gt;&gt; x, y = y, x
       
  1366 &gt;&gt;&gt; x
       
  1367 2
       
  1368 &gt;&gt;&gt; y
       
  1369 1
       
  1370      </pre>
       
  1371      <div class="section" title="2.1.Common Tuple Operations">
       
  1372       <div class="titlepage">
       
  1373        <div>
       
  1374         <div>
       
  1375          <h3 class="title">
       
  1376           <a name="id3003578">
       
  1377           </a>
       
  1378           2.1.Common Tuple Operations
       
  1379          </h3>
       
  1380         </div>
       
  1381        </div>
       
  1382       </div>
       
  1383       <p id="ch3list_tuples_27">
       
  1384        There is no need to introduce all the
       
  1385        <span class="emphasis">
       
  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]
       
  1444 6
       
  1445 &gt;&gt;&gt; b = (7, 8, 9)
       
  1446 &gt;&gt;&gt; a + b
       
  1447 (1, 2, 3, 4, 5, 6, 7, 8, 9)
       
  1448 &gt;&gt;&gt; a[3:5]
       
  1449 (4, 5)
       
  1450 &gt;&gt;&gt; 5 in a
       
  1451 True
       
  1452 &gt;&gt;&gt; c = (1,)
       
  1453 &gt;&gt;&gt; c * 5
       
  1454 (1, 1, 1, 1, 1)
       
  1455 &gt;&gt;&gt; len(a)
       
  1456 6
       
  1457 &gt;&gt;&gt; max(a)
       
  1458 6
       
  1459 &gt;&gt;&gt; min(a)
       
  1460 1
       
  1461       </pre>
       
  1462       <p id="ch3list_tuples_2f">
       
  1463        However the following
       
  1464        <span class="emphasis">
       
  1465         <em>
       
  1466          List
       
  1467         </em>
       
  1468        </span>
       
  1469        operations are not supported by
       
  1470        <span class="emphasis">
       
  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
       
  1542 also there are some use cases like co-ordinate among other things. So
       
  1543        <span class="emphasis">
       
  1544         <em>
       
  1545          Tuples
       
  1546         </em>
       
  1547        </span>
       
  1548        are helpful.
       
  1549       </p>
       
  1550      </div>
       
  1551     </div>
       
  1552     <div class="section" title="3.Additional Syntax">
       
  1553      <div class="titlepage">
       
  1554       <div>
       
  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
       
  1594 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
       
  1596 only one argument is specified, then
       
  1597        <span class="emphasis">
       
  1598         <em>
       
  1599          range
       
  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]
       
  1608 &gt;&gt;&gt; range(2, 15)
       
  1609 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
       
  1610 &gt;&gt;&gt; range(12)
       
  1611 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
       
  1612       </pre>
       
  1613      </div>
       
  1614      <div class="section" title="3.2.for">
       
  1615       <div class="titlepage">
       
  1616        <div>
       
  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
       
  1635 sequences like
       
  1636        <span class="emphasis">
       
  1637         <em>
       
  1638          Lists
       
  1639         </em>
       
  1640        </span>
       
  1641        ,
       
  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:
       
  1680 ...   print "Name =", name
       
  1681 ...
       
  1682 Name = Guido
       
  1683 Name = Alex
       
  1684 Name = Tim
       
  1685       </pre>
       
  1686      </div>
       
  1687     </div>
       
  1688     <div class="section" title="4.Conclusion">
       
  1689      <div class="titlepage">
       
  1690       <div>
       
  1691        <div>
       
  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
       
  1728 in the subsequent chapters.
       
  1729      </p>
       
  1730     </div>
       
  1731    </div>
       
  1732   </div>
       
  1733  </body>
       
  1734 </html>