web/html/backup/ch01-intro.html
author amit@thunder
Mon, 25 Jan 2010 18:56:45 +0530
changeset 0 8083d21c0020
permissions -rwxr-xr-x
The first commit of all the required files for the review app
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     1
<html>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     2
<head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     3
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     4
<title>Basic Python</title>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     5
<link rel="stylesheet" href="/review/support/styles.css" type="text/css">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     6
<meta name="generator" content="DocBook XSL Stylesheets V1.74.3">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     7
<link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     8
<script type="text/javascript" src="/review/support/jquery-min.js"></script>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
     9
<script type="text/javascript" src="/review/support/form.js"></script>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    10
<script type="text/javascript" src="/review/support/hsbook.js"></script>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    11
</head>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    12
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    13
<!--<div class="navheader">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    14
<table width="100%" summary="Navigation header">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    15
<tr><th colspan="3" align="center">Basic Python</th></tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    16
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    17
<td width="20%" align="left">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    18
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    19
<th width="60%" align="center"> </th>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    20
<td width="20%" align="right"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    21
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    22
</table>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    23
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    24
</div>  -->
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    25
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    26
<div class="chapter" lang="en" id="chap_intro">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    27
<div class="article" title="Basic Python">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    28
<div class="titlepage">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    29
<div><div><h2 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    30
<a name="id2988867"></a>Basic Python</h2></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    31
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    32
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    33
<div class="toc">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    34
<p><b>Table of Contents</b></p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    35
<dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    36
<dt><span class="section"><a href="#introduction">1. Introduction</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    37
<dt><span class="section"><a href="#the-python-interpreter">2. The Python Interpreter</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    38
<dd><dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    39
<dt><span class="section"><a href="#the-interactive-interpreter">2.1. The Interactive Interpreter</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    40
<dt><span class="section"><a href="#ipython-an-enhanced-interactive-python-interpreter">2.2. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    41
        <span class="emphasis"><em>ipython</em></span> - An enhanced interactive Python interpreter
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    42
      </a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    43
</dl></dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    44
<dt><span class="section"><a href="#editing-and-running-a-python-file">3. Editing and running a python file</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    45
<dt><span class="section"><a href="#basic-datatypes-and-operators-in-python">4. Basic Datatypes and operators in Python</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    46
<dd><dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    47
<dt><span class="section"><a href="#numbers">4.1. Numbers</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    48
<dt><span class="section"><a href="#variables">4.2. Variables</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    49
<dt><span class="section"><a href="#strings">4.3. Strings</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    50
<dt><span class="section"><a href="#boolean">4.4. Boolean</a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    51
</dl></dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    52
<dt><span class="section"><a href="#the-while-loop">5. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    53
      <span class="strong"><strong>while</strong></span> loop
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    54
    </a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    55
<dt><span class="section"><a href="#the-if-conditional">6. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    56
      <span class="strong"><strong>if</strong></span> conditional
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    57
    </a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    58
<dt><span class="section"><a href="#raw_input">7.       <span class="strong"><strong>raw_input()</strong></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    59
    </a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    60
<dt><span class="section"><a href="#int-method">8. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    61
      <span class="strong"><strong>int()</strong></span> method
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    62
    </a></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    63
</dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    64
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    65
<p id="x_38"></a>This document is intended to be handed out at the end of the workshop. It has been designed for Engineering students who are Python beginners and have basic programming skills. The focus is on basic numerics and plotting using Python.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    66
<p id="x_39"></a>The system requirements:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    67
<div class="itemizedlist"><ul class="itemizedlist" type="*">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    68
<li class="listitem" style="list-style-type: *"><p id="x_3a"></a>Python - version 2.5.x or newer.</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    69
<li class="listitem" style="list-style-type: *"><p id="x_3b"></a>IPython</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    70
<li class="listitem" style="list-style-type: *"><p id="x_3c"></a>Text editor - scite, vim, emacs or whatever you are comfortable with.</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    71
</ul></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    72
<div class="section" title="1. Introduction">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    73
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    74
<a name="introduction"></a>1. Introduction</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    75
<p id="x_3d"></a>The Python programming language was created by a dutch named Guido van Rossum. The idea of Python was conceived in December 1989. The name Python has nothing to do with the reptilian, but its been named after the 70s comedy series "Monty Python's Flying Circus", since it happens to be Guido's favourite TV series.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    76
<p id="x_3e"></a>Current stable version of Python is 2.6.x, although Python 3.0 is also the stable version, it is not backwards compatible with the previous versions and is hence not entirely popular at the moment. This material will focus on the 2.6.x series.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    77
<p id="x_3f"></a>Python is licensed under the Python Software Foundation License (PSF License) which is GPL compatible Free Software license (excepting license version 1.6 and 2.0) It is a no strings attached license, which means the source code is free to modify and redistribute.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    78
<p id="x_40"></a>The Python docs define Python as "Python is an interpreted, object-oriented, high-level programming language with dynamic semantics." A more detailed summary can be found at 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    79
      <a class="ulink" href="http://www.python.org/doc/essays/blurb.html" target="_top">http://www.python.org/doc/essays/blurb.html</a>. Python is a language that has been designed to help the programmer concentrate on solving the problem at hand and not worry about the programming language idiosyncrasies.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    80
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    81
<p id="x_41"></a>Python is a highly cross platform compatible language on account of it being an interpreted language. It is highly scalable and hence has been adapted to run on the Nokia 60 series phones. Python has been designed to be readable and easy to use</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    82
<p id="x_42"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    83
      <span class="strong"><strong>Resources available for reference</strong></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    84
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    85
<div class="itemizedlist"><ul class="itemizedlist" type="*">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    86
<li class="listitem" style="list-style-type: *"><p id="x_43"></a>Web: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    87
          <a class="ulink" href="http://www.python.org" target="_top">http://www.python.org</a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    88
        </p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    89
