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