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