<li class="listitem" style="list-style-type: *"><p id="x_44"></a>Doc: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    90
          <a class="ulink" href="http://www.python.org/doc" target="_top">http://www.python.org/doc</a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    91
        </p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    92
<li class="listitem" style="list-style-type: *"><p id="x_45"></a>Free Tutorials: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    93
          <span class="emphasis"><em> Official Python Tutorial: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    94
            <a class="ulink" href="http://docs.python.org/tut/tut.html" target="_top">http://docs.python.org/tut/tut.html</a> 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    95
          </em></span> Byte of Python: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    96
          <a class="ulink" href="http://www.byteofpython.info" target="_top">http://www.byteofpython.info</a>/ * Dive into Python: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    97
          <a class="ulink" href="http://diveintopython.org" target="_top">http://diveintopython.org</a>/
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    98
        </p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
    99
</ul></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   100
<p id="x_46"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   101
      <span class="strong"><strong>Advantages of Python - Why Python??</strong></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   102
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   103
<div class="itemizedlist"><ul class="itemizedlist" type="*">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   104
<li class="listitem" style="list-style-type: *"><p id="x_47"></a>Python has been designed for readability and ease of use. Its been designed in  such a fashion that it imposes readability on the programmer. Python does away with the braces and the semicolons and instead implements code blocks based on indentation, thus enhancing readability.</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   105
<li class="listitem" style="list-style-type: *"><p id="x_48"></a>Python is a high level, interpreted, modular and object oriented language. Python performs memory management on its own, thus the programmer need not bother about allocating and deallocating memory to variables. Python provides extensibility by providing modules which can be easily imported similar to headers in C and packages in Java. Python is object oriented and hence provides all the object oriented characteristics such as inheritance, encapsulation and polymorphism.</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   106
<li class="listitem" style="list-style-type: *"><p id="x_49"></a>Python offers a highly powerful interactive programming interface in the form of the 'Interactive Interpreter' which will be discussed in more detail in the following sections.</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   107
<li class="listitem" style="list-style-type: *"><p id="x_4a"></a>Python provides a rich standard library and an extensive set of modules. The  power of Python modules can be seen in this slightly exaggerated cartoon 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   108
          <a class="ulink" href="http://xkcd.com/353" target="_top">http://xkcd.com/353</a>/
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   109
        </p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   110
