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