SEESenv/web/html/ch2intro.html
changeset 25 ed38dd9bdb50
parent 24 10074d1357ff
child 27 cb14131583c6
equal deleted inserted replaced
24:10074d1357ff 25:ed38dd9bdb50
     1 <html>
     1 <html>
     2 <head>
     2  <head>
     3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     4   <title>
     4   <title>
     5    Chapter. intro
     5    Chapter. intro
     6   </title>
     6   </title>
     7   <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
     7   <link rel="stylesheet" href="/review/support/styles.css" type="text/css" />
    12   <script type="text/javascript" src="/review/support/form.js">
    12   <script type="text/javascript" src="/review/support/form.js">
    13   </script>
    13   </script>
    14   <script type="text/javascript" src="/review/support/hsbook.js">
    14   <script type="text/javascript" src="/review/support/hsbook.js">
    15   </script>
    15   </script>
    16  </head>
    16  </head>
    17 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" title="Chapter 1. ">
    17  <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
    18 <div class="titlepage"></div>
    18   <div class="chapter" id="ch2intro">
    19 <div class="toc">
    19    <div class="titlepage">
    20 <p><b>Table of Contents</b></p>
    20    </div>
    21 <dl>
    21    <div class="toc">
    22 <dt><span class="article"><a href="#id2580245">Basic Python</a></span></dt>
    22     <p>
    23 <dd><dl>
    23      <b>
    24 <dt><span class="section"><a href="#id2388008">1. Introduction</a></span></dt>
    24       Table of Contents
    25 <dt><span class="section"><a href="#id2631374">2. The Python Interpreter</a></span></dt>
    25      </b>
    26 <dd><dl>
    26     </p>
    27 <dt><span class="section"><a href="#id2631383">2.1. The Interactive Interpreter</a></span></dt>
    27     <dl>
    28 <dt><span class="section"><a href="#id2631556">2.2. <span class="emphasis"><em>ipython</em></span> - An enhanced interactive Python interpreter</a></span></dt>
    28      <dt>
    29 </dl></dd>
    29       <span class="article">
    30 <dt><span class="section"><a href="#id2631681">3. Editing and running a python file</a></span></dt>
    30        <a href="#id2809348">
    31 <dt><span class="section"><a href="#id2631788">4. Basic Datatypes and operators in Python</a></span></dt>
    31         Basic Python
    32 <dd><dl>
    32        </a>
    33 <dt><span class="section"><a href="#id2631822">4.1. Numbers</a></span></dt>
    33       </span>
    34 <dt><span class="section"><a href="#id2631952">4.2. Variables</a></span></dt>
    34      </dt>
    35 <dt><span class="section"><a href="#id2632000">4.3. Strings</a></span></dt>
    35      <dd>
    36 <dt><span class="section"><a href="#id2632194">4.4. Boolean</a></span></dt>
    36       <dl>
    37 </dl></dd>
    37        <dt>
    38 <dt><span class="section"><a href="#id2632223">5. The <span class="strong"><strong>while</strong></span> loop</a></span></dt>
    38         <span class="section">
    39 <dt><span class="section"><a href="#id2632260">6. The <span class="strong"><strong>if</strong></span> conditional</a></span></dt>
    39          <a href="#id2617111">
    40 <dt><span class="section"><a href="#id2632330">7. <span class="strong"><strong>raw_input()</strong></span></a></span></dt>
    40           1. Introduction
    41 <dt><span class="section"><a href="#id2632484">8. <span class="strong"><strong>int()</strong></span> method</a></span></dt>
    41          </a>
    42 </dl></dd>
    42         </span>
    43 </dl>
    43        </dt>
    44 </div>
    44        <dt>
    45 <div class="article" title="Basic Python">
    45         <span class="section">
    46 <div class="titlepage">
    46          <a href="#id2860477">
    47 <div><div><h2 class="title">
    47           2. The Python Interpreter
    48 <a name="id2580245"></a>Basic Python</h2></div></div>
    48          </a>
    49 <hr>
    49         </span>
    50 </div>
    50        </dt>
    51 <div class="toc">
    51        <dd>
    52 <p><b>Table of Contents</b></p>
    52         <dl>
    53 <dl>
    53          <dt>
    54 <dt><span class="section"><a href="#id2388008">1. Introduction</a></span></dt>
    54           <span class="section">
    55 <dt><span class="section"><a href="#id2631374">2. The Python Interpreter</a></span></dt>
    55            <a href="#id2860486">
    56 <dd><dl>
    56             2.1. The Interactive Interpreter
    57 <dt><span class="section"><a href="#id2631383">2.1. The Interactive Interpreter</a></span></dt>
    57            </a>
    58 <dt><span class="section"><a href="#id2631556">2.2. <span class="emphasis"><em>ipython</em></span> - An enhanced interactive Python interpreter</a></span></dt>
    58           </span>
    59 </dl></dd>
    59          </dt>
    60 <dt><span class="section"><a href="#id2631681">3. Editing and running a python file</a></span></dt>
    60          <dt>
    61 <dt><span class="section"><a href="#id2631788">4. Basic Datatypes and operators in Python</a></span></dt>
    61           <span class="section">
    62 <dd><dl>
    62            <a href="#id2860659">
    63 <dt><span class="section"><a href="#id2631822">4.1. Numbers</a></span></dt>
    63             2.2.
    64 <dt><span class="section"><a href="#id2631952">4.2. Variables</a></span></dt>
    64             <span class="emphasis">
    65 <dt><span class="section"><a href="#id2632000">4.3. Strings</a></span></dt>
    65              <em>
    66 <dt><span class="section"><a href="#id2632194">4.4. Boolean</a></span></dt>
    66               ipython
    67 </dl></dd>
    67              </em>
    68 <dt><span class="section"><a href="#id2632223">5. The <span class="strong"><strong>while</strong></span> loop</a></span></dt>
    68             </span>
    69 <dt><span class="section"><a href="#id2632260">6. The <span class="strong"><strong>if</strong></span> conditional</a></span></dt>
    69             - An enhanced interactive Python interpreter
    70 <dt><span class="section"><a href="#id2632330">7. <span class="strong"><strong>raw_input()</strong></span></a></span></dt>
    70            </a>
    71 <dt><span class="section"><a href="#id2632484">8. <span class="strong"><strong>int()</strong></span> method</a></span></dt>
    71           </span>
    72 </dl>
    72          </dt>
    73 </div>
    73         </dl>
    74 <p><a name="ch2intro_1"></a>This document is intended to be handed out at the end of the workshop. It has
    74        </dd>
       
    75        <dt>
       
    76         <span class="section">
       
    77          <a href="#id2860784">
       
    78           3. Editing and running a python file
       
    79          </a>
       
    80         </span>
       
    81        </dt>
       
    82        <dt>
       
    83         <span class="section">
       
    84          <a href="#id2860890">
       
    85           4. Basic Datatypes and operators in Python
       
    86          </a>
       
    87         </span>
       
    88        </dt>
       
    89        <dd>
       
    90         <dl>
       
    91          <dt>
       
    92           <span class="section">
       
    93            <a href="#id2860925">
       
    94             4.1. Numbers
       
    95            </a>
       
    96           </span>
       
    97          </dt>
       
    98          <dt>
       
    99           <span class="section">
       
   100            <a href="#id2861055">
       
   101             4.2. Variables
       
   102            </a>
       
   103           </span>
       
   104          </dt>
       
   105          <dt>
       
   106           <span class="section">
       
   107            <a href="#id2861102">
       
   108             4.3. Strings
       
   109            </a>
       
   110           </span>
       
   111          </dt>
       
   112          <dt>
       
   113           <span class="section">
       
   114            <a href="#id2861297">
       
   115             4.4. Boolean
       
   116            </a>
       
   117           </span>
       
   118          </dt>
       
   119         </dl>
       
   120        </dd>
       
   121        <dt>
       
   122         <span class="section">
       
   123          <a href="#id2861325">
       
   124           5. The
       
   125           <span class="strong">
       
   126            <strong>
       
   127             while
       
   128            </strong>
       
   129           </span>
       
   130           loop
       
   131          </a>
       
   132         </span>
       
   133        </dt>
       
   134        <dt>
       
   135         <span class="section">
       
   136          <a href="#id2861363">
       
   137           6. The
       
   138           <span class="strong">
       
   139            <strong>
       
   140             if
       
   141            </strong>
       
   142           </span>
       
   143           conditional
       
   144          </a>
       
   145         </span>
       
   146        </dt>
       
   147        <dt>
       
   148         <span class="section">
       
   149          <a href="#id2861432">
       
   150           7.
       
   151           <span class="strong">
       
   152            <strong>
       
   153             raw_input()
       
   154            </strong>
       
   155           </span>
       
   156          </a>
       
   157         </span>
       
   158        </dt>
       
   159        <dt>
       
   160         <span class="section">
       
   161          <a href="#id2861586">
       
   162           8.
       
   163           <span class="strong">
       
   164            <strong>
       
   165             int()
       
   166            </strong>
       
   167           </span>
       
   168           method
       
   169          </a>
       
   170         </span>
       
   171        </dt>
       
   172       </dl>
       
   173      </dd>
       
   174     </dl>
       
   175    </div>
       
   176    <div class="article" title="Basic Python">
       
   177     <div class="titlepage">
       
   178      <div>
       
   179       <div>
       
   180        <h2 class="title">
       
   181         <a name="id2809348">
       
   182         </a>
       
   183         Basic Python
       
   184        </h2>
       
   185       </div>
       
   186      </div>
       
   187      <hr />
       
   188     </div>
       
   189     <div class="toc">
       
   190      <p>
       
   191       <b>
       
   192        Table of Contents
       
   193       </b>
       
   194      </p>
       
   195      <dl>
       
   196       <dt>
       
   197        <span class="section">
       
   198         <a href="#id2617111">
       
   199          1. Introduction
       
   200         </a>
       
   201        </span>
       
   202       </dt>
       
   203       <dt>
       
   204        <span class="section">
       
   205         <a href="#id2860477">
       
   206          2. The Python Interpreter
       
   207         </a>
       
   208        </span>
       
   209       </dt>
       
   210       <dd>
       
   211        <dl>
       
   212         <dt>
       
   213          <span class="section">
       
   214           <a href="#id2860486">
       
   215            2.1. The Interactive Interpreter
       
   216           </a>
       
   217          </span>
       
   218         </dt>
       
   219         <dt>
       
   220          <span class="section">
       
   221           <a href="#id2860659">
       
   222            2.2.
       
   223            <span class="emphasis">
       
   224             <em>
       
   225              ipython
       
   226             </em>
       
   227            </span>
       
   228            - An enhanced interactive Python interpreter
       
   229           </a>
       
   230          </span>
       
   231         </dt>
       
   232        </dl>
       
   233       </dd>
       
   234       <dt>
       
   235        <span class="section">
       
   236         <a href="#id2860784">
       
   237          3. Editing and running a python file
       
   238         </a>
       
   239        </span>
       
   240       </dt>
       
   241       <dt>
       
   242        <span class="section">
       
   243         <a href="#id2860890">
       
   244          4. Basic Datatypes and operators in Python
       
   245         </a>
       
   246        </span>
       
   247       </dt>
       
   248       <dd>
       
   249        <dl>
       
   250         <dt>
       
   251          <span class="section">
       
   252           <a href="#id2860925">
       
   253            4.1. Numbers
       
   254           </a>
       
   255          </span>
       
   256         </dt>
       
   257         <dt>
       
   258          <span class="section">
       
   259           <a href="#id2861055">
       
   260            4.2. Variables
       
   261           </a>
       
   262          </span>
       
   263         </dt>
       
   264         <dt>
       
   265          <span class="section">
       
   266           <a href="#id2861102">
       
   267            4.3. Strings
       
   268           </a>
       
   269          </span>
       
   270         </dt>
       
   271         <dt>
       
   272          <span class="section">
       
   273           <a href="#id2861297">
       
   274            4.4. Boolean
       
   275           </a>
       
   276          </span>
       
   277         </dt>
       
   278        </dl>
       
   279       </dd>
       
   280       <dt>
       
   281        <span class="section">
       
   282         <a href="#id2861325">
       
   283          5. The
       
   284          <span class="strong">
       
   285           <strong>
       
   286            while
       
   287           </strong>
       
   288          </span>
       
   289          loop
       
   290         </a>
       
   291        </span>
       
   292       </dt>
       
   293       <dt>
       
   294        <span class="section">
       
   295         <a href="#id2861363">
       
   296          6. The
       
   297          <span class="strong">
       
   298           <strong>
       
   299            if
       
   300           </strong>
       
   301          </span>
       
   302          conditional
       
   303         </a>
       
   304        </span>
       
   305       </dt>
       
   306       <dt>
       
   307        <span class="section">
       
   308         <a href="#id2861432">
       
   309          7.
       
   310          <span class="strong">
       
   311           <strong>
       
   312            raw_input()
       
   313           </strong>
       
   314          </span>
       
   315         </a>
       
   316        </span>
       
   317       </dt>
       
   318       <dt>
       
   319        <span class="section">
       
   320         <a href="#id2861586">
       
   321          8.
       
   322          <span class="strong">
       
   323           <strong>
       
   324            int()
       
   325           </strong>
       
   326          </span>
       
   327          method
       
   328         </a>
       
   329        </span>
       
   330       </dt>
       
   331      </dl>
       
   332     </div>
       
   333     <p id="ch2intro_1">
       
   334      This document is intended to be handed out at the end of the workshop. It has
    75 been designed for Engineering students who are Python beginners and have basic
   335 been designed for Engineering students who are Python beginners and have basic
    76 programming skills. The focus is on basic numerics and plotting using Python.</p>The system requirements:<div class="itemizedlist"><ul class="itemizedlist" type="*">
   336 programming skills. The focus is on basic numerics and plotting using Python.
    77 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_2"></a>Python - version 2.5.x or newer.</p></li>
   337     </p>
    78 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_3"></a>IPython</p></li>
   338     The system requirements:
    79 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_4"></a>Text editor - scite, vim, emacs or whatever you are comfortable with.</p></li>
   339     <div class="itemizedlist">
    80 </ul></div>
   340      <ul class="itemizedlist" type="*">
    81 <div class="section" title="1. Introduction">
   341       <li class="listitem" style="list-style-type: *">
    82 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   342        <p id="ch2intro_2">
    83 <a name="id2388008"></a>1. Introduction</h2></div></div></div>
   343         Python - version 2.5.x or newer.
    84 <p><a name="ch2intro_5"></a>The Python programming language was created by a dutch named Guido van Rossum.
   344        </p>
       
   345       </li>
       
   346       <li class="listitem" style="list-style-type: *">
       
   347        <p id="ch2intro_3">
       
   348         IPython
       
   349        </p>
       
   350       </li>
       
   351       <li class="listitem" style="list-style-type: *">
       
   352        <p id="ch2intro_4">
       
   353         Text editor - scite, vim, emacs or whatever you are comfortable with.
       
   354        </p>
       
   355       </li>
       
   356      </ul>
       
   357     </div>
       
   358     <div class="section" title="1.Introduction">
       
   359      <div class="titlepage">
       
   360       <div>
       
   361        <div>
       
   362         <h2 class="title" style="clear: both">
       
   363          <a name="id2617111">
       
   364          </a>
       
   365          1.Introduction
       
   366         </h2>
       
   367        </div>
       
   368       </div>
       
   369      </div>
       
   370      <p id="ch2intro_5">
       
   371       The Python programming language was created by a dutch named Guido van Rossum.
    85 The idea of Python was conceived in December 1989. The name Python has nothing
   372 The idea of Python was conceived in December 1989. The name Python has nothing
    86 to do with the reptilian, but its been named after the 70s comedy series
   373 to do with the reptilian, but its been named after the 70s comedy series
    87 "Monty Python's Flying Circus", since it happens to be Guido's favourite
   374 "Monty Python's Flying Circus", since it happens to be Guido's favourite
    88 TV series.</p>
   375 TV series.
    89 <p><a name="ch2intro_6"></a>Current stable version of Python is 2.6.x, although Python 3.0 is also the stable
   376      </p>
       
   377      <p id="ch2intro_6">
       
   378       Current stable version of Python is 2.6.x, although Python 3.0 is also the stable
    90 version, it is not backwards compatible with the previous versions and is hence
   379 version, it is not backwards compatible with the previous versions and is hence
    91 not entirely popular at the moment. This material will focus on the 2.6.x series.</p>
   380 not entirely popular at the moment. This material will focus on the 2.6.x series.
    92 <p><a name="ch2intro_7"></a>Python is licensed under the Python Software Foundation License (PSF License)
   381      </p>
       
   382      <p id="ch2intro_7">
       
   383       Python is licensed under the Python Software Foundation License (PSF License)
    93 which is GPL compatible Free Software license (excepting license version 1.6 and 2.0)
   384 which is GPL compatible Free Software license (excepting license version 1.6 and 2.0)
    94 It is a no strings attached license, which means the source code is free to modify
   385 It is a no strings attached license, which means the source code is free to modify
    95 and redistribute.</p>
   386 and redistribute.
    96 <p><a name="ch2intro_8"></a>The Python docs define Python as "Python is an interpreted, object-oriented,
   387      </p>
       
   388      <p id="ch2intro_8">
       
   389       The Python docs define Python as "Python is an interpreted, object-oriented,
    97 high-level programming language with dynamic semantics." A more detailed summary
   390 high-level programming language with dynamic semantics." A more detailed summary
    98 can be found at </p>
   391 can be found at
    99 <div class="reference">
   392      </p>
   100 <div class="titlepage"><hr></div>http://www.python.org/doc/essays/blurb.html</div>
   393      <div class="reference">
   101 <p>. Python is a language that
   394       <div class="titlepage">
       
   395        <hr />
       
   396       </div>
       
   397       http://www.python.org/doc/essays/blurb.html
       
   398      </div>
       
   399      <p>
       
   400       . Python is a language that
   102 has been designed to help the programmer concentrate on solving the problem at hand
   401 has been designed to help the programmer concentrate on solving the problem at hand
   103 and not worry about the programming language idiosyncrasies.</p>
   402 and not worry about the programming language idiosyncrasies.
   104 <p><a name="ch2intro_9"></a>Python is a highly cross platform compatible language on account of it being an
   403      </p>
       
   404      <p id="ch2intro_9">
       
   405       Python is a highly cross platform compatible language on account of it being an
   105 interpreted language. It is highly scalable and hence has been adapted to run on
   406 interpreted language. It is highly scalable and hence has been adapted to run on
   106 the Nokia 60 series phones. Python has been designed to be readable and easy to use</p>
   407 the Nokia 60 series phones. Python has been designed to be readable and easy to use
   107 <p><a name="ch2intro_a"></a><span class="strong"><strong>Resources available for reference</strong></span></p>
   408      </p>
   108 <div class="itemizedlist"><ul class="itemizedlist" type="*">
   409      <p id="ch2intro_a">
   109 <li class="listitem" style="list-style-type: *">
   410       <span class="strong">
   110 <p><a name="ch2intro_b"></a>Web: </p>
   411        <strong>
   111 <div class="reference">
   412         Resources available for reference
   112 <div class="titlepage"><hr></div>http://www.python.org</div>
   413        </strong>
   113 </li>
   414       </span>
   114 <li class="listitem" style="list-style-type: *">
   415      </p>
   115 <p><a name="ch2intro_c"></a>Doc: </p>
   416      <div class="itemizedlist">
   116 <div class="reference">
   417       <ul class="itemizedlist" type="*">
   117 <div class="titlepage"><hr></div>http://www.python.org/doc</div>
   418        <li class="listitem" style="list-style-type: *">
   118 </li>
   419         <p id="ch2intro_b">
   119 <li class="listitem" style="list-style-type: *">Free Tutorials:<div class="itemizedlist"><ul class="itemizedlist" type="*">
   420          Web:
   120 <li class="listitem" style="list-style-type: *">
   421         </p>
   121 <p><a name="ch2intro_d"></a>Official Python Tutorial: </p>
   422         <div class="reference">
   122 <div class="reference">
   423          <div class="titlepage">
   123 <div class="titlepage"><hr></div>http://docs.python.org/tut/tut.html</div>
   424           <hr />
   124 </li>
   425          </div>
   125 <li class="listitem" style="list-style-type: *">
   426          http://www.python.org
   126 <p><a name="ch2intro_e"></a>Byte of Python: </p>
   427         </div>
   127 <div class="reference">
   428        </li>
   128 <div class="titlepage"><hr></div>http://www.byteofpython.info/</div>
   429        <li class="listitem" style="list-style-type: *">
   129 </li>
   430         <p id="ch2intro_c">
   130 <li class="listitem" style="list-style-type: *">
   431          Doc:
   131 <p><a name="ch2intro_f"></a>Dive into Python: </p>
   432         </p>
   132 <div class="reference">
   433         <div class="reference">
   133 <div class="titlepage"><hr></div>http://diveintopython.org/</div>
   434          <div class="titlepage">
   134 </li>
   435           <hr />
   135 </ul></div>
   436          </div>
   136 </li>
   437          http://www.python.org/doc
   137 </ul></div>
   438         </div>
   138 <p><a name="ch2intro_10"></a><span class="strong"><strong>Advantages of Python - Why Python??</strong></span></p>
   439        </li>
   139 <div class="itemizedlist"><ul class="itemizedlist" type="*">
   440        <li class="listitem" style="list-style-type: *">
   140 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_11"></a>Python has been designed for readability and ease of use. Its been designed in
   441         Free Tutorials:
       
   442         <div class="itemizedlist">
       
   443          <ul class="itemizedlist" type="*">
       
   444           <li class="listitem" style="list-style-type: *">
       
   445            <p id="ch2intro_d">
       
   446             Official Python Tutorial:
       
   447            </p>
       
   448            <div class="reference">
       
   449             <div class="titlepage">
       
   450              <hr />
       
   451             </div>
       
   452             http://docs.python.org/tut/tut.html
       
   453            </div>
       
   454           </li>
       
   455           <li class="listitem" style="list-style-type: *">
       
   456            <p id="ch2intro_e">
       
   457             Byte of Python:
       
   458            </p>
       
   459            <div class="reference">
       
   460             <div class="titlepage">
       
   461              <hr />
       
   462             </div>
       
   463             http://www.byteofpython.info/
       
   464            </div>
       
   465           </li>
       
   466           <li class="listitem" style="list-style-type: *">
       
   467            <p id="ch2intro_f">
       
   468             Dive into Python:
       
   469            </p>
       
   470            <div class="reference">
       
   471             <div class="titlepage">
       
   472              <hr />
       
   473             </div>
       
   474             http://diveintopython.org/
       
   475            </div>
       
   476           </li>
       
   477          </ul>
       
   478         </div>
       
   479        </li>
       
   480       </ul>
       
   481      </div>
       
   482      <p id="ch2intro_10">
       
   483       <span class="strong">
       
   484        <strong>
       
   485         Advantages of Python - Why Python??
       
   486        </strong>
       
   487       </span>
       
   488      </p>
       
   489      <div class="itemizedlist">
       
   490       <ul class="itemizedlist" type="*">
       
   491        <li class="listitem" style="list-style-type: *">
       
   492         <p id="ch2intro_11">
       
   493          Python has been designed for readability and ease of use. Its been designed in
   141 such a fashion that it imposes readability on the programmer. Python does away
   494 such a fashion that it imposes readability on the programmer. Python does away
   142 with the braces and the semicolons and instead implements code blocks based on
   495 with the braces and the semicolons and instead implements code blocks based on
   143 indentation, thus enhancing readability.</p></li>
   496 indentation, thus enhancing readability.
   144 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_12"></a>Python is a high level, interpreted, modular and object oriented language.
   497         </p>
       
   498        </li>
       
   499        <li class="listitem" style="list-style-type: *">
       
   500         <p id="ch2intro_12">
       
   501          Python is a high level, interpreted, modular and object oriented language.
   145 Python performs memory management on its own, thus the programmer need not bother
   502 Python performs memory management on its own, thus the programmer need not bother
   146 about allocating and deallocating memory to variables. Python provides extensibility
   503 about allocating and deallocating memory to variables. Python provides extensibility
   147 by providing modules which can be easily imported similar to headers in C and
   504 by providing modules which can be easily imported similar to headers in C and
   148 packages in Java. Python is object oriented and hence provides all the object oriented
   505 packages in Java. Python is object oriented and hence provides all the object oriented
   149 characteristics such as inheritance, encapsulation and polymorphism.</p></li>
   506 characteristics such as inheritance, encapsulation and polymorphism.
   150 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_13"></a>Python offers a highly powerful interactive programming interface in the form
   507         </p>
       
   508        </li>
       
   509        <li class="listitem" style="list-style-type: *">
       
   510         <p id="ch2intro_13">
       
   511          Python offers a highly powerful interactive programming interface in the form
   151 of the 'Interactive Interpreter' which will be discussed in more detail in the
   512 of the 'Interactive Interpreter' which will be discussed in more detail in the
   152 following sections.</p></li>
   513 following sections.
   153 <li class="listitem" style="list-style-type: *">
   514         </p>
   154 <p><a name="ch2intro_14"></a>Python provides a rich standard library and an extensive set of modules. The
   515        </li>
       
   516        <li class="listitem" style="list-style-type: *">
       
   517         <p id="ch2intro_14">
       
   518          Python provides a rich standard library and an extensive set of modules. The
   155 power of Python modules can be seen in this slightly exaggerated cartoon
   519 power of Python modules can be seen in this slightly exaggerated cartoon
   156 </p>
   520         </p>
   157 <div class="reference">
   521         <div class="reference">
   158 <div class="titlepage"><hr></div>http://xkcd.com/353/</div>
   522          <div class="titlepage">
   159 </li>
   523           <hr />
   160 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_15"></a>Python interfaces well with most other programming languages such as C, C++
   524          </div>
   161 and FORTRAN.</p></li>
   525          http://xkcd.com/353/
   162 </ul></div>
   526         </div>
   163 <p><a name="ch2intro_16"></a>Although, Python has one setback. Python is not fast as some of the compiled
   527        </li>
       
   528        <li class="listitem" style="list-style-type: *">
       
   529         <p id="ch2intro_15">
       
   530          Python interfaces well with most other programming languages such as C, C++
       
   531 and FORTRAN.
       
   532         </p>
       
   533        </li>
       
   534       </ul>
       
   535      </div>
       
   536      <p id="ch2intro_16">
       
   537       Although, Python has one setback. Python is not fast as some of the compiled
   164 languages like C or C++. Yet, the amount of flexibility and power more than make
   538 languages like C or C++. Yet, the amount of flexibility and power more than make
   165 up for this setback.</p>
   539 up for this setback.
   166 </div>
   540      </p>
   167 <div class="section" title="2. The Python Interpreter">
   541     </div>
   168 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   542     <div class="section" title="2.The Python Interpreter">
   169 <a name="id2631374"></a>2. The Python Interpreter</h2></div></div></div>
   543      <div class="titlepage">
   170 <div class="section" title="2.1. The Interactive Interpreter">
   544       <div>
   171 <div class="titlepage"><div><div><h3 class="title">
   545        <div>
   172 <a name="id2631383"></a>2.1. The Interactive Interpreter</h3></div></div></div>
   546         <h2 class="title" style="clear: both">
   173 <p><a name="ch2intro_17"></a>Typing <span class="emphasis"><em>python</em></span> at the shell prompt on any standard Unix/Gnu-Linux system and
   547          <a name="id2860477">
       
   548          </a>
       
   549          2.The Python Interpreter
       
   550         </h2>
       
   551        </div>
       
   552       </div>
       
   553      </div>
       
   554      <div class="section" title="2.1.The Interactive Interpreter">
       
   555       <div class="titlepage">
       
   556        <div>
       
   557         <div>
       
   558          <h3 class="title">
       
   559           <a name="id2860486">
       
   560           </a>
       
   561           2.1.The Interactive Interpreter
       
   562          </h3>
       
   563         </div>
       
   564        </div>
       
   565       </div>
       
   566       <p id="ch2intro_17">
       
   567        Typing
       
   568        <span class="emphasis">
       
   569         <em>
       
   570          python
       
   571         </em>
       
   572        </span>
       
   573        at the shell prompt on any standard Unix/Gnu-Linux system and
   174 hitting the enter key fires up the Python 'Interactive Interpreter'. The Python
   574 hitting the enter key fires up the Python 'Interactive Interpreter'. The Python
   175 interpreter is one of the most integral features of Python. The prompt obtained
   575 interpreter is one of the most integral features of Python. The prompt obtained
   176 when the interactive interpreter is similar to what is shown below. The exact
   576 when the interactive interpreter is similar to what is shown below. The exact
   177 appearance might differ based on the version of Python being used. The <code class="literal">&gt;&gt;&gt;</code>
   577 appearance might differ based on the version of Python being used. The
   178 thing shown is the python prompt. When something is typed at the prompt and the
   578        <code class="literal">
       
   579         &gt;&gt;&gt;
       
   580        </code>
       
   581        thing shown is the python prompt. When something is typed at the prompt and the
   179 enter key is hit, the python interpreter interprets the command entered and
   582 enter key is hit, the python interpreter interprets the command entered and
   180 performs the appropriate action. All the examples presented in this document are
   583 performs the appropriate action. All the examples presented in this document are
   181 to be tried hands on, on the interactive interpreter.</p>
   584 to be tried hands on, on the interactive interpreter.
   182 <pre class="programlisting">Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
   585       </p>
       
   586       <pre class="programlisting">
       
   587        Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
   183 [GCC 4.3.2] on linux2
   588 [GCC 4.3.2] on linux2
   184 Type "help", "copyright", "credits" or "license" for more information.
   589 Type "help", "copyright", "credits" or "license" for more information.
   185 &gt;&gt;&gt;</pre>
   590 &gt;&gt;&gt;
   186 <p><a name="ch2intro_18"></a>Lets try with an example, type <code class="literal">print 'Hello, World!'</code> at the prompt and hit
   591       </pre>
   187 the enter key.</p>
   592       <p id="ch2intro_18">
   188 <pre class="programlisting">&gt;&gt;&gt; print 'Hello, World!'
   593        Lets try with an example, type
   189 Hello, World!</pre>
   594        <code class="literal">
   190 <p><a name="ch2intro_19"></a>This example was quite straight forward, and thus we have written our first
   595         print 'Hello, World!'
       
   596        </code>
       
   597        at the prompt and hit
       
   598 the enter key.
       
   599       </p>
       
   600       <pre class="programlisting">
       
   601        &gt;&gt;&gt; print 'Hello, World!'
       
   602 Hello, World!
       
   603       </pre>
       
   604       <p id="ch2intro_19">
       
   605        This example was quite straight forward, and thus we have written our first
   191 line of Python code. Now let us try typing something arbitrary at the prompt.
   606 line of Python code. Now let us try typing something arbitrary at the prompt.
   192 For example:</p>
   607 For example:
   193 <pre class="programlisting">&gt;&gt;&gt; arbit word
   608       </p>
       
   609       <pre class="programlisting">
       
   610        &gt;&gt;&gt; arbit word
   194   File "&lt;stdin&gt;", line 1
   611   File "&lt;stdin&gt;", line 1
   195     arbit word
   612     arbit word
   196             ^
   613             ^
   197 SyntaxError: invalid syntax
   614 SyntaxError: invalid syntax
   198 &gt;&gt;&gt;</pre>
   615 &gt;&gt;&gt;
   199 <p><a name="ch2intro_1a"></a>The interpreter gave an error message saying that 'arbit word' was invalid
   616       </pre>
       
   617       <p id="ch2intro_1a">
       
   618        The interpreter gave an error message saying that 'arbit word' was invalid
   200 syntax which is valid. The interpreter is an amazing tool when learning to
   619 syntax which is valid. The interpreter is an amazing tool when learning to
   201 program in Python. The interpreter provides a help function that provides the
   620 program in Python. The interpreter provides a help function that provides the
   202 necessary documentation regarding all Python syntax, constructs, modules and
   621 necessary documentation regarding all Python syntax, constructs, modules and
   203 objects. Typing <span class="emphasis"><em>help()</em></span> at the prompt gives the following output:</p>
   622 objects. Typing
   204 <pre class="programlisting">&gt;&gt;&gt; help()
   623        <span class="emphasis">
       
   624         <em>
       
   625          help()
       
   626         </em>
       
   627        </span>
       
   628        at the prompt gives the following output:
       
   629       </p>
       
   630       <pre class="programlisting">
       
   631        &gt;&gt;&gt; help()
   205 
   632 
   206 Welcome to Python 2.5!  This is the online help utility.
   633 Welcome to Python 2.5!  This is the online help utility.
   207 
   634 
   208 If this is your first time using Python, you should definitely check out
   635 If this is your first time using Python, you should definitely check out
   209 the tutorial on the Internet at http://www.python.org/doc/tut/.
   636 the tutorial on the Internet at http://www.python.org/doc/tut/.
   215 To get a list of available modules, keywords, or topics, type "modules",
   642 To get a list of available modules, keywords, or topics, type "modules",
   216 "keywords", or "topics".  Each module also comes with a one-line summary
   643 "keywords", or "topics".  Each module also comes with a one-line summary
   217 of what it does; to list the modules whose summaries contain a given word
   644 of what it does; to list the modules whose summaries contain a given word
   218 such as "spam", type "modules spam".
   645 such as "spam", type "modules spam".
   219 
   646 
   220 help&gt;</pre>
   647 help&gt;
   221 <p><a name="ch2intro_1b"></a>As mentioned in the output, entering the name of any module, keyword or topic
   648       </pre>
       
   649       <p id="ch2intro_1b">
       
   650        As mentioned in the output, entering the name of any module, keyword or topic
   222 will provide the documentation and help regarding the same through the online
   651 will provide the documentation and help regarding the same through the online
   223 help utility. Pressing <span class="emphasis"><em>Ctrl+d</em></span> exits the help prompt and returns to the
   652 help utility. Pressing
   224 python prompt.</p>
   653        <span class="emphasis">
   225 <p><a name="ch2intro_1c"></a>Let us now try a few examples at the python interpreter.</p>
   654         <em>
   226 <p><a name="ch2intro_1d"></a>Eg 1:</p>
   655          Ctrl+d
   227 <pre class="programlisting">&gt;&gt;&gt; print 'Hello, python!'
   656         </em>
       
   657        </span>
       
   658        exits the help prompt and returns to the
       
   659 python prompt.
       
   660       </p>
       
   661       <p id="ch2intro_1c">
       
   662        Let us now try a few examples at the python interpreter.
       
   663       </p>
       
   664       <p id="ch2intro_1d">
       
   665        Eg 1:
       
   666       </p>
       
   667       <pre class="programlisting">
       
   668        &gt;&gt;&gt; print 'Hello, python!'
   228 Hello, python!
   669 Hello, python!
   229 &gt;&gt;&gt;</pre>
   670 &gt;&gt;&gt;
   230 <p><a name="ch2intro_1e"></a>Eg 2:</p>
   671       </pre>
   231 <pre class="programlisting">&gt;&gt;&gt; print 4321*567890
   672       <p id="ch2intro_1e">
       
   673        Eg 2:
       
   674       </p>
       
   675       <pre class="programlisting">
       
   676        &gt;&gt;&gt; print 4321*567890
   232 2453852690
   677 2453852690
   233 &gt;&gt;&gt;</pre>
   678 &gt;&gt;&gt;
   234 <p><a name="ch2intro_1f"></a>Eg 3:</p>
   679       </pre>
   235 <pre class="programlisting">&gt;&gt;&gt; 4321*567890
   680       <p id="ch2intro_1f">
       
   681        Eg 3:
       
   682       </p>
       
   683       <pre class="programlisting">
       
   684        &gt;&gt;&gt; 4321*567890
   236 2453852690L
   685 2453852690L
   237 &gt;&gt;&gt;</pre>
   686 &gt;&gt;&gt;
   238 <pre class="programlisting">Note: Notice the 'L' at the end of the output. The 'L' signifies that the
   687       </pre>
       
   688       <pre class="programlisting">
       
   689        Note: Notice the 'L' at the end of the output. The 'L' signifies that the
   239 output of the operation is of type *long*. It was absent in the previous
   690 output of the operation is of type *long*. It was absent in the previous
   240 example because we used the print statement. This is because *print* formats
   691 example because we used the print statement. This is because *print* formats
   241 the output before displaying.</pre>
   692 the output before displaying.
   242 <p><a name="ch2intro_20"></a>Eg 4:</p>
   693       </pre>
   243 <pre class="programlisting">&gt;&gt;&gt; big = 12345678901234567890 ** 3
   694       <p id="ch2intro_20">
       
   695        Eg 4:
       
   696       </p>
       
   697       <pre class="programlisting">
       
   698        &gt;&gt;&gt; big = 12345678901234567890 ** 3
   244 &gt;&gt;&gt; print big
   699 &gt;&gt;&gt; print big
   245 1881676372353657772490265749424677022198701224860897069000
   700 1881676372353657772490265749424677022198701224860897069000
   246 &gt;&gt;&gt;</pre>
   701 &gt;&gt;&gt;
   247 <pre class="programlisting">This example is to show that unlike in C or C++ there is no limit on the
   702       </pre>
   248 value of an integer.</pre>
   703       <pre class="programlisting">
   249 <p><a name="ch2intro_21"></a>Try this on the interactive interpreter:
   704        This example is to show that unlike in C or C++ there is no limit on the
   250 <code class="literal">import this</code></p>
   705 value of an integer.
   251 <p><a name="ch2intro_22"></a><span class="emphasis"><em>Hint: The output gives an idea of Power of Python</em></span></p>
   706       </pre>
   252 </div>
   707       <p id="ch2intro_21">
   253 <div class="section" title="2.2. ipython - An enhanced interactive Python interpreter">
   708        Try this on the interactive interpreter:
   254 <div class="titlepage"><div><div><h3 class="title">
   709        <code class="literal">
   255 <a name="id2631556"></a>2.2. <span class="emphasis"><em>ipython</em></span> - An enhanced interactive Python interpreter</h3></div></div></div>
   710         import this
   256 <p><a name="ch2intro_23"></a>The power and the importance of the interactive interpreter was the highlight
   711        </code>
       
   712       </p>
       
   713       <p id="ch2intro_22">
       
   714        <span class="emphasis">
       
   715         <em>
       
   716          Hint: The output gives an idea of Power of Python
       
   717         </em>
       
   718        </span>
       
   719       </p>
       
   720      </div>
       
   721      <div class="section" title="2.2.ipython - An enhanced interactive Python interpreter">
       
   722       <div class="titlepage">
       
   723        <div>
       
   724         <div>
       
   725          <h3 class="title">
       
   726           <a name="id2860659">
       
   727           </a>
       
   728           2.2.
       
   729           <span class="emphasis">
       
   730            <em>
       
   731             ipython
       
   732            </em>
       
   733           </span>
       
   734           - An enhanced interactive Python interpreter
       
   735          </h3>
       
   736         </div>
       
   737        </div>
       
   738       </div>
       
   739       <p id="ch2intro_23">
       
   740        The power and the importance of the interactive interpreter was the highlight
   257 of the previous section. This section provides insight into the enhanced
   741 of the previous section. This section provides insight into the enhanced
   258 interpreter with more advanced set of features called <span class="strong"><strong>ipython</strong></span>. Entering
   742 interpreter with more advanced set of features called
   259 <span class="emphasis"><em>ipython</em></span> at the shell prompt fires up the interactive interpreter.</p>
   743        <span class="strong">
   260 <pre class="programlisting">$ ipython
   744         <strong>
       
   745          ipython
       
   746         </strong>
       
   747        </span>
       
   748        . Entering
       
   749        <span class="emphasis">
       
   750         <em>
       
   751          ipython
       
   752         </em>
       
   753        </span>
       
   754        at the shell prompt fires up the interactive interpreter.
       
   755       </p>
       
   756       <pre class="programlisting">
       
   757        $ ipython
   261 Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
   758 Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
   262 Type "copyright", "credits" or "license" for more information.
   759 Type "copyright", "credits" or "license" for more information.
   263 
   760 
   264 IPython 0.8.4 -- An enhanced Interactive Python.
   761 IPython 0.8.4 -- An enhanced Interactive Python.
   265 ?         -&gt; Introduction and overview of IPython's features.
   762 ?         -&gt; Introduction and overview of IPython's features.
   266 %quickref -&gt; Quick reference.
   763 %quickref -&gt; Quick reference.
   267 help      -&gt; Python's own help system.
   764 help      -&gt; Python's own help system.
   268 object?   -&gt; Details about 'object'. ?object also works, ?? prints more.
   765 object?   -&gt; Details about 'object'. ?object also works, ?? prints more.
   269 
   766 
   270 In [1]:</pre>
   767 In [1]:
   271 <p><a name="ch2intro_24"></a>This is the output obtained upon firing ipython. The exact appearance may
   768       </pre>
       
   769       <p id="ch2intro_24">
       
   770        This is the output obtained upon firing ipython. The exact appearance may
   272 change based on the Python version installed. The following are some of the
   771 change based on the Python version installed. The following are some of the
   273 various features provided by <span class="strong"><strong>ipython</strong></span>:</p>
   772 various features provided by
   274 <p><a name="ch2intro_25"></a>Suggestions - ipython provides suggestions of the possible methods and
   773        <span class="strong">
   275 operations available for the given python object.</p>
   774         <strong>
   276 <p><a name="ch2intro_26"></a>Eg 5:</p>
   775          ipython
   277 <pre class="programlisting">In [4]: a = 6
   776         </strong>
       
   777        </span>
       
   778        :
       
   779       </p>
       
   780       <p id="ch2intro_25">
       
   781        Suggestions - ipython provides suggestions of the possible methods and
       
   782 operations available for the given python object.
       
   783       </p>
       
   784       <p id="ch2intro_26">
       
   785        Eg 5:
       
   786       </p>
       
   787       <pre class="programlisting">
       
   788        In [4]: a = 6
   278 
   789 
   279 In [5]: a.
   790 In [5]: a.
   280 a.__abs__           a.__divmod__        a.__index__         a.__neg__          a.__rand__          a.__rmod__          a.__rxor__
   791 a.__abs__           a.__divmod__        a.__index__         a.__neg__          a.__rand__          a.__rmod__          a.__rxor__
   281 a.__add__           a.__doc__           a.__init__          a.__new__          a.__rdiv__          a.__rmul__          a.__setattr__
   792 a.__add__           a.__doc__           a.__init__          a.__new__          a.__rdiv__          a.__rmul__          a.__setattr__
   282 a.__and__           a.__float__         a.__int__           a.__nonzero__      a.__rdivmod__       a.__ror__           a.__str__
   793 a.__and__           a.__float__         a.__int__           a.__nonzero__      a.__rdivmod__       a.__ror__           a.__str__
   283 a.__class__         a.__floordiv__      a.__invert__        a.__oct__          a.__reduce__        a.__rpow__          a.__sub__
   794 a.__class__         a.__floordiv__      a.__invert__        a.__oct__          a.__reduce__        a.__rpow__          a.__sub__
   284 a.__cmp__           a.__getattribute__  a.__long__          a.__or__           a.__reduce_ex__     a.__rrshift__       a.__truediv__
   795 a.__cmp__           a.__getattribute__  a.__long__          a.__or__           a.__reduce_ex__     a.__rrshift__       a.__truediv__
   285 a.__coerce__        a.__getnewargs__    a.__lshift__        a.__pos__          a.__repr__          a.__rshift__        a.__xor__
   796 a.__coerce__        a.__getnewargs__    a.__lshift__        a.__pos__          a.__repr__          a.__rshift__        a.__xor__
   286 a.__delattr__       a.__hash__          a.__mod__           a.__pow__          a.__rfloordiv__     a.__rsub__
   797 a.__delattr__       a.__hash__          a.__mod__           a.__pow__          a.__rfloordiv__     a.__rsub__
   287 a.__div__           a.__hex__           a.__mul__           a.__radd__         a.__rlshift__       a.__rtruediv__</pre>
   798 a.__div__           a.__hex__           a.__mul__           a.__radd__         a.__rlshift__       a.__rtruediv__
   288 <p><a name="ch2intro_27"></a>In this example, we initialized 'a' (a variable - a concept that will be
   799       </pre>
   289 discussed in the subsequent sections.) to 6. In the next line when the <span class="emphasis"><em>tab</em></span> key
   800       <p id="ch2intro_27">
   290 is pressed after typing '<span class="emphasis"><em>a.</em></span>' ipython displays the set of all possible methods
   801        In this example, we initialized 'a' (a variable - a concept that will be
       
   802 discussed in the subsequent sections.) to 6. In the next line when the
       
   803        <span class="emphasis">
       
   804         <em>
       
   805          tab
       
   806         </em>
       
   807        </span>
       
   808        key
       
   809 is pressed after typing '
       
   810        <span class="emphasis">
       
   811         <em>
       
   812          a.
       
   813         </em>
       
   814        </span>
       
   815        ' ipython displays the set of all possible methods
   291 that are applicable on the object 'a' (an integer in this context). Ipython
   816 that are applicable on the object 'a' (an integer in this context). Ipython
   292 provides many such datatype specific features which will be presented in the
   817 provides many such datatype specific features which will be presented in the
   293 further sections as and when the datatypes are introduced.</p>
   818 further sections as and when the datatypes are introduced.
   294 </div>
   819       </p>
   295 </div>
   820      </div>
   296 <div class="section" title="3. Editing and running a python file">
   821     </div>
   297 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   822     <div class="section" title="3.Editing and running a python file">
   298 <a name="id2631681"></a>3. Editing and running a python file</h2></div></div></div>
   823      <div class="titlepage">
   299 <p><a name="ch2intro_28"></a>The previous sections focused on the use of the interpreter to run python code.
   824       <div>
       
   825        <div>
       
   826         <h2 class="title" style="clear: both">
       
   827          <a name="id2860784">
       
   828          </a>
       
   829          3.Editing and running a python file
       
   830         </h2>
       
   831        </div>
       
   832       </div>
       
   833      </div>
       
   834      <p id="ch2intro_28">
       
   835       The previous sections focused on the use of the interpreter to run python code.
   300 While the interpeter is an excellent tool to test simple solutions and
   836 While the interpeter is an excellent tool to test simple solutions and
   301 experiment with small code snippets, its main disadvantage is that everything
   837 experiment with small code snippets, its main disadvantage is that everything
   302 written in the interpreter is lost once its quit. Most of the times a program is
   838 written in the interpreter is lost once its quit. Most of the times a program is
   303 used by people other than the author. So the programs have to be available in
   839 used by people other than the author. So the programs have to be available in
   304 some form suitable for distribution, and hence they are written in files. This
   840 some form suitable for distribution, and hence they are written in files. This
   305 section will focus on editing and running python files. Start by opening a text
   841 section will focus on editing and running python files. Start by opening a text
   306 editor ( it is recommended you choose one from the list at the top of this page ).
   842 editor ( it is recommended you choose one from the list at the top of this page ).
   307 In the editor type down python code and save the file with an extension <span class="strong"><strong>.py</strong></span>
   843 In the editor type down python code and save the file with an extension
   308 (python files have an extension of .py). Once done with the editing, save the
   844       <span class="strong">
   309 file and exit the editor.</p>
   845        <strong>
   310 <p><a name="ch2intro_29"></a>Let us look at a simple example of calculating the gcd of 2 numbers using Python:</p>
   846         .py
   311 <p><a name="ch2intro_2a"></a><span class="strong"><strong>Creating the first python script(file)</strong></span></p>
   847        </strong>
   312 <pre class="programlisting">$ emacs gcd.py
   848       </span>
       
   849       (python files have an extension of .py). Once done with the editing, save the
       
   850 file and exit the editor.
       
   851      </p>
       
   852      <p id="ch2intro_29">
       
   853       Let us look at a simple example of calculating the gcd of 2 numbers using Python:
       
   854      </p>
       
   855      <p id="ch2intro_2a">
       
   856       <span class="strong">
       
   857        <strong>
       
   858         Creating the first python script(file)
       
   859        </strong>
       
   860       </span>
       
   861      </p>
       
   862      <pre class="programlisting">
       
   863       $ emacs gcd.py
   313   def gcd(x,y):
   864   def gcd(x,y):
   314     if x % y == 0:
   865     if x % y == 0:
   315       return y
   866       return y
   316     return gcd(y, x%y)
   867     return gcd(y, x%y)
   317 
   868 
   318   print gcd(72, 92)</pre>
   869   print gcd(72, 92)
   319 <p><a name="ch2intro_2b"></a>To run the script, open the shell prompt, navigate to the directory that
   870      </pre>
   320 contains the python file and run <code class="literal">python &lt;filename.py&gt;</code> at the prompt ( in this
   871      <p id="ch2intro_2b">
   321 case filename is gcd.py )</p>
   872       To run the script, open the shell prompt, navigate to the directory that
   322 <p><a name="ch2intro_2c"></a><span class="strong"><strong>Running the python script</strong></span></p>
   873 contains the python file and run
   323 <pre class="programlisting">$ python gcd.py
   874       <code class="literal">
       
   875        python &lt;filename.py&gt;
       
   876       </code>
       
   877       at the prompt ( in this
       
   878 case filename is gcd.py )
       
   879      </p>
       
   880      <p id="ch2intro_2c">
       
   881       <span class="strong">
       
   882        <strong>
       
   883         Running the python script
       
   884        </strong>
       
   885       </span>
       
   886      </p>
       
   887      <pre class="programlisting">
       
   888       $ python gcd.py
   324 4
   889 4
   325 $</pre>
   890 $
   326 <p><a name="ch2intro_2d"></a>Another method to run a python script would be to include the line</p>
   891      </pre>
   327 <p><a name="ch2intro_2e"></a><code class="literal">#! /usr/bin/python</code></p>
   892      <p id="ch2intro_2d">
   328 <p><a name="ch2intro_2f"></a>at the beginning of the python file and then make the file executable by</p>
   893       Another method to run a python script would be to include the line
   329 <p><a name="ch2intro_30"></a>$ chmod a+x <span class="emphasis"><em>filename.py</em></span></p>
   894      </p>
   330 <p><a name="ch2intro_31"></a>Once this is done, the script can be run as a standalone program as follows:</p>
   895      <p id="ch2intro_2e">
   331 <p><a name="ch2intro_32"></a>$ ./<span class="emphasis"><em>filename.py</em></span></p>
   896       <code class="literal">
   332 </div>
   897        #! /usr/bin/python
   333 <div class="section" title="4. Basic Datatypes and operators in Python">
   898       </code>
   334 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
   899      </p>
   335 <a name="id2631788"></a>4. Basic Datatypes and operators in Python</h2></div></div></div>
   900      <p id="ch2intro_2f">
   336 <p><a name="ch2intro_33"></a>Python provides the following set of basic datatypes.</p>
   901       at the beginning of the python file and then make the file executable by
   337 <div class="itemizedlist"><ul class="itemizedlist" type="*">
   902      </p>
   338 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_34"></a>Numbers: int, float, long, complex</p></li>
   903      <p id="ch2intro_30">
   339 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_35"></a>Strings</p></li>
   904       $ chmod a+x
   340 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_36"></a>Boolean</p></li>
   905       <span class="emphasis">
   341 </ul></div>
   906        <em>
   342 <div class="section" title="4.1. Numbers">
   907         filename.py
   343 <div class="titlepage"><div><div><h3 class="title">
   908        </em>
   344 <a name="id2631822"></a>4.1. Numbers</h3></div></div></div>
   909       </span>
   345 <p><a name="ch2intro_37"></a>Numbers were introduced in the examples presented in the interactive interpreter
   910      </p>
       
   911      <p id="ch2intro_31">
       
   912       Once this is done, the script can be run as a standalone program as follows:
       
   913      </p>
       
   914      <p id="ch2intro_32">
       
   915       $ ./
       
   916       <span class="emphasis">
       
   917        <em>
       
   918         filename.py
       
   919        </em>
       
   920       </span>
       
   921      </p>
       
   922     </div>
       
   923     <div class="section" title="4.Basic Datatypes and operators in Python">
       
   924      <div class="titlepage">
       
   925       <div>
       
   926        <div>
       
   927         <h2 class="title" style="clear: both">
       
   928          <a name="id2860890">
       
   929          </a>
       
   930          4.Basic Datatypes and operators in Python
       
   931         </h2>
       
   932        </div>
       
   933       </div>
       
   934      </div>
       
   935      <p id="ch2intro_33">
       
   936       Python provides the following set of basic datatypes.
       
   937      </p>
       
   938      <div class="itemizedlist">
       
   939       <ul class="itemizedlist" type="*">
       
   940        <li class="listitem" style="list-style-type: *">
       
   941         <p id="ch2intro_34">
       
   942          Numbers: int, float, long, complex
       
   943         </p>
       
   944        </li>
       
   945        <li class="listitem" style="list-style-type: *">
       
   946         <p id="ch2intro_35">
       
   947          Strings
       
   948         </p>
       
   949        </li>
       
   950        <li class="listitem" style="list-style-type: *">
       
   951         <p id="ch2intro_36">
       
   952          Boolean
       
   953         </p>
       
   954        </li>
       
   955       </ul>
       
   956      </div>
       
   957      <div class="section" title="4.1.Numbers">
       
   958       <div class="titlepage">
       
   959        <div>
       
   960         <div>
       
   961          <h3 class="title">
       
   962           <a name="id2860925">
       
   963           </a>
       
   964           4.1.Numbers
       
   965          </h3>
       
   966         </div>
       
   967        </div>
       
   968       </div>
       
   969       <p id="ch2intro_37">
       
   970        Numbers were introduced in the examples presented in the interactive interpreter
   346 section. Numbers include types as mentioned earlier viz., int (integers), float
   971 section. Numbers include types as mentioned earlier viz., int (integers), float
   347 (floating point numbers), long (large integers), complex (complex numbers with
   972 (floating point numbers), long (large integers), complex (complex numbers with
   348 real and imaginary parts). Python is not a strongly typed language, which means
   973 real and imaginary parts). Python is not a strongly typed language, which means
   349 the type of a variable need not mentioned during its initialization. Let us look
   974 the type of a variable need not mentioned during its initialization. Let us look
   350 at a few examples.</p>
   975 at a few examples.
   351 <p><a name="ch2intro_38"></a>Eg 6:</p>
   976       </p>
   352 <pre class="programlisting">&gt;&gt;&gt; a = 1 #here a is an integer variable</pre>
   977       <p id="ch2intro_38">
   353 <p><a name="ch2intro_39"></a>Eg 7:</p>
   978        Eg 6:
   354 <pre class="programlisting">&gt;&gt;&gt; lng = 122333444455555666666777777788888888999999999 #here lng is a variable of type long
   979       </p>
       
   980       <pre class="programlisting">
       
   981        &gt;&gt;&gt; a = 1 #here a is an integer variable
       
   982       </pre>
       
   983       <p id="ch2intro_39">
       
   984        Eg 7:
       
   985       </p>
       
   986       <pre class="programlisting">
       
   987        &gt;&gt;&gt; lng = 122333444455555666666777777788888888999999999 #here lng is a variable of type long
   355 &gt;&gt;&gt; lng
   988 &gt;&gt;&gt; lng
   356 122333444455555666666777777788888888999999999L #notice the trailing 'L'
   989 122333444455555666666777777788888888999999999L #notice the trailing 'L'
   357 &gt;&gt;&gt; print lng
   990 &gt;&gt;&gt; print lng
   358 122333444455555666666777777788888888999999999 #notice the absence of the trailing 'L'
   991 122333444455555666666777777788888888999999999 #notice the absence of the trailing 'L'
   359 &gt;&gt;&gt; lng+1
   992 &gt;&gt;&gt; lng+1
   360 122333444455555666666777777788888889000000000L</pre>
   993 122333444455555666666777777788888889000000000L
   361 <p><a name="ch2intro_3a"></a>Long numbers are the same as integers in almost all aspects. They can be used in
   994       </pre>
       
   995       <p id="ch2intro_3a">
       
   996        Long numbers are the same as integers in almost all aspects. They can be used in
   362 operations just like integers and along with integers without any distinction.
   997 operations just like integers and along with integers without any distinction.
   363 The only distinction comes during type checking (which is not a healthy practice).
   998 The only distinction comes during type checking (which is not a healthy practice).
   364 Long numbers are tucked with a trailing 'L' just to signify that they are long.
   999 Long numbers are tucked with a trailing 'L' just to signify that they are long.
   365 Notice that in the example just lng at the prompt displays the value of the variable
  1000 Notice that in the example just lng at the prompt displays the value of the variable
   366 with the 'L' whereas <code class="literal">print lng</code> displays without the 'L'. This is because print
  1001 with the 'L' whereas
       
  1002        <code class="literal">
       
  1003         print lng
       
  1004        </code>
       
  1005        displays without the 'L'. This is because print
   367 formats the output before printing. Also in the example, notice that adding an
  1006 formats the output before printing. Also in the example, notice that adding an
   368 integer to a long does not give any errors and the result is as expected. So for
  1007 integer to a long does not give any errors and the result is as expected. So for
   369 all practical purposes longs can be treated as ints.</p>
  1008 all practical purposes longs can be treated as ints.
   370 <p><a name="ch2intro_3b"></a>Eg 8:</p>
  1009       </p>
   371 <pre class="programlisting">&gt;&gt;&gt; fl = 3.14159 #fl is a float variable
  1010       <p id="ch2intro_3b">
       
  1011        Eg 8:
       
  1012       </p>
       
  1013       <pre class="programlisting">
       
  1014        &gt;&gt;&gt; fl = 3.14159 #fl is a float variable
   372 &gt;&gt;&gt; e = 1.234e-4 #e is also a float variable, specified in the exponential form
  1015 &gt;&gt;&gt; e = 1.234e-4 #e is also a float variable, specified in the exponential form
   373 &gt;&gt;&gt; a = 1
  1016 &gt;&gt;&gt; a = 1
   374 &gt;&gt;&gt; b = 2
  1017 &gt;&gt;&gt; b = 2
   375 &gt;&gt;&gt; a/b #integer division
  1018 &gt;&gt;&gt; a/b #integer division
   376 0
  1019 0
   377 &gt;&gt;&gt; a/fl #floating point division
  1020 &gt;&gt;&gt; a/fl #floating point division
   378 0.31831015504887655
  1021 0.31831015504887655
   379 &gt;&gt;&gt; e/fl
  1022 &gt;&gt;&gt; e/fl
   380 3.9279473133031364e-05</pre>
  1023 3.9279473133031364e-05
   381 <p><a name="ch2intro_3c"></a>Floating point numbers, simply called floats are real numbers with a decimal point.
  1024       </pre>
       
  1025       <p id="ch2intro_3c">
       
  1026        Floating point numbers, simply called floats are real numbers with a decimal point.
   382 The example above shows the initialization of a float variable. Shown also in this
  1027 The example above shows the initialization of a float variable. Shown also in this
   383 example is the difference between integer division and floating point division.
  1028 example is the difference between integer division and floating point division.
   384 'a' and 'b' here are integer variables and hence the division gives 0 as the quotient.
  1029 'a' and 'b' here are integer variables and hence the division gives 0 as the quotient.
   385 When either of the operands is a float, the operation is a floating point division,
  1030 When either of the operands is a float, the operation is a floating point division,
   386 and the result is also a float as illustrated.</p>
  1031 and the result is also a float as illustrated.
   387 <p><a name="ch2intro_3d"></a>Eg 9:</p>
  1032       </p>
   388 <pre class="programlisting">&gt;&gt;&gt; cplx = 3 + 4j #cplx is a complex variable
  1033       <p id="ch2intro_3d">
       
  1034        Eg 9:
       
  1035       </p>
       
  1036       <pre class="programlisting">
       
  1037        &gt;&gt;&gt; cplx = 3 + 4j #cplx is a complex variable
   389 &gt;&gt;&gt; cplx
  1038 &gt;&gt;&gt; cplx
   390 (3+4j)
  1039 (3+4j)
   391 &gt;&gt;&gt; print cplx.real #prints the real part of the complex number
  1040 &gt;&gt;&gt; print cplx.real #prints the real part of the complex number
   392 3.0
  1041 3.0
   393 &gt;&gt;&gt; print cplx.imag #prints the imaginary part of the complex number
  1042 &gt;&gt;&gt; print cplx.imag #prints the imaginary part of the complex number
   394 4.0
  1043 4.0
   395 &gt;&gt;&gt; print cplx*fl  #multiplies the real and imag parts of the complex number with the multiplier
  1044 &gt;&gt;&gt; print cplx*fl  #multiplies the real and imag parts of the complex number with the multiplier
   396 (9.42477+12.56636j)
  1045 (9.42477+12.56636j)
   397 &gt;&gt;&gt; abs(cplx) #returns the absolute value of the complex number
  1046 &gt;&gt;&gt; abs(cplx) #returns the absolute value of the complex number
   398 5.0</pre>
  1047 5.0
   399 <p><a name="ch2intro_3e"></a>Python provides a datatype for complex numbers. Complex numbers are initialized
  1048       </pre>
   400 as shown in the example above. The <span class="emphasis"><em>real</em></span> and <span class="emphasis"><em>imag</em></span> operators return the real and
  1049       <p id="ch2intro_3e">
   401 imaginary parts of the complex number as shown. The <span class="emphasis"><em>abs()</em></span> returns the absolute
  1050        Python provides a datatype for complex numbers. Complex numbers are initialized
   402 value of the complex number.</p>
  1051 as shown in the example above. The
   403 </div>
  1052        <span class="emphasis">
   404 <div class="section" title="4.2. Variables">
  1053         <em>
   405 <div class="titlepage"><div><div><h3 class="title">
  1054          real
   406 <a name="id2631952"></a>4.2. Variables</h3></div></div></div>
  1055         </em>
   407 <p><a name="ch2intro_3f"></a>Variables are just names that represent a value. Variables have already been
  1056        </span>
       
  1057        and
       
  1058        <span class="emphasis">
       
  1059         <em>
       
  1060          imag
       
  1061         </em>
       
  1062        </span>
       
  1063        operators return the real and
       
  1064 imaginary parts of the complex number as shown. The
       
  1065        <span class="emphasis">
       
  1066         <em>
       
  1067          abs()
       
  1068         </em>
       
  1069        </span>
       
  1070        returns the absolute
       
  1071 value of the complex number.
       
  1072       </p>
       
  1073      </div>
       
  1074      <div class="section" title="4.2.Variables">
       
  1075       <div class="titlepage">
       
  1076        <div>
       
  1077         <div>
       
  1078          <h3 class="title">
       
  1079           <a name="id2861055">
       
  1080           </a>
       
  1081           4.2.Variables
       
  1082          </h3>
       
  1083         </div>
       
  1084        </div>
       
  1085       </div>
       
  1086       <p id="ch2intro_3f">
       
  1087        Variables are just names that represent a value. Variables have already been
   408 introduced in the various examples from the previous sections. Certain rules about
  1088 introduced in the various examples from the previous sections. Certain rules about
   409 using variables:</p>
  1089 using variables:
   410 <div class="itemizedlist"><ul class="itemizedlist" type="*">
  1090       </p>
   411 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_40"></a>Variables have to be initialized or assigned a value before being used.</p></li>
  1091       <div class="itemizedlist">
   412 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_41"></a>Variable names can consist of letters, digits and underscores(_).</p></li>
  1092        <ul class="itemizedlist" type="*">
   413 <li class="listitem" style="list-style-type: *"><p><a name="ch2intro_42"></a>Variable names cannot begin with digits, but can contain digits in them.</p></li>
  1093         <li class="listitem" style="list-style-type: *">
   414 </ul></div>
  1094          <p id="ch2intro_40">
   415 <p><a name="ch2intro_43"></a>In reference to the previous section examples, 'a', 'b', 'lng', 'fl', 'e' and 'cplx'
  1095           Variables have to be initialized or assigned a value before being used.
   416 are all variables of various datatypes.</p>
  1096          </p>
   417 <pre class="programlisting">Note: Python is not a strongly typed language and hence an integer variable can at a
  1097         </li>
   418 later stage be used as a float variable as well.</pre>
  1098         <li class="listitem" style="list-style-type: *">
   419 </div>
  1099          <p id="ch2intro_41">
   420 <div class="section" title="4.3. Strings">
  1100           Variable names can consist of letters, digits and underscores(_).
   421 <div class="titlepage"><div><div><h3 class="title">
  1101          </p>
   422 <a name="id2632000"></a>4.3. Strings</h3></div></div></div>
  1102         </li>
   423 <p><a name="ch2intro_44"></a>Strings are one of the essential data structures of any programming language.
  1103         <li class="listitem" style="list-style-type: *">
   424 The <code class="literal">print "Hello, World!"</code> program was introduced in the earlier section, and
  1104          <p id="ch2intro_42">
   425 the <span class="emphasis"><em>"Hello, World!"</em></span> in the print statement is a string. A string is basically
  1105           Variable names cannot begin with digits, but can contain digits in them.
   426 a set of characters. Strings can be represented in various ways shown below:</p>
  1106          </p>
   427 <pre class="programlisting">s = 'this is a string'              # a string variable can be represented using single quotes
  1107         </li>
       
  1108        </ul>
       
  1109       </div>
       
  1110       <p id="ch2intro_43">
       
  1111        In reference to the previous section examples, 'a', 'b', 'lng', 'fl', 'e' and 'cplx'
       
  1112 are all variables of various datatypes.
       
  1113       </p>
       
  1114       <pre class="programlisting">
       
  1115        Note: Python is not a strongly typed language and hence an integer variable can at a
       
  1116 later stage be used as a float variable as well.
       
  1117       </pre>
       
  1118      </div>
       
  1119      <div class="section" title="4.3.Strings">
       
  1120       <div class="titlepage">
       
  1121        <div>
       
  1122         <div>
       
  1123          <h3 class="title">
       
  1124           <a name="id2861102">
       
  1125           </a>
       
  1126           4.3.Strings
       
  1127          </h3>
       
  1128         </div>
       
  1129        </div>
       
  1130       </div>
       
  1131       <p id="ch2intro_44">
       
  1132        Strings are one of the essential data structures of any programming language.
       
  1133 The
       
  1134        <code class="literal">
       
  1135         print "Hello, World!"
       
  1136        </code>
       
  1137        program was introduced in the earlier section, and
       
  1138 the
       
  1139        <span class="emphasis">
       
  1140         <em>
       
  1141          "Hello, World!"
       
  1142         </em>
       
  1143        </span>
       
  1144        in the print statement is a string. A string is basically
       
  1145 a set of characters. Strings can be represented in various ways shown below:
       
  1146       </p>
       
  1147       <pre class="programlisting">
       
  1148        s = 'this is a string'              # a string variable can be represented using single quotes
   428 s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown
  1149 s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown
   429 s = "I have 'single-quotes' inside!"
  1150 s = "I have 'single-quotes' inside!"
   430 l = "A string spanning many lines\
  1151 l = "A string spanning many lines 
   431 one more line\
  1152 one more line 
   432 yet another"                        # a string can span more than a single line.
  1153 yet another"                        # a string can span more than a single line.
   433 t = """A triple quoted string does  # another way of representing multiline strings.
  1154 t = """A triple quoted string does  # another way of representing multiline strings.
   434 not need to be escaped at the end and
  1155 not need to be escaped at the end and
   435 "can have nested quotes" etc."""</pre>
  1156 "can have nested quotes" etc."""
   436 <p><a name="ch2intro_45"></a>Try the following on the interpreter:
  1157       </pre>
   437 <code class="literal">s = 'this is a string with 'quotes' of similar kind'</code></p>
  1158       <p id="ch2intro_45">
   438 <p><a name="ch2intro_46"></a><span class="strong"><strong>Exercise: How to use single quotes within single quotes in a string as shown
  1159        Try the following on the interpreter:
   439 in the above example without getting an error?</strong></span></p>
  1160        <code class="literal">
   440 <div class="section" title="4.3.1. String operations">
  1161         s = 'this is a string with 'quotes' of similar kind'
   441 <div class="titlepage"><div><div><h4 class="title">
  1162        </code>
   442 <a name="id2632051"></a>4.3.1. String operations</h4></div></div></div>
  1163       </p>
   443 <p><a name="ch2intro_47"></a>A few basic string operations are presented here.</p>
  1164       <p id="ch2intro_46">
   444 <p><a name="ch2intro_48"></a><span class="strong"><strong>String concatenation</strong></span>
  1165        <span class="strong">
   445 String concatenation is done by simple addition of two strings.</p>
  1166         <strong>
   446 <pre class="programlisting">&gt;&gt;&gt; x = 'Hello'
  1167          Exercise: How to use single quotes within single quotes in a string as shown
       
  1168 in the above example without getting an error?
       
  1169         </strong>
       
  1170        </span>
       
  1171       </p>
       
  1172       <div class="section" title="4.3.1.String operations">
       
  1173        <div class="titlepage">
       
  1174         <div>
       
  1175          <div>
       
  1176           <h4 class="title">
       
  1177            <a name="id2861154">
       
  1178            </a>
       
  1179            4.3.1.String operations
       
  1180           </h4>
       
  1181          </div>
       
  1182         </div>
       
  1183        </div>
       
  1184        <p id="ch2intro_47">
       
  1185         A few basic string operations are presented here.
       
  1186        </p>
       
  1187        <p id="ch2intro_48">
       
  1188         <span class="strong">
       
  1189          <strong>
       
  1190           String concatenation
       
  1191          </strong>
       
  1192         </span>
       
  1193         String concatenation is done by simple addition of two strings.
       
  1194        </p>
       
  1195        <pre class="programlisting">
       
  1196         &gt;&gt;&gt; x = 'Hello'
   447 &gt;&gt;&gt; y = ' Python'
  1197 &gt;&gt;&gt; y = ' Python'
   448 &gt;&gt;&gt; print x+y
  1198 &gt;&gt;&gt; print x+y
   449 Hello Python</pre>
  1199 Hello Python
   450 <p><a name="ch2intro_49"></a><span class="emphasis"><em>Try this yourself:</em></span></p>
  1200        </pre>
   451 <pre class="programlisting">&gt;&gt;&gt; somenum = 13
  1201        <p id="ch2intro_49">
   452 &gt;&gt;&gt; print x+somenum</pre>
  1202         <span class="emphasis">
   453 <p><a name="ch2intro_4a"></a>The problem with the above example is that here a string variable and an integer
  1203          <em>
       
  1204           Try this yourself:
       
  1205          </em>
       
  1206         </span>
       
  1207        </p>
       
  1208        <pre class="programlisting">
       
  1209         &gt;&gt;&gt; somenum = 13
       
  1210 &gt;&gt;&gt; print x+somenum
       
  1211        </pre>
       
  1212        <p id="ch2intro_4a">
       
  1213         The problem with the above example is that here a string variable and an integer
   454 variable are trying to be concantenated. To obtain the desired result from the
  1214 variable are trying to be concantenated. To obtain the desired result from the
   455 above example the str(), repr() and the `` can be used.</p>
  1215 above example the str(), repr() and the `` can be used.
   456 <p><a name="ch2intro_4b"></a><span class="strong"><strong>str()</strong></span> simply converts a value to a string in a reasonable form.
  1216        </p>
   457 <span class="strong"><strong>repr()</strong></span> creates a string that is a representation of the value.</p>
  1217        <p id="ch2intro_4b">
   458 <p><a name="ch2intro_4c"></a>The difference can be seen in the example shown below:</p>
  1218         <span class="strong">
   459 <pre class="programlisting">&gt;&gt;&gt; str(1000000000000000000000000000000000000000000000000L)
  1219          <strong>
       
  1220           str()
       
  1221          </strong>
       
  1222         </span>
       
  1223         simply converts a value to a string in a reasonable form.
       
  1224         <span class="strong">
       
  1225          <strong>
       
  1226           repr()
       
  1227          </strong>
       
  1228         </span>
       
  1229         creates a string that is a representation of the value.
       
  1230        </p>
       
  1231        <p id="ch2intro_4c">
       
  1232         The difference can be seen in the example shown below:
       
  1233        </p>
       
  1234        <pre class="programlisting">
       
  1235         &gt;&gt;&gt; str(1000000000000000000000000000000000000000000000000L)
   460 '1000000000000000000000000000000000000000000000000'
  1236 '1000000000000000000000000000000000000000000000000'
   461 &gt;&gt;&gt; repr(1000000000000000000000000000000000000000000000000L)
  1237 &gt;&gt;&gt; repr(1000000000000000000000000000000000000000000000000L)
   462 '1000000000000000000000000000000000000000000000000L'</pre>
  1238 '1000000000000000000000000000000000000000000000000L'
   463 <p><a name="ch2intro_4d"></a>It can be observed that the 'L' in the long value shown was omitted by str(),
  1239        </pre>
       
  1240        <p id="ch2intro_4d">
       
  1241         It can be observed that the 'L' in the long value shown was omitted by str(),
   464 whereas repr() converted that into a string too. An alternative way of using
  1242 whereas repr() converted that into a string too. An alternative way of using
   465 repr(value) is <code class="literal">`value`</code>.</p>
  1243 repr(value) is
   466 <p><a name="ch2intro_4e"></a>A few more examples:</p>
  1244         <code class="literal">
   467 <pre class="programlisting">&gt;&gt;&gt; x = "Let's go \nto Pycon"
  1245          `value`
       
  1246         </code>
       
  1247         .
       
  1248        </p>
       
  1249        <p id="ch2intro_4e">
       
  1250         A few more examples:
       
  1251        </p>
       
  1252        <pre class="programlisting">
       
  1253         &gt;&gt;&gt; x = "Let's go  nto Pycon"
   468 &gt;&gt;&gt; print x
  1254 &gt;&gt;&gt; print x
   469 Let's go
  1255 Let's go
   470 to Pycon</pre>
  1256 to Pycon
   471 <p><a name="ch2intro_4f"></a>In the above example, notice that the 'n'(newline) character is formatted and
  1257        </pre>
       
  1258        <p id="ch2intro_4f">
       
  1259         In the above example, notice that the 'n'(newline) character is formatted and
   472 the string is printed on two lines. The strings discussed until now were normal
  1260 the string is printed on two lines. The strings discussed until now were normal
   473 strings. Other than these there are two other types of strings namely, raw strings
  1261 strings. Other than these there are two other types of strings namely, raw strings
   474 and unicode strings.</p>
  1262 and unicode strings.
   475 <p><a name="ch2intro_50"></a><span class="strong"><strong>Raw strings</strong></span> are strings which are unformatted, that is the backslashes() are
  1263        </p>
       
  1264        <p id="ch2intro_50">
       
  1265         <span class="strong">
       
  1266          <strong>
       
  1267           Raw strings
       
  1268          </strong>
       
  1269         </span>
       
  1270         are strings which are unformatted, that is the backslashes() are
   476 not parsed and are left as it is in the string. Raw strings are represented with
  1271 not parsed and are left as it is in the string. Raw strings are represented with
   477 an 'r' at the start of a string.
  1272 an 'r' at the start of a string.
   478 Let us look at an example</p>
  1273 Let us look at an example
   479 <pre class="programlisting">&gt;&gt;&gt; x = r"Let's go \nto Pycon"
  1274        </p>
       
  1275        <pre class="programlisting">
       
  1276         &gt;&gt;&gt; x = r"Let's go  nto Pycon"
   480 &gt;&gt;&gt; print x
  1277 &gt;&gt;&gt; print x
   481 Let's go \nto Pycon</pre>
  1278 Let's go  nto Pycon
   482 <p><a name="ch2intro_51"></a>Note: The 'n' is not being parsed into a new line and is left as it is.</p>
  1279        </pre>
   483 <p><a name="ch2intro_52"></a><span class="emphasis"><em>Try this yourself:</em></span></p>
  1280        <p id="ch2intro_51">
   484 <pre class="programlisting">&gt;&gt;&gt; x = r"Let's go to Pycon\"</pre>
  1281         Note: The 'n' is not being parsed into a new line and is left as it is.
   485 <p><a name="ch2intro_53"></a><span class="strong"><strong>Unicode strings</strong></span> are strings where the characters are Unicode characters as
  1282        </p>
       
  1283        <p id="ch2intro_52">
       
  1284         <span class="emphasis">
       
  1285          <em>
       
  1286           Try this yourself:
       
  1287          </em>
       
  1288         </span>
       
  1289        </p>
       
  1290        <pre class="programlisting">
       
  1291         &gt;&gt;&gt; x = r"Let's go to Pycon "
       
  1292        </pre>
       
  1293        <p id="ch2intro_53">
       
  1294         <span class="strong">
       
  1295          <strong>
       
  1296           Unicode strings
       
  1297          </strong>
       
  1298         </span>
       
  1299         are strings where the characters are Unicode characters as
   486 opposed to ASCII characters. Unicode strings are represented with a 'u' at the
  1300 opposed to ASCII characters. Unicode strings are represented with a 'u' at the
   487 start of the string.
  1301 start of the string.
   488 Let us look at an example:</p>
  1302 Let us look at an example:
   489 <pre class="programlisting">&gt;&gt;&gt; x = u"Let's go to Pycon!"
  1303        </p>
       
  1304        <pre class="programlisting">
       
  1305         &gt;&gt;&gt; x = u"Let's go to Pycon!"
   490 &gt;&gt;&gt; print x
  1306 &gt;&gt;&gt; print x
   491 Let's go to Pycon!</pre>
  1307 Let's go to Pycon!
   492 </div>
  1308        </pre>
   493 </div>
  1309       </div>
   494 <div class="section" title="4.4. Boolean">
  1310      </div>
   495 <div class="titlepage"><div><div><h3 class="title">
  1311      <div class="section" title="4.4.Boolean">
   496 <a name="id2632194"></a>4.4. Boolean</h3></div></div></div>
  1312       <div class="titlepage">
   497 <p><a name="ch2intro_54"></a>Python also provides special Boolean datatype. A boolean variable can assume a
  1313        <div>
   498 value of either <span class="emphasis"><em>True</em></span> or <span class="emphasis"><em>False</em></span> (Note the capitalizations).</p>
  1314         <div>
   499 <p><a name="ch2intro_55"></a>Let us look at examples:</p>
  1315          <h3 class="title">
   500 <pre class="programlisting">&gt;&gt;&gt; t = True
  1316           <a name="id2861297">
       
  1317           </a>
       
  1318           4.4.Boolean
       
  1319          </h3>
       
  1320         </div>
       
  1321        </div>
       
  1322       </div>
       
  1323       <p id="ch2intro_54">
       
  1324        Python also provides special Boolean datatype. A boolean variable can assume a
       
  1325 value of either
       
  1326        <span class="emphasis">
       
  1327         <em>
       
  1328          True
       
  1329         </em>
       
  1330        </span>
       
  1331        or
       
  1332        <span class="emphasis">
       
  1333         <em>
       
  1334          False
       
  1335         </em>
       
  1336        </span>
       
  1337        (Note the capitalizations).
       
  1338       </p>
       
  1339       <p id="ch2intro_55">
       
  1340        Let us look at examples:
       
  1341       </p>
       
  1342       <pre class="programlisting">
       
  1343        &gt;&gt;&gt; t = True
   501 &gt;&gt;&gt; f = not t
  1344 &gt;&gt;&gt; f = not t
   502 &gt;&gt;&gt; print f
  1345 &gt;&gt;&gt; print f
   503 False
  1346 False
   504 &gt;&gt;&gt; f or t
  1347 &gt;&gt;&gt; f or t
   505 True
  1348 True
   506 &gt;&gt;&gt; f and t
  1349 &gt;&gt;&gt; f and t
   507 False</pre>
  1350 False
   508 </div>
  1351       </pre>
   509 </div>
  1352      </div>
   510 <div class="section" title="5. The while loop">
  1353     </div>
   511 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1354     <div class="section" title="5.The while loop">
   512 <a name="id2632223"></a>5. The <span class="strong"><strong>while</strong></span> loop</h2></div></div></div>
  1355      <div class="titlepage">
   513 <p><a name="ch2intro_56"></a>The Python <span class="strong"><strong>while</strong></span> loop is similar to the C/C++ while loop. The syntax is as
  1356       <div>
   514 follows:</p>
  1357        <div>
   515 <pre class="programlisting">statement 0
  1358         <h2 class="title" style="clear: both">
       
  1359          <a name="id2861325">
       
  1360          </a>
       
  1361          5.The
       
  1362          <span class="strong">
       
  1363           <strong>
       
  1364            while
       
  1365           </strong>
       
  1366          </span>
       
  1367          loop
       
  1368         </h2>
       
  1369        </div>
       
  1370       </div>
       
  1371      </div>
       
  1372      <p id="ch2intro_56">
       
  1373       The Python
       
  1374       <span class="strong">
       
  1375        <strong>
       
  1376         while
       
  1377        </strong>
       
  1378       </span>
       
  1379       loop is similar to the C/C++ while loop. The syntax is as
       
  1380 follows:
       
  1381      </p>
       
  1382      <pre class="programlisting">
       
  1383       statement 0
   516 while condition:
  1384 while condition:
   517   statement 1 #while block
  1385   statement 1 #while block
   518   statement 2 #while block
  1386   statement 2 #while block
   519 statement 3 #outside the while block.</pre>
  1387 statement 3 #outside the while block.
   520 <p><a name="ch2intro_57"></a>Let us look at an example:</p>
  1388      </pre>
   521 <pre class="programlisting">&gt;&gt;&gt; x = 1
  1389      <p id="ch2intro_57">
       
  1390       Let us look at an example:
       
  1391      </p>
       
  1392      <pre class="programlisting">
       
  1393       &gt;&gt;&gt; x = 1
   522 &gt;&gt;&gt; while x &lt;= 5:
  1394 &gt;&gt;&gt; while x &lt;= 5:
   523 ...   print x
  1395 ...   print x
   524 ...   x += 1
  1396 ...   x += 1
   525 ...
  1397 ...
   526 1
  1398 1
   527 2
  1399 2
   528 3
  1400 3
   529 4
  1401 4
   530 5</pre>
  1402 5
   531 </div>
  1403      </pre>
   532 <div class="section" title="6. The if conditional">
  1404     </div>
   533 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1405     <div class="section" title="6.The if conditional">
   534 <a name="id2632260"></a>6. The <span class="strong"><strong>if</strong></span> conditional</h2></div></div></div>
  1406      <div class="titlepage">
   535 <p><a name="ch2intro_58"></a>The Python <span class="strong"><strong>if</strong></span> block provides the conditional execution of statements.
  1407       <div>
       
  1408        <div>
       
  1409         <h2 class="title" style="clear: both">
       
  1410          <a name="id2861363">
       
  1411          </a>
       
  1412          6.The
       
  1413          <span class="strong">
       
  1414           <strong>
       
  1415            if
       
  1416           </strong>
       
  1417          </span>
       
  1418          conditional
       
  1419         </h2>
       
  1420        </div>
       
  1421       </div>
       
  1422      </div>
       
  1423      <p id="ch2intro_58">
       
  1424       The Python
       
  1425       <span class="strong">
       
  1426        <strong>
       
  1427         if
       
  1428        </strong>
       
  1429       </span>
       
  1430       block provides the conditional execution of statements.
   536 If the condition evaluates as true the block of statements defined under the if
  1431 If the condition evaluates as true the block of statements defined under the if
   537 block are executed.</p>
  1432 block are executed.
   538 <p><a name="ch2intro_59"></a>If the first block is not executed on account of the condition not being satisfied,
  1433      </p>
   539 the set of statements in the <span class="strong"><strong>else</strong></span> block are executed.</p>
  1434      <p id="ch2intro_59">
   540 <p><a name="ch2intro_5a"></a>The <span class="strong"><strong>elif</strong></span> block provides the functionality of evaluation of multiple conditions
  1435       If the first block is not executed on account of the condition not being satisfied,
   541 as shown in the example.</p>
  1436 the set of statements in the
   542 <p><a name="ch2intro_5b"></a>The syntax is as follows:</p>
  1437       <span class="strong">
   543 <pre class="programlisting">if condition :
  1438        <strong>
       
  1439         else
       
  1440        </strong>
       
  1441       </span>
       
  1442       block are executed.
       
  1443      </p>
       
  1444      <p id="ch2intro_5a">
       
  1445       The
       
  1446       <span class="strong">
       
  1447        <strong>
       
  1448         elif
       
  1449        </strong>
       
  1450       </span>
       
  1451       block provides the functionality of evaluation of multiple conditions
       
  1452 as shown in the example.
       
  1453      </p>
       
  1454      <p id="ch2intro_5b">
       
  1455       The syntax is as follows:
       
  1456      </p>
       
  1457      <pre class="programlisting">
       
  1458       if condition :
   544     statement_1
  1459     statement_1
   545     statement_2
  1460     statement_2
   546 
  1461 
   547 elif condition:
  1462 elif condition:
   548     statement_3
  1463     statement_3
   549     statement_4
  1464     statement_4
   550 else:
  1465 else:
   551     statement_5
  1466     statement_5
   552     statement_6</pre>
  1467     statement_6
   553 <p><a name="ch2intro_5c"></a>Let us look at an example:</p>
  1468      </pre>
   554 <pre class="programlisting">&gt;&gt;&gt; n = raw_input("Input a number:")
  1469      <p id="ch2intro_5c">
       
  1470       Let us look at an example:
       
  1471      </p>
       
  1472      <pre class="programlisting">
       
  1473       &gt;&gt;&gt; n = raw_input("Input a number:")
   555 &gt;&gt;&gt; if n &lt; 0:
  1474 &gt;&gt;&gt; if n &lt; 0:
   556       print n," is negative"
  1475       print n," is negative"
   557       elif n &gt; 0:
  1476       elif n &gt; 0:
   558       print n," is positive"
  1477       print n," is positive"
   559       else:
  1478       else:
   560       print n, " is 0"</pre>
  1479       print n, " is 0"
   561 </div>
  1480      </pre>
   562 <div class="section" title="7. raw_input()">
  1481     </div>
   563 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1482     <div class="section" title="7.raw_input()">
   564 <a name="id2632330"></a>7. <span class="strong"><strong>raw_input()</strong></span>
  1483      <div class="titlepage">
   565 </h2></div></div></div>
  1484       <div>
   566 <p><a name="ch2intro_5d"></a>In the previous example we saw the call to the raw_input() subroutine.
  1485        <div>
   567 The <span class="strong"><strong>raw_input()</strong></span> method is used to take user inputs through the console.
  1486         <h2 class="title" style="clear: both">
   568 Unlike <span class="strong"><strong>input()</strong></span> which assumes the data entered by the user as a standard python
  1487          <a name="id2861432">
   569 expression, <span class="strong"><strong>raw_input()</strong></span> treats all the input data as raw data and converts
  1488          </a>
   570 everything into a string. To illustrate this let us look at an example.</p>
  1489          7.
   571 <pre class="programlisting">&gt;&gt;&gt; input("Enter a number thats a palindrome:")
  1490          <span class="strong">
       
  1491           <strong>
       
  1492            raw_input()
       
  1493           </strong>
       
  1494          </span>
       
  1495         </h2>
       
  1496        </div>
       
  1497       </div>
       
  1498      </div>
       
  1499      <p id="ch2intro_5d">
       
  1500       In the previous example we saw the call to the raw_input() subroutine.
       
  1501 The
       
  1502       <span class="strong">
       
  1503        <strong>
       
  1504         raw_input()
       
  1505        </strong>
       
  1506       </span>
       
  1507       method is used to take user inputs through the console.
       
  1508 Unlike
       
  1509       <span class="strong">
       
  1510        <strong>
       
  1511         input()
       
  1512        </strong>
       
  1513       </span>
       
  1514       which assumes the data entered by the user as a standard python
       
  1515 expression,
       
  1516       <span class="strong">
       
  1517        <strong>
       
  1518         raw_input()
       
  1519        </strong>
       
  1520       </span>
       
  1521       treats all the input data as raw data and converts
       
  1522 everything into a string. To illustrate this let us look at an example.
       
  1523      </p>
       
  1524      <pre class="programlisting">
       
  1525       &gt;&gt;&gt; input("Enter a number thats a palindrome:")
   572 Enter a number thats a palindrome:121
  1526 Enter a number thats a palindrome:121
   573 121
  1527 121
   574 
  1528 
   575 &gt;&gt;&gt; input("Enter your name:")
  1529 &gt;&gt;&gt; input("Enter your name:")
   576 Enter your name:PythonFreak
  1530 Enter your name:PythonFreak
   577 Traceback (most recent call last):
  1531 Traceback (most recent call last):
   578   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  1532   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
   579   File "&lt;string&gt;", line 1, in &lt;module&gt;
  1533   File "&lt;string&gt;", line 1, in &lt;module&gt;
   580 NameError: name 'PythonFreak' is not defined</pre>
  1534 NameError: name 'PythonFreak' is not defined
   581 <p><a name="ch2intro_5e"></a>As shown above the <span class="strong"><strong>input()</strong></span> assumes that the data entered is a valid Python
  1535      </pre>
       
  1536      <p id="ch2intro_5e">
       
  1537       As shown above the
       
  1538       <span class="strong">
       
  1539        <strong>
       
  1540         input()
       
  1541        </strong>
       
  1542       </span>
       
  1543       assumes that the data entered is a valid Python
   582 expression. In the first call it prompts for an integer input and when entered
  1544 expression. In the first call it prompts for an integer input and when entered
   583 it accepts the integer as an integer, whereas in the second call, when the string
  1545 it accepts the integer as an integer, whereas in the second call, when the string
   584 is entered without the quotes, <span class="strong"><strong>input()</strong></span> assumes that the entered data is a valid
  1546 is entered without the quotes,
       
  1547       <span class="strong">
       
  1548        <strong>
       
  1549         input()
       
  1550        </strong>
       
  1551       </span>
       
  1552       assumes that the entered data is a valid
   585 Python expression and hence it raises and exception saying PythonFreak is not
  1553 Python expression and hence it raises and exception saying PythonFreak is not
   586 defined.</p>
  1554 defined.
   587 <pre class="programlisting">&gt;&gt;&gt; input("Enter your name:")
  1555      </p>
       
  1556      <pre class="programlisting">
       
  1557       &gt;&gt;&gt; input("Enter your name:")
   588 Enter your name:'PythonFreak'
  1558 Enter your name:'PythonFreak'
   589 'PythonFreak'
  1559 'PythonFreak'
   590 &gt;&gt;&gt;</pre>
  1560 &gt;&gt;&gt;
   591 <p><a name="ch2intro_5f"></a>Here the name is accepted because its entered as a string (within quotes). But
  1561      </pre>
       
  1562      <p id="ch2intro_5f">
       
  1563       Here the name is accepted because its entered as a string (within quotes). But
   592 its unreasonable to go on using quotes each time a string is entered. Hence the
  1564 its unreasonable to go on using quotes each time a string is entered. Hence the
   593 alternative is to use <span class="strong"><strong>raw_input()</strong></span>.</p>
  1565 alternative is to use
   594 <p><a name="ch2intro_60"></a>Let us now look at how <span class="strong"><strong>raw_input()</strong></span> operates with an example.</p>
  1566       <span class="strong">
   595 <pre class="programlisting">&gt;&gt;&gt; raw_input("Enter your name:")
  1567        <strong>
       
  1568         raw_input()
       
  1569        </strong>
       
  1570       </span>
       
  1571       .
       
  1572      </p>
       
  1573      <p id="ch2intro_60">
       
  1574       Let us now look at how
       
  1575       <span class="strong">
       
  1576        <strong>
       
  1577         raw_input()
       
  1578        </strong>
       
  1579       </span>
       
  1580       operates with an example.
       
  1581      </p>
       
  1582      <pre class="programlisting">
       
  1583       &gt;&gt;&gt; raw_input("Enter your name:")
   596 Enter your name:PythonFreak
  1584 Enter your name:PythonFreak
   597 'PythonFreak'</pre>
  1585 'PythonFreak'
   598 <p><a name="ch2intro_61"></a>Observe that the <span class="strong"><strong>raw_input()</strong></span> is converting it into a string all by itself.</p>
  1586      </pre>
   599 <pre class="programlisting">&gt;&gt;&gt; pal = raw_input("Enter a number thats a palindrome:")
  1587      <p id="ch2intro_61">
       
  1588       Observe that the
       
  1589       <span class="strong">
       
  1590        <strong>
       
  1591         raw_input()
       
  1592        </strong>
       
  1593       </span>
       
  1594       is converting it into a string all by itself.
       
  1595      </p>
       
  1596      <pre class="programlisting">
       
  1597       &gt;&gt;&gt; pal = raw_input("Enter a number thats a palindrome:")
   600 Enter a number thats a palindrome:121
  1598 Enter a number thats a palindrome:121
   601 '121'</pre>
  1599 '121'
   602 <p><a name="ch2intro_62"></a>Observe that <span class="strong"><strong>raw_input()</strong></span> is converting the integer 121 also to a string as
  1600      </pre>
   603 '121'. Let us look at another example:</p>
  1601      <p id="ch2intro_62">
   604 <pre class="programlisting">&gt;&gt;&gt; pal = raw_input("Enter a number thats a palindrome:")
  1602       Observe that
       
  1603       <span class="strong">
       
  1604        <strong>
       
  1605         raw_input()
       
  1606        </strong>
       
  1607       </span>
       
  1608       is converting the integer 121 also to a string as
       
  1609 '121'. Let us look at another example:
       
  1610      </p>
       
  1611      <pre class="programlisting">
       
  1612       &gt;&gt;&gt; pal = raw_input("Enter a number thats a palindrome:")
   605 Enter a number thats a palindrome:121
  1613 Enter a number thats a palindrome:121
   606 &gt;&gt;&gt; pal + 2
  1614 &gt;&gt;&gt; pal + 2
   607 Traceback (most recent call last):
  1615 Traceback (most recent call last):
   608   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  1616   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
   609 TypeError: cannot concatenate 'str' and 'int' objects
  1617 TypeError: cannot concatenate 'str' and 'int' objects
   610 &gt;&gt;&gt; pal
  1618 &gt;&gt;&gt; pal
   611 '121'</pre>
  1619 '121'
   612 <p><a name="ch2intro_63"></a>Observe here that the variable <span class="emphasis"><em>pal</em></span> is a string and hence integer operations
  1620      </pre>
   613 cannot be performed on it. Hence the exception is raised.</p>
  1621      <p id="ch2intro_63">
   614 </div>
  1622       Observe here that the variable
   615 <div class="section" title="8. int() method">
  1623       <span class="emphasis">
   616 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  1624        <em>
   617 <a name="id2632484"></a>8. <span class="strong"><strong>int()</strong></span> method</h2></div></div></div>
  1625         pal
   618 <p><a name="ch2intro_64"></a>Generally for computing purposes, the data used is not strings or raw data but
  1626        </em>
       
  1627       </span>
       
  1628       is a string and hence integer operations
       
  1629 cannot be performed on it. Hence the exception is raised.
       
  1630      </p>
       
  1631     </div>
       
  1632     <div class="section" title="8.int() method">
       
  1633      <div class="titlepage">
       
  1634       <div>
       
  1635        <div>
       
  1636         <h2 class="title" style="clear: both">
       
  1637          <a name="id2861586">
       
  1638          </a>
       
  1639          8.
       
  1640          <span class="strong">
       
  1641           <strong>
       
  1642            int()
       
  1643           </strong>
       
  1644          </span>
       
  1645          method
       
  1646         </h2>
       
  1647        </div>
       
  1648       </div>
       
  1649      </div>
       
  1650      <p id="ch2intro_64">
       
  1651       Generally for computing purposes, the data used is not strings or raw data but
   619 on integers, floats and similar mathematical data structures. The data obtained
  1652 on integers, floats and similar mathematical data structures. The data obtained
   620 from <span class="strong"><strong>raw_input()</strong></span> is raw data in the form of strings. In order to obtain integers
  1653 from
   621 from strings we use the method <span class="strong"><strong>int()</strong></span>.</p>
  1654       <span class="strong">
   622 <p><a name="ch2intro_65"></a>Let us look at an example.</p>
  1655        <strong>
   623 <pre class="programlisting">&gt;&gt;&gt; intpal = int(pal)
  1656         raw_input()
       
  1657        </strong>
       
  1658       </span>
       
  1659       is raw data in the form of strings. In order to obtain integers
       
  1660 from strings we use the method
       
  1661       <span class="strong">
       
  1662        <strong>
       
  1663         int()
       
  1664        </strong>
       
  1665       </span>
       
  1666       .
       
  1667      </p>
       
  1668      <p id="ch2intro_65">
       
  1669       Let us look at an example.
       
  1670      </p>
       
  1671      <pre class="programlisting">
       
  1672       &gt;&gt;&gt; intpal = int(pal)
   624 &gt;&gt;&gt; intpal
  1673 &gt;&gt;&gt; intpal
   625 121</pre>
  1674 121
   626 <p><a name="ch2intro_66"></a>In the previous example it was observed that <span class="emphasis"><em>pal</em></span> was a string variable. Here
  1675      </pre>
   627 using the <span class="strong"><strong>int()</strong></span> method the string <span class="emphasis"><em>pal</em></span> was converted to an integer variable.</p>
  1676      <p id="ch2intro_66">
   628 <p><a name="ch2intro_67"></a><span class="emphasis"><em>Try This Yourself:</em></span></p>
  1677       In the previous example it was observed that
   629 <pre class="programlisting">&gt;&gt;&gt; stringvar = raw_input("Enter a name:")
  1678       <span class="emphasis">
       
  1679        <em>
       
  1680         pal
       
  1681        </em>
       
  1682       </span>
       
  1683       was a string variable. Here
       
  1684 using the
       
  1685       <span class="strong">
       
  1686        <strong>
       
  1687         int()
       
  1688        </strong>
       
  1689       </span>
       
  1690       method the string
       
  1691       <span class="emphasis">
       
  1692        <em>
       
  1693         pal
       
  1694        </em>
       
  1695       </span>
       
  1696       was converted to an integer variable.
       
  1697      </p>
       
  1698      <p id="ch2intro_67">
       
  1699       <span class="emphasis">
       
  1700        <em>
       
  1701         Try This Yourself:
       
  1702        </em>
       
  1703       </span>
       
  1704      </p>
       
  1705      <pre class="programlisting">
       
  1706       &gt;&gt;&gt; stringvar = raw_input("Enter a name:")
   630 Enter a name:Guido Van Rossum
  1707 Enter a name:Guido Van Rossum
   631 &gt;&gt;&gt; stringvar
  1708 &gt;&gt;&gt; stringvar
   632 'Guido Van Rossum'
  1709 'Guido Van Rossum'
   633 &gt;&gt;&gt; numvar = int(stringvar)</pre>
  1710 &gt;&gt;&gt; numvar = int(stringvar)
   634 </div>
  1711      </pre>
   635 </div>
  1712     </div>
   636 </div></body>
  1713    </div>
       
  1714   </div>
       
  1715  </body>
   637 </html>
  1716 </html>