<li class="listitem" style="list-style-type: *"><p id="x_4b"></a>Python interfaces well with most other programming languages such as C, C++  and FORTRAN.</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   111
</ul></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   112
<p id="x_4c"></a>Although, Python has one setback. Python is not fast as some of the compiled languages like C or C++. Yet, the amount of flexibility and power more than make up for this setback.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   113
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   114
<div class="section" title="2. The Python Interpreter">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   115
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   116
<a name="the-python-interpreter"></a>2. The Python Interpreter</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   117
<div class="section" title="2.1. The Interactive Interpreter">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   118
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   119
<a name="the-interactive-interpreter"></a>2.1. The Interactive Interpreter</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   120
<p id="x_4d"></a>Typing 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   121
        <span class="emphasis"><em>python</em></span> at the shell prompt on any standard Unix/Gnu-Linux system and hitting the enter key fires up the Python 'Interactive Interpreter'. The Python interpreter is one of the most integral features of Python. The prompt obtained when the interactive interpreter is similar to what is shown below. The exact appearance might differ based on the version of Python being used. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   122
        <code class="literal">&gt;&gt;&gt;</code> thing shown is the python prompt. When something is typed at the prompt and the enter key is hit, the python interpreter interprets the command entered and performs the appropriate action. All the examples presented in this document are to be tried hands on, on the interactive interpreter.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   123
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   124
<pre class="programlisting">Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   125
[GCC 4.3.2] on linux2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   126
Type "help", "copyright", "credits" or "license" for more information.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   127
&gt;&gt;&gt; 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   128
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   129
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   130
<p id="x_4e"></a>Lets try with an example, type 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   131
        <code class="literal">print 'Hello, World!'</code> at the prompt and hit the enter key.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   132
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   133
<pre class="programlisting">&gt;&gt;&gt; print 'Hello, World!'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   134
Hello, World!
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   135
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   136
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   137
<p id="x_4f"></a>This example was quite straight forward, and thus we have written our first line of Python code. Now let us try typing something arbitrary at the prompt. For example:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   138
<pre class="programlisting">&gt;&gt;&gt; arbit word
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   139
  File "&lt;stdin&gt;", line 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   140
    arbit word
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   141
            ^
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   142
SyntaxError: invalid syntax
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   143
&gt;&gt;&gt;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   144
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   145
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   146
<p id="x_50"></a>The interpreter gave an error message saying that 'arbit word' was invalid syntax which is valid. The interpreter is an amazing tool when learning to program in Python. The interpreter provides a help function that provides the necessary documentation regarding all Python syntax, constructs, modules and objects. Typing 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   147
        <span class="emphasis"><em>help()</em></span> at the prompt gives the following output:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   148
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   149
<pre class="programlisting">&gt;&gt;&gt; help()
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   150
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   151
Welcome to Python 2.5!  This is the online help utility.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   152
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   153
If this is your first time using Python, you should definitely check out
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   154
the tutorial on the Internet at http://www.python.org/doc/tut/.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   155
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   156
Enter the name of any module, keyword, or topic to get help on writing
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   157
Python programs and using Python modules.  To quit this help utility and
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   158
return to the interpreter, just type "quit".
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   159
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   160
To get a list of available modules, keywords, or topics, type "modules",
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   161
"keywords", or "topics".  Each module also comes with a one-line summary
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   162
of what it does; to list the modules whose summaries contain a given word
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   163
such as "spam", type "modules spam".
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   164
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   165
help&gt; 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   166
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   167
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   168
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   169
<p id="x_51"></a>As mentioned in the output, entering the name of any module, keyword or topic will provide the documentation and help regarding the same through the online help utility. Pressing 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   170
        <span class="emphasis"><em>Ctrl+d</em></span> exits the help prompt and returns to the python prompt.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   171
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   172
<p id="x_52"></a>Let us now try a few examples at the python interpreter.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   173
<p id="x_53"></a>Eg 1:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   174
<pre class="programlisting">&gt;&gt;&gt; print 'Hello, python!'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   175
Hello, python!
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   176
&gt;&gt;&gt;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   177
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   178
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   179
<p id="x_54"></a>Eg 2:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   180
<pre class="programlisting">&gt;&gt;&gt; print 4321*567890
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   181
2453852690
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   182
&gt;&gt;&gt; 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   183
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   184
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   185
<p id="x_55"></a>Eg 3:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   186
<pre class="programlisting">&gt;&gt;&gt; 4321*567890
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   187
2453852690L
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   188
&gt;&gt;&gt;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   189
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   190
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   191
<pre class="programlisting">Note: Notice the 'L' at the end of the output. The 'L' signifies that the
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   192
output of the operation is of type *long*. It was absent in the previous
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   193
example because we used the print statement. This is because *print* formats
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   194
the output before displaying.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   195
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   196
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   197
<p id="x_56"></a>Eg 4:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   198
<pre class="programlisting">&gt;&gt;&gt; big = 12345678901234567890 ** 3
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   199
&gt;&gt;&gt; print big
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   200
1881676372353657772490265749424677022198701224860897069000
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   201
&gt;&gt;&gt; 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   202
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   203
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   204
<pre class="programlisting">This example is to show that unlike in C or C++ there is no limit on the
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   205
value of an integer.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   206
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   207
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   208
<p id="x_57"></a>Try this on the interactive interpreter: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   209
        <code class="literal">import this</code>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   210
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   211
<p id="x_58"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   212
        <span class="emphasis"><em>Hint: The output gives an idea of Power of Python</em></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   213
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   214
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   215
<div class="section" title="2.2.  ipython - An enhanced interactive Python interpreter">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   216
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   217
<a name="ipython-an-enhanced-interactive-python-interpreter"></a>2.2. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   218
        <span class="emphasis"><em>ipython</em></span> - An enhanced interactive Python interpreter
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   219
      </h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   220
