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