<p id="x_59"></a>The power and the importance of the interactive interpreter was the highlight of the previous section. This section provides insight into the enhanced interpreter with more advanced set of features called 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   221
        <span class="strong"><strong>ipython</strong></span>. Entering 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   222
        <span class="emphasis"><em>ipython</em></span> at the shell prompt fires up the interactive interpreter.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   223
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   224
<pre class="programlisting">$ ipython
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   225
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   226
Type "copyright", "credits" or "license" for more information.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   227
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   228
IPython 0.8.4 -- An enhanced Interactive Python.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   229
?         -&gt; Introduction and overview of IPython's features.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   230
%quickref -&gt; Quick reference.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   231
help      -&gt; Python's own help system.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   232
object?   -&gt; Details about 'object'. ?object also works, ?? prints more.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   233
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   234
In [1]: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   235
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   236
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   237
<p id="x_5a"></a>This is the output obtained upon firing ipython. The exact appearance may change based on the Python version installed. The following are some of the various features provided by 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   238
        <span class="strong"><strong>ipython</strong></span>:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   239
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   240
<div class="variablelist"><dl>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   241
<dt><span class="term"></span></dt>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   242
<dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   243
<p></p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   244
<p id="x_5b"></a>Suggestions - ipython provides suggestions of the possible methods and operations available for the given python object.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   245
</dd>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   246
</dl></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   247
<p id="x_5c"></a>Eg 5:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   248
<pre class="programlisting">In [4]: a = 6
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   249
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   250
In [5]: a.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   251
a.__abs__           a.__divmod__        a.__index__         a.__neg__          a.__rand__          a.__rmod__          a.__rxor__
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   252
a.__add__           a.__doc__           a.__init__          a.__new__          a.__rdiv__          a.__rmul__          a.__setattr__
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   253
a.__and__           a.__float__         a.__int__           a.__nonzero__      a.__rdivmod__       a.__ror__           a.__str__
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   254
a.__class__         a.__floordiv__      a.__invert__        a.__oct__          a.__reduce__        a.__rpow__          a.__sub__
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   255
a.__cmp__           a.__getattribute__  a.__long__          a.__or__           a.__reduce_ex__     a.__rrshift__       a.__truediv__
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   256
a.__coerce__        a.__getnewargs__    a.__lshift__        a.__pos__          a.__repr__          a.__rshift__        a.__xor__
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   257
a.__delattr__       a.__hash__          a.__mod__           a.__pow__          a.__rfloordiv__     a.__rsub__          
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   258
a.__div__           a.__hex__           a.__mul__           a.__radd__         a.__rlshift__       a.__rtruediv__      
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   259
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   260
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   261
<p id="x_5d"></a>In this example, we initialized 'a' (a variable - a concept that will be discussed in the subsequent sections.) to 6. In the next line when the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   262
        <span class="emphasis"><em>tab</em></span> key is pressed after typing '
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   263
        <span class="emphasis"><em>a.</em></span>' ipython displays the set of all possible methods that are applicable on the object 'a' (an integer in this context). Ipython provides many such datatype specific features which will be presented in the further sections as and when the datatypes are introduced.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   264
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   265
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   266
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   267
<div class="section" title="3. Editing and running a python file">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   268
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   269
<a name="editing-and-running-a-python-file"></a>3. Editing and running a python file</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   270
<p id="x_5e"></a>The previous sections focused on the use of the interpreter to run python code. While the interpeter is an excellent tool to test simple solutions and experiment with small code snippets, its main disadvantage is that everything written in the interpreter is lost once its quit. Most of the times a program is used by people other than the author. So the programs have to be available in some form suitable for distribution, and hence they are written in files. This section will focus on editing and running python files. Start by opening a text editor ( it is recommended you choose one from the list at the top of this page ). In the editor type down python code and save the file with an extension 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   271
      <span class="strong"><strong>.py</strong></span> (python files have an extension of .py). Once done with the editing, save the file and exit the editor.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   272
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   273
<p id="x_5f"></a>Let us look at a simple example of calculating the gcd of 2 numbers using Python:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   274
<p id="x_60"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   275
      <span class="strong"><strong>Creating the first python script(file)</strong></span> :
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   276
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   277
<pre class="programlisting">$ emacs gcd.py
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   278
  def gcd(x,y):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   279
    if x % y == 0:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   280
      return y
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   281
    return gcd(y, x%y)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   282
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   283
  print gcd(72, 92)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   284
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   285
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   286
<p id="x_61"></a>To run the script, open the shell prompt, navigate to the directory that contains the python file and run 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   287
      <code class="literal">python &lt;filename.py&gt;</code> at the prompt ( in this case filename is gcd.py )
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   288
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   289
<p id="x_62"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   290
      <span class="strong"><strong>Running the python script</strong></span> :
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   291
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   292
<pre class="programlisting">$ python gcd.py
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   293
4
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   294
$ 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   295
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   296
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   297
<p id="x_63"></a>Another method to run a python script would be to include the line</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   298
<p id="x_64"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   299
      <code class="literal">#! /usr/bin/python</code>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   300
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   301
<p id="x_65"></a>at the beginning of the python file and then make the file executable by</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   302
<p id="x_66"></a>$ chmod a+x 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   303
      <span class="emphasis"><em>filename.py</em></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   304
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   305
<p id="x_67"></a>Once this is done, the script can be run as a standalone program as follows:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   306
<p id="x_68"></a>$ ./
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   307
      <span class="emphasis"><em>filename.py</em></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   308
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   309
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   310
<div class="section" title="4. Basic Datatypes and operators in Python">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   311
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   312
<a name="basic-datatypes-and-operators-in-python"></a>4. Basic Datatypes and operators in Python</h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   313
<p id="x_69"></a>Python provides the following set of basic datatypes.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   314
<div class="blockquote"><blockquote class="blockquote"><div class="itemizedlist"><ul class="itemizedlist" type="*">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   315
<li class="listitem" style="list-style-type: *"><p id="x_6a"></a>Numbers: int, float, long, complex</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   316
<li class="listitem" style="list-style-type: *"><p id="x_6b"></a>Strings</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   317
<li class="listitem" style="list-style-type: *"><p id="x_6c"></a>Boolean</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   318
</ul></div></blockquote></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   319
<div class="section" title="4.1. Numbers">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   320
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   321
<a name="numbers"></a>4.1. Numbers</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   322
<p id="x_6d"></a>Numbers were introduced in the examples presented in the interactive interpreter section. Numbers include types as mentioned earlier viz., int (integers), float (floating point numbers), long (large integers), complex (complex numbers with real and imaginary parts). Python is not a strongly typed language, which means the type of a variable need not mentioned during its initialization. Let us look at a few examples.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   323
<p id="x_6e"></a>Eg 6:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   324
<pre class="programlisting">&gt;&gt;&gt; a = 1 #here a is an integer variable
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   325
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   326
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   327
<p id="x_6f"></a>Eg 7:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   328
<pre class="programlisting">&gt;&gt;&gt; lng = 122333444455555666666777777788888888999999999 #here lng is a variable of type long
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   329
&gt;&gt;&gt; lng
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   330
122333444455555666666777777788888888999999999L #notice the trailing 'L'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   331
&gt;&gt;&gt; print lng
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   332
122333444455555666666777777788888888999999999 #notice the absence of the trailing 'L'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   333
&gt;&gt;&gt; lng+1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   334
122333444455555666666777777788888889000000000L
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   335
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   336
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   337
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   338
<p id="x_70"></a>Long numbers are the same as integers in almost all aspects. They can be used in operations just like integers and along with integers without any distinction. The only distinction comes during type checking (which is not a healthy practice). Long numbers are tucked with a trailing 'L' just to signify that they are long. Notice that in the example just lng at the prompt displays the value of the variable with the 'L' whereas 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   339
        <code class="literal">print lng</code> displays without the 'L'. This is because print formats the output before printing. Also in the example, notice that adding an integer to a long does not give any errors and the result is as expected. So for all practical purposes longs can be treated as ints.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   340
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   341
<p id="x_71"></a>Eg 8:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   342
<pre class="programlisting">&gt;&gt;&gt; fl = 3.14159 #fl is a float variable
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   343
&gt;&gt;&gt; e = 1.234e-4 #e is also a float variable, specified in the exponential form
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   344
&gt;&gt;&gt; a = 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   345
&gt;&gt;&gt; b = 2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   346
&gt;&gt;&gt; a/b #integer division
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   347
0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   348
&gt;&gt;&gt; a/fl #floating point division
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   349
0.31831015504887655
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   350
&gt;&gt;&gt; e/fl
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   351
3.9279473133031364e-05
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   352
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   353
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   354
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   355
<p id="x_72"></a>Floating point numbers, simply called floats are real numbers with a decimal point. The example above shows the initialization of a float variable. Shown also in this example is the difference between integer division and floating point division. 'a' and 'b' here are integer variables and hence the division gives 0 as the quotient. When either of the operands is a float, the operation is a floating point division, and the result is also a float as illustrated.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   356
<p id="x_73"></a>Eg 9:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   357
<pre class="programlisting">&gt;&gt;&gt; cplx = 3 + 4j #cplx is a complex variable
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   358
&gt;&gt;&gt; cplx
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   359
(3+4j)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   360
&gt;&gt;&gt; print cplx.real #prints the real part of the complex number
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   361
3.0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   362
&gt;&gt;&gt; print cplx.imag #prints the imaginary part of the complex number
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   363
4.0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   364
&gt;&gt;&gt; print cplx*fl  #multiplies the real and imag parts of the complex number with the multiplier
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   365
(9.42477+12.56636j)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   366
&gt;&gt;&gt; abs(cplx) #returns the absolute value of the complex number
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   367
5.0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   368
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   369
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   370
<p id="x_74"></a>Python provides a datatype for complex numbers. Complex numbers are initialized as shown in the example above. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   371
        <span class="emphasis"><em>real</em></span> and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   372
        <span class="emphasis"><em>imag</em></span> operators return the real and imaginary parts of the complex number as shown. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   373
        <span class="emphasis"><em>abs()</em></span> returns the absolute value of the complex number.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   374
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   375
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   376
<div class="section" title="4.2. Variables">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   377
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   378
<a name="variables"></a>4.2. Variables</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   379
<p id="x_75"></a>Variables are just names that represent a value. Variables have already been introduced in the various examples from the previous sections. Certain rules about using variables:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   380
<div class="blockquote"><blockquote class="blockquote"><div class="itemizedlist"><ul class="itemizedlist" type="*">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   381
<li class="listitem" style="list-style-type: *"><p id="x_76"></a>Variables have to be initialized or assigned a value before being used.</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   382
<li class="listitem" style="list-style-type: *"><p id="x_77"></a>Variable names can consist of letters, digits and 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   383
              <a class="link" href="">underscores</a> .
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   384
            </p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   385
<li class="listitem" style="list-style-type: *"><p id="x_78"></a>Variable names cannot begin with digits, but can contain digits in them.</p></li>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   386
</ul></div></blockquote></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   387
<p id="x_79"></a>In reference to the previous section examples, 'a', 'b', 'lng', 'fl', 'e' and 'cplx' are all variables of various datatypes.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   388
<pre class="programlisting">Note: Python is not a strongly typed language and hence an integer variable can at a
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   389
later stage be used as a float variable as well.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   390
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   391
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   392
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   393
<div class="section" title="4.3. Strings">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   394
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   395
<a name="strings"></a>4.3. Strings</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   396
<p id="x_7a"></a>Strings are one of the essential data structures of any programming language. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   397
        <code class="literal">print "Hello, World!"</code> program was introduced in the earlier section, and the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   398
        <span class="emphasis"><em>"Hello, World!"</em></span> in the print statement is a string. A string is basically a set of characters. Strings can be represented in various ways shown below:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   399
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   400
<pre class="programlisting">s = 'this is a string'              # a string variable can be represented using single quotes
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   401
s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   402
s = "I have 'single-quotes' inside!"
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   403
l = "A string spanning many lines\
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   404
one more line\
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   405
yet another"                        # a string can span more than a single line.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   406
t = """A triple quoted string does  # another way of representing multiline strings.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   407
not need to be escaped at the end and
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   408
"can have nested quotes" etc."""
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   409
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   410
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   411
<p id="x_7b"></a>Try the following on the interpreter: 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   412
        <code class="literal">s = 'this is a string with 'quotes' of similar kind'</code>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   413
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   414
<p id="x_7c"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   415
        <span class="strong"><strong>Exercise: How to use single quotes within single quotes in a string as shown in the above example without getting an error?</strong></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   416
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   417
<div class="section" title="4.3.1. String operations">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   418
<div class="titlepage"><div><div><h4 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   419
<a name="string-operations"></a>4.3.1. String operations</h4></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   420
<p id="x_7d"></a>A few basic string operations are presented here.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   421
<p id="x_7e"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   422
          <span class="strong"><strong>String concatenation</strong></span> String concatenation is done by simple addition of two strings.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   423
        </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   424
<pre class="programlisting">&gt;&gt;&gt; x = 'Hello'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   425
&gt;&gt;&gt; y = ' Python'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   426
&gt;&gt;&gt; print x+y
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   427
Hello Python
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   428
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   429
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   430
<p id="x_7f"></a>          <span class="emphasis"><em>Try this yourself:</em></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   431
        </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   432
<pre class="programlisting">&gt;&gt;&gt; somenum = 13
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   433
&gt;&gt;&gt; print x+somenum
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   434
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   435
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   436
<p id="x_80"></a>The problem with the above example is that here a string variable and an integer variable are trying to be concantenated. To obtain the desired result from the above example the str(), repr() and the `` can be used.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   437
<p id="x_81"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   438
          <span class="strong"><strong>str()</strong></span> simply converts a value to a string in a reasonable form. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   439
          <span class="strong"><strong>repr()</strong></span> creates a string that is a representation of the value.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   440
        </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   441
<p id="x_82"></a>The difference can be seen in the example shown below:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   442
<pre class="programlisting">&gt;&gt;&gt; str(1000000000000000000000000000000000000000000000000L)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   443
'1000000000000000000000000000000000000000000000000'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   444
&gt;&gt;&gt; repr(1000000000000000000000000000000000000000000000000L)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   445
'1000000000000000000000000000000000000000000000000L'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   446
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   447
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   448
<p id="x_83"></a>It can be observed that the 'L' in the long value shown was omitted by str(), whereas repr() converted that into a string too. An alternative way of using repr(value) is `
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   449
          <code class="literal">value</code>`.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   450
        </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   451
<p id="x_84"></a>A few more examples:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   452
<pre class="programlisting">&gt;&gt;&gt; x = "Let's go \nto Pycon"
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   453
&gt;&gt;&gt; print x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   454
Let's go 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   455
to Pycon
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   456
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   457
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   458
<p id="x_85"></a>In the above example, notice that the '\n'(newline) character is formatted and the string is printed on two lines. The strings discussed until now were normal strings. Other than these there are two other types of strings namely, raw strings and unicode strings.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   459
<p id="x_86"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   460
          <span class="strong"><strong>Raw strings</strong></span> are strings which are unformatted, that is the backslashes(\) are not parsed and are left as it is in the string. Raw strings are represented with an 'r' at the start of a string. Let us look at an example
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   461
        </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   462
<pre class="programlisting">&gt;&gt;&gt; x = r"Let's go \nto Pycon"
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   463
&gt;&gt;&gt; print x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   464
Let's go \nto Pycon
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   465
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   466
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   467
<p id="x_87"></a>Note: The '\n' is not being parsed into a new line and is left as it is.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   468
<p id="x_88"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   469
          <span class="emphasis"><em>Try this yourself:</em></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   470
        </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   471
<pre class="programlisting">&gt;&gt;&gt; x = r"Let's go to Pycon\"
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   472
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   473
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   474
<p id="x_89"></a>          <span class="strong"><strong>Unicode strings</strong></span> are strings where the characters are Unicode characters as opposed to ASCII characters. Unicode strings are represented with a 'u' at the start of the string. Let us look at an example:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   475
        </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   476
<pre class="programlisting">&gt;&gt;&gt; x = u"Let's go to Pycon!"
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   477
&gt;&gt;&gt; print x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   478
Let's go to Pycon!
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   479
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   480
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   481
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   482
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   483
<div class="section" title="4.4. Boolean">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   484
<div class="titlepage"><div><div><h3 class="title">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   485
<a name="boolean"></a>4.4. Boolean</h3></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   486
<p id="x_8a"></a>Python also provides special Boolean datatype. A boolean variable can assume a value of either 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   487
        <span class="emphasis"><em>True</em></span> or 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   488
        <span class="emphasis"><em>False</em></span> (Note the capitalizations).
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   489
      </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   490
<p id="x_8b"></a>Let us look at examples:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   491
<pre class="programlisting">&gt;&gt;&gt; t = True
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   492
&gt;&gt;&gt; f = not t
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   493
&gt;&gt;&gt; print f
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   494
False
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   495
&gt;&gt;&gt; f or t
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   496
True
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   497
&gt;&gt;&gt; f and t
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   498
False
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   499
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   500
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   501
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   502
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   503
<div class="section" title="5. The while loop">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   504
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   505
<a name="the-while-loop"></a>5. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   506
      <span class="strong"><strong>while</strong></span> loop
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   507
    </h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   508
<p id="x_8c"></a>The Python 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   509
      <span class="strong"><strong>while</strong></span> loop is similar to the C/C++ while loop. The syntax is as follows:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   510
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   511
<pre class="programlisting">statement 0
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   512
while condition:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   513
  statement 1 #while block
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   514
  statement 2 #while block
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   515
statement 3 #outside the while block.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   516
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   517
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   518
<p id="x_8d"></a>Let us look at an example:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   519
<pre class="programlisting">&gt;&gt;&gt; x = 1  
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   520
&gt;&gt;&gt; while x &lt;= 5:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   521
...   print x
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   522
...   x += 1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   523
... 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   524
1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   525
2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   526
3
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   527
4
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   528
5
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   529
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   530
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   531
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   532
<div class="section" title="6. The if conditional">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   533
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   534
<a name="the-if-conditional"></a>6. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   535
      <span class="strong"><strong>if</strong></span> conditional
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   536
    </h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   537
<p id="x_8e"></a>The Python 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   538
      <span class="strong"><strong>if</strong></span> block provides the conditional execution of statements. If the condition evaluates as true the block of statements defined under the if block are executed.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   539
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   540
<p id="x_8f"></a>If the first block is not executed on account of the condition not being satisfied, the set of statements in the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   541
      <span class="strong"><strong>else</strong></span> block are executed.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   542
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   543
<p id="x_90"></a>The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   544
      <span class="strong"><strong>elif</strong></span> block provides the functionality of evaluation of multiple conditions as shown in the example.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   545
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   546
<p id="x_91"></a>The syntax is as follows:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   547
<pre class="programlisting">if condition :
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   548
    statement_1
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   549
    statement_2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   550
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   551
elif condition:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   552
    statement_3
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   553
    statement_4
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   554
else:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   555
    statement_5
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   556
    statement_6
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   557
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   558
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   559
<p id="x_92"></a>Let us look at an example:</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   560
<pre class="programlisting">&gt;&gt;&gt; n = raw_input("Input a number:")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   561
&gt;&gt;&gt; if n &lt; 0:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   562
      print n," is negative"
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   563
      elif n &gt; 0:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   564
      print n," is positive"
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   565
      else:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   566
      print n, " is 0"
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   567
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   568
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   569
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   570
<div class="section" title="7.  raw_input()">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   571
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   572
<a name="raw_input"></a>7.       <span class="strong"><strong>raw_input()</strong></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   573
    </h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   574
<p id="x_93"></a>In the previous example we saw the call to the raw_input() subroutine. The 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   575
      <span class="strong"><strong>raw_input()</strong></span> method is used to take user inputs through the console. Unlike 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   576
      <span class="strong"><strong>input()</strong></span> which assumes the data entered by the user as a standard python expression, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   577
      <span class="strong"><strong>raw_input()</strong></span> treats all the input data as raw data and converts everything into a string. To illustrate this let us look at an example.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   578
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   579
<pre class="programlisting">&gt;&gt;&gt; input("Enter a number thats a palindrome:")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   580
Enter a number thats a palindrome:121
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   581
121
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   582
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   583
&gt;&gt;&gt; input("Enter your name:")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   584
Enter your name:PythonFreak
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   585
Traceback (most recent call last):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   586
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   587
  File "&lt;string&gt;", line 1, in &lt;module&gt;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   588
NameError: name 'PythonFreak' is not defined
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   589
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   590
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   591
<p id="x_94"></a>As shown above the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   592
      <span class="strong"><strong>input()</strong></span> assumes that the data entered is a valid Python expression. In the first call it prompts for an integer input and when entered it accepts the integer as an integer, whereas in the second call, when the string is entered without the quotes, 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   593
      <span class="strong"><strong>input()</strong></span> assumes that the entered data is a valid Python expression and hence it raises and exception saying PythonFreak is not defined.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   594
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   595
<pre class="programlisting">&gt;&gt;&gt; input("Enter your name:")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   596
Enter your name:'PythonFreak'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   597
'PythonFreak'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   598
&gt;&gt;&gt; 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   599
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   600
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   601
<p id="x_95"></a>Here the name is accepted because its entered as a string (within quotes). But its unreasonable to go on using quotes each time a string is entered. Hence the alternative is to use 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   602
      <span class="strong"><strong>raw_input()</strong></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   603
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   604
<p id="x_96"></a>Let us now look at how 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   605
      <span class="strong"><strong>raw_input()</strong></span> operates with an example.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   606
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   607
<pre class="programlisting">&gt;&gt;&gt; raw_input("Enter your name:")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   608
Enter your name:PythonFreak
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   609
'PythonFreak'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   610
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   611
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   612
<p id="x_97"></a>Observe that the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   613
      <span class="strong"><strong>raw_input()</strong></span> is converting it into a string all by itself.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   614
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   615
<pre class="programlisting">&gt;&gt;&gt; pal = raw_input("Enter a number thats a palindrome:")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   616
Enter a number thats a palindrome:121
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   617
'121'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   618
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   619
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   620
<p id="x_98"></a>Observe that 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   621
      <span class="strong"><strong>raw_input()</strong></span> is converting the integer 121 also to a string as '121'. Let us look at another example:
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   622
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   623
<pre class="programlisting">&gt;&gt;&gt; pal = raw_input("Enter a number thats a palindrome:")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   624
Enter a number thats a palindrome:121
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   625
&gt;&gt;&gt; pal + 2
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   626
Traceback (most recent call last):
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   627
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   628
TypeError: cannot concatenate 'str' and 'int' objects
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   629
&gt;&gt;&gt; pal
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   630
'121'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   631
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   632
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   633
<p id="x_99"></a>Observe here that the variable 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   634
      <span class="emphasis"><em>pal</em></span> is a string and hence integer operations cannot be performed on it. Hence the exception is raised.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   635
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   636
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   637
<div class="section" title="8.  int() method">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   638
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   639
<a name="int-method"></a>8. 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   640
      <span class="strong"><strong>int()</strong></span> method
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   641
    </h2></div></div></div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   642
<p id="x_9a"></a>Generally for computing purposes, the data used is not strings or raw data but on integers, floats and similar mathematical data structures. The data obtained from 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   643
      <span class="strong"><strong>raw_input()</strong></span> is raw data in the form of strings. In order to obtain integers from strings we use the method 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   644
      <span class="strong"><strong>int()</strong></span>.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   645
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   646
<p id="x_9b"></a>Let us look at an example.</p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   647
<pre class="programlisting">&gt;&gt;&gt; intpal = int(pal)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   648
&gt;&gt;&gt; intpal
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   649
121
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   650
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   651
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   652
<p id="x_9c"></a>In the previous example it was observed that 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   653
      <span class="emphasis"><em>pal</em></span> was a string variable. Here using the 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   654
      <span class="strong"><strong>int()</strong></span> method the string 
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   655
      <span class="emphasis"><em>pal</em></span> was converted to an integer variable.
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   656
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   657
<p id="x_9d"></a>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   658
      <span class="emphasis"><em>Try This Yourself:</em></span>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   659
    </p>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   660
<pre class="programlisting">&gt;&gt;&gt; stringvar = raw_input("Enter a name:")
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   661
Enter a name:Guido Van Rossum
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   662
&gt;&gt;&gt; stringvar
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   663
'Guido Van Rossum'
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   664
&gt;&gt;&gt; numvar = int(stringvar)
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   665
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   666
</pre>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   667
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   668
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   669
<div class="navfooter">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   670
<hr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   671
<table width="100%" summary="Navigation footer">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   672
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   673
<td width="40%" align="left">
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   674
<a accesskey="p" href="index.html">Prev</a> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   675
<td width="20%" align="center"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   676
<td width="40%" align="right"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   677
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   678
<tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   679
<td width="40%" align="left" valign="top">Chapter 1. Introduction </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   680
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   681
<td width="40%" align="right" valign="top"> </td>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   682
</tr>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   683
</table>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   684
</div>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   685
</body>
8083d21c0020 The first commit of all the required files for the review app
amit@thunder
parents:
diff changeset
   686
</html>