1 <html> |
1 <html> |
2 <head> |
2 <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. intro</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head> |
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
3 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch2intro"> |
4 <title> |
4 <div class="titlepage"></div> |
5 Chapter. intro |
5 <div class="toc"> |
6 </title> |
6 <p><b>Table of Contents</b></p> |
7 <link rel="stylesheet" href="/review/support/styles.css" type="text/css" /> |
7 <dl> |
8 <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /> |
8 <dt><span class="article"><a href="#id2915639">Basic Python</a></span></dt> |
9 <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /> |
9 <dd><dl> |
10 <script type="text/javascript" src="/review/support/jquery-min.js"> |
10 <dt><span class="section"><a href="#id2723402">1. Introduction</a></span></dt> |
11 </script> |
11 <dt><span class="section"><a href="#id2966769">2. The Python Interpreter</a></span></dt> |
12 <script type="text/javascript" src="/review/support/form.js"> |
12 <dd><dl> |
13 </script> |
13 <dt><span class="section"><a href="#id2966777">2.1. The Interactive Interpreter</a></span></dt> |
14 <script type="text/javascript" src="/review/support/hsbook.js"> |
14 <dt><span class="section"><a href="#id2966950">2.2. <span class="emphasis"><em>ipython</em></span> - An enhanced interactive Python interpreter</a></span></dt> |
15 </script> |
15 </dl></dd> |
16 </head> |
16 <dt><span class="section"><a href="#id2967075">3. Editing and running a python file</a></span></dt> |
17 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
17 <dt><span class="section"><a href="#id2967182">4. Basic Datatypes and operators in Python</a></span></dt> |
18 <div class="chapter" id="ch2intro"> |
18 <dd><dl> |
19 <div class="titlepage"> |
19 <dt><span class="section"><a href="#id2967216">4.1. Numbers</a></span></dt> |
20 </div> |
20 <dt><span class="section"><a href="#id2967346">4.2. Variables</a></span></dt> |
21 <div class="toc"> |
21 <dt><span class="section"><a href="#id2967394">4.3. Strings</a></span></dt> |
22 <p> |
22 <dt><span class="section"><a href="#id2967588">4.4. Boolean</a></span></dt> |
23 <b> |
23 </dl></dd> |
24 Table of Contents |
24 <dt><span class="section"><a href="#id2967617">5. The <span class="strong"><strong>while</strong></span> loop</a></span></dt> |
25 </b> |
25 <dt><span class="section"><a href="#id2967654">6. The <span class="strong"><strong>if</strong></span> conditional</a></span></dt> |
26 </p> |
26 <dt><span class="section"><a href="#id2967723">7. <span class="strong"><strong>raw_input()</strong></span></a></span></dt> |
27 <dl> |
27 <dt><span class="section"><a href="#id2967877">8. <span class="strong"><strong>int()</strong></span> method</a></span></dt> |
28 <dt> |
28 </dl></dd> |
29 <span class="article"> |
29 </dl> |
30 <a href="#id2766476"> |
30 </div> |
31 Basic Python |
31 <div class="article" title="Basic Python"> |
32 </a> |
32 <div class="titlepage"> |
33 </span> |
33 <div><div><h2 class="title"> |
34 </dt> |
34 <a name="id2915639"></a>Basic Python</h2></div></div> |
35 <dd> |
35 <hr /> |
36 <dl> |
36 </div> |
37 <dt> |
37 <div class="toc"> |
38 <span class="section"> |
38 <p><b>Table of Contents</b></p> |
39 <a href="#id2574240"> |
39 <dl> |
40 1. Introduction |
40 <dt><span class="section"><a href="#id2723402">1. Introduction</a></span></dt> |
41 </a> |
41 <dt><span class="section"><a href="#id2966769">2. The Python Interpreter</a></span></dt> |
42 </span> |
42 <dd><dl> |
43 </dt> |
43 <dt><span class="section"><a href="#id2966777">2.1. The Interactive Interpreter</a></span></dt> |
44 <dt> |
44 <dt><span class="section"><a href="#id2966950">2.2. <span class="emphasis"><em>ipython</em></span> - An enhanced interactive Python interpreter</a></span></dt> |
45 <span class="section"> |
45 </dl></dd> |
46 <a href="#id2817606"> |
46 <dt><span class="section"><a href="#id2967075">3. Editing and running a python file</a></span></dt> |
47 2. The Python Interpreter |
47 <dt><span class="section"><a href="#id2967182">4. Basic Datatypes and operators in Python</a></span></dt> |
48 </a> |
48 <dd><dl> |
49 </span> |
49 <dt><span class="section"><a href="#id2967216">4.1. Numbers</a></span></dt> |
50 </dt> |
50 <dt><span class="section"><a href="#id2967346">4.2. Variables</a></span></dt> |
51 <dd> |
51 <dt><span class="section"><a href="#id2967394">4.3. Strings</a></span></dt> |
52 <dl> |
52 <dt><span class="section"><a href="#id2967588">4.4. Boolean</a></span></dt> |
53 <dt> |
53 </dl></dd> |
54 <span class="section"> |
54 <dt><span class="section"><a href="#id2967617">5. The <span class="strong"><strong>while</strong></span> loop</a></span></dt> |
55 <a href="#id2817615"> |
55 <dt><span class="section"><a href="#id2967654">6. The <span class="strong"><strong>if</strong></span> conditional</a></span></dt> |
56 2.1. The Interactive Interpreter |
56 <dt><span class="section"><a href="#id2967723">7. <span class="strong"><strong>raw_input()</strong></span></a></span></dt> |
57 </a> |
57 <dt><span class="section"><a href="#id2967877">8. <span class="strong"><strong>int()</strong></span> method</a></span></dt> |
58 </span> |
58 </dl> |
59 </dt> |
59 </div> |
60 <dt> |
60 <p id="ch2intro_1">This document is intended to be handed out at the end of the workshop. It has |
61 <span class="section"> |
|
62 <a href="#id2817788"> |
|
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="#id2817912"> |
|
78 3. Editing and running a python file |
|
79 </a> |
|
80 </span> |
|
81 </dt> |
|
82 <dt> |
|
83 <span class="section"> |
|
84 <a href="#id2818019"> |
|
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="#id2818053"> |
|
94 4.1. Numbers |
|
95 </a> |
|
96 </span> |
|
97 </dt> |
|
98 <dt> |
|
99 <span class="section"> |
|
100 <a href="#id2818183"> |
|
101 4.2. Variables |
|
102 </a> |
|
103 </span> |
|
104 </dt> |
|
105 <dt> |
|
106 <span class="section"> |
|
107 <a href="#id2818231"> |
|
108 4.3. Strings |
|
109 </a> |
|
110 </span> |
|
111 </dt> |
|
112 <dt> |
|
113 <span class="section"> |
|
114 <a href="#id2818425"> |
|
115 4.4. Boolean |
|
116 </a> |
|
117 </span> |
|
118 </dt> |
|
119 </dl> |
|
120 </dd> |
|
121 <dt> |
|
122 <span class="section"> |
|
123 <a href="#id2818454"> |
|
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="#id2818491"> |
|
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="#id2818560"> |
|
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="#id2818715"> |
|
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="id2766476"> |
|
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="#id2574240"> |
|
199 1. Introduction |
|
200 </a> |
|
201 </span> |
|
202 </dt> |
|
203 <dt> |
|
204 <span class="section"> |
|
205 <a href="#id2817606"> |
|
206 2. The Python Interpreter |
|
207 </a> |
|
208 </span> |
|
209 </dt> |
|
210 <dd> |
|
211 <dl> |
|
212 <dt> |
|
213 <span class="section"> |
|
214 <a href="#id2817615"> |
|
215 2.1. The Interactive Interpreter |
|
216 </a> |
|
217 </span> |
|
218 </dt> |
|
219 <dt> |
|
220 <span class="section"> |
|
221 <a href="#id2817788"> |
|
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="#id2817912"> |
|
237 3. Editing and running a python file |
|
238 </a> |
|
239 </span> |
|
240 </dt> |
|
241 <dt> |
|
242 <span class="section"> |
|
243 <a href="#id2818019"> |
|
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="#id2818053"> |
|
253 4.1. Numbers |
|
254 </a> |
|
255 </span> |
|
256 </dt> |
|
257 <dt> |
|
258 <span class="section"> |
|
259 <a href="#id2818183"> |
|
260 4.2. Variables |
|
261 </a> |
|
262 </span> |
|
263 </dt> |
|
264 <dt> |
|
265 <span class="section"> |
|
266 <a href="#id2818231"> |
|
267 4.3. Strings |
|
268 </a> |
|
269 </span> |
|
270 </dt> |
|
271 <dt> |
|
272 <span class="section"> |
|
273 <a href="#id2818425"> |
|
274 4.4. Boolean |
|
275 </a> |
|
276 </span> |
|
277 </dt> |
|
278 </dl> |
|
279 </dd> |
|
280 <dt> |
|
281 <span class="section"> |
|
282 <a href="#id2818454"> |
|
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="#id2818491"> |
|
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="#id2818560"> |
|
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="#id2818715"> |
|
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 |
|
335 been designed for Engineering students who are Python beginners and have basic |
61 been designed for Engineering students who are Python beginners and have basic |
336 programming skills. The focus is on basic numerics and plotting using Python. |
62 programming skills. The focus is on basic numerics and plotting using Python.</p>The system requirements:<div class="itemizedlist"><ul class="itemizedlist" type="*"> |
337 </p> |
63 <li class="listitem" style="list-style-type: *"><p id="ch2intro_2">Python - version 2.5.x or newer.</p></li> |
338 The system requirements: |
64 <li class="listitem" style="list-style-type: *"><p id="ch2intro_3">IPython</p></li> |
339 <div class="itemizedlist"> |
65 <li class="listitem" style="list-style-type: *"><p id="ch2intro_4">Text editor - scite, vim, emacs or whatever you are comfortable with.</p></li> |
340 <ul class="itemizedlist" type="*"> |
66 </ul></div> |
341 <li class="listitem" style="list-style-type: *"> |
67 <div class="section" title="1.Introduction"> |
342 <p id="ch2intro_2"> |
68 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
343 Python - version 2.5.x or newer. |
69 <a name="id2723402"></a>1.Introduction</h2></div></div></div> |
344 </p> |
70 <p id="ch2intro_5">The Python programming language was created by a dutch named Guido van Rossum. |
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="id2574240"> |
|
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. |
|
372 The idea of Python was conceived in December 1989. The name Python has nothing |
71 The idea of Python was conceived in December 1989. The name Python has nothing |
373 to do with the reptilian, but its been named after the 70s comedy series |
72 to do with the reptilian, but its been named after the 70s comedy series |
374 "Monty Python's Flying Circus", since it happens to be Guido's favourite |
73 "Monty Python's Flying Circus", since it happens to be Guido's favourite |
375 TV series. |
74 TV series.</p> |
376 </p> |
75 <p id="ch2intro_6">Current stable version of Python is 2.6.x, although Python 3.0 is also the stable |
377 <p id="ch2intro_6"> |
|
378 Current stable version of Python is 2.6.x, although Python 3.0 is also the stable |
|
379 version, it is not backwards compatible with the previous versions and is hence |
76 version, it is not backwards compatible with the previous versions and is hence |
380 not entirely popular at the moment. This material will focus on the 2.6.x series. |
77 not entirely popular at the moment. This material will focus on the 2.6.x series.</p> |
381 </p> |
78 <p id="ch2intro_7">Python is licensed under the Python Software Foundation License (PSF License) |
382 <p id="ch2intro_7"> |
|
383 Python is licensed under the Python Software Foundation License (PSF License) |
|
384 which is GPL compatible Free Software license (excepting license version 1.6 and 2.0) |
79 which is GPL compatible Free Software license (excepting license version 1.6 and 2.0) |
385 It is a no strings attached license, which means the source code is free to modify |
80 It is a no strings attached license, which means the source code is free to modify |
386 and redistribute. |
81 and redistribute.</p> |
387 </p> |
82 <p id="ch2intro_8">The Python docs define Python as "Python is an interpreted, object-oriented, |
388 <p id="ch2intro_8"> |
|
389 The Python docs define Python as "Python is an interpreted, object-oriented, |
|
390 high-level programming language with dynamic semantics." A more detailed summary |
83 high-level programming language with dynamic semantics." A more detailed summary |
391 can be found at |
84 can be found at </p> |
392 </p> |
85 <div class="reference"> |
393 <div class="reference"> |
86 <div class="titlepage"><hr /></div>http://www.python.org/doc/essays/blurb.html</div> |
394 <div class="titlepage"> |
87 <p>. Python is a language that |
395 <hr /> |
|
396 </div> |
|
397 http://www.python.org/doc/essays/blurb.html |
|
398 </div> |
|
399 <p> |
|
400 . Python is a language that |
|
401 has been designed to help the programmer concentrate on solving the problem at hand |
88 has been designed to help the programmer concentrate on solving the problem at hand |
402 and not worry about the programming language idiosyncrasies. |
89 and not worry about the programming language idiosyncrasies.</p> |
403 </p> |
90 <p id="ch2intro_9">Python is a highly cross platform compatible language on account of it being an |
404 <p id="ch2intro_9"> |
|
405 Python is a highly cross platform compatible language on account of it being an |
|
406 interpreted language. It is highly scalable and hence has been adapted to run on |
91 interpreted language. It is highly scalable and hence has been adapted to run on |
407 the Nokia 60 series phones. Python has been designed to be readable and easy to use |
92 the Nokia 60 series phones. Python has been designed to be readable and easy to use</p> |
408 </p> |
93 <p id="ch2intro_a"><span class="strong"><strong>Resources available for reference</strong></span></p> |
409 <p id="ch2intro_a"> |
94 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
410 <span class="strong"> |
95 <li class="listitem" style="list-style-type: *"> |
411 <strong> |
96 <p id="ch2intro_b">Web: </p> |
412 Resources available for reference |
97 <div class="reference"> |
413 </strong> |
98 <div class="titlepage"><hr /></div>http://www.python.org</div> |
414 </span> |
99 </li> |
415 </p> |
100 <li class="listitem" style="list-style-type: *"> |
416 <div class="itemizedlist"> |
101 <p id="ch2intro_c">Doc: </p> |
417 <ul class="itemizedlist" type="*"> |
102 <div class="reference"> |
418 <li class="listitem" style="list-style-type: *"> |
103 <div class="titlepage"><hr /></div>http://www.python.org/doc</div> |
419 <p id="ch2intro_b"> |
104 </li> |
420 Web: |
105 <li class="listitem" style="list-style-type: *">Free Tutorials:<div class="itemizedlist"><ul class="itemizedlist" type="*"> |
421 </p> |
106 <li class="listitem" style="list-style-type: *"> |
422 <div class="reference"> |
107 <p id="ch2intro_d">Official Python Tutorial: </p> |
423 <div class="titlepage"> |
108 <div class="reference"> |
424 <hr /> |
109 <div class="titlepage"><hr /></div>http://docs.python.org/tut/tut.html</div> |
425 </div> |
110 </li> |
426 http://www.python.org |
111 <li class="listitem" style="list-style-type: *"> |
427 </div> |
112 <p id="ch2intro_e">Byte of Python: </p> |
428 </li> |
113 <div class="reference"> |
429 <li class="listitem" style="list-style-type: *"> |
114 <div class="titlepage"><hr /></div>http://www.byteofpython.info/</div> |
430 <p id="ch2intro_c"> |
115 </li> |
431 Doc: |
116 <li class="listitem" style="list-style-type: *"> |
432 </p> |
117 <p id="ch2intro_f">Dive into Python: </p> |
433 <div class="reference"> |
118 <div class="reference"> |
434 <div class="titlepage"> |
119 <div class="titlepage"><hr /></div>http://diveintopython.org/</div> |
435 <hr /> |
120 </li> |
436 </div> |
121 </ul></div> |
437 http://www.python.org/doc |
122 </li> |
438 </div> |
123 </ul></div> |
439 </li> |
124 <p id="ch2intro_10"><span class="strong"><strong>Advantages of Python - Why Python??</strong></span></p> |
440 <li class="listitem" style="list-style-type: *"> |
125 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
441 Free Tutorials: |
126 <li class="listitem" style="list-style-type: *"><p id="ch2intro_11">Python has been designed for readability and ease of use. Its been designed in |
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 |
|
494 such a fashion that it imposes readability on the programmer. Python does away |
127 such a fashion that it imposes readability on the programmer. Python does away |
495 with the braces and the semicolons and instead implements code blocks based on |
128 with the braces and the semicolons and instead implements code blocks based on |
496 indentation, thus enhancing readability. |
129 indentation, thus enhancing readability.</p></li> |
497 </p> |
130 <li class="listitem" style="list-style-type: *"><p id="ch2intro_12">Python is a high level, interpreted, modular and object oriented language. |
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. |
|
502 Python performs memory management on its own, thus the programmer need not bother |
131 Python performs memory management on its own, thus the programmer need not bother |
503 about allocating and deallocating memory to variables. Python provides extensibility |
132 about allocating and deallocating memory to variables. Python provides extensibility |
504 by providing modules which can be easily imported similar to headers in C and |
133 by providing modules which can be easily imported similar to headers in C and |
505 packages in Java. Python is object oriented and hence provides all the object oriented |
134 packages in Java. Python is object oriented and hence provides all the object oriented |
506 characteristics such as inheritance, encapsulation and polymorphism. |
135 characteristics such as inheritance, encapsulation and polymorphism.</p></li> |
507 </p> |
136 <li class="listitem" style="list-style-type: *"><p id="ch2intro_13">Python offers a highly powerful interactive programming interface in the form |
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 |
|
512 of the 'Interactive Interpreter' which will be discussed in more detail in the |
137 of the 'Interactive Interpreter' which will be discussed in more detail in the |
513 following sections. |
138 following sections.</p></li> |
514 </p> |
139 <li class="listitem" style="list-style-type: *"> |
515 </li> |
140 <p id="ch2intro_14">Python provides a rich standard library and an extensive set of modules. The |
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 |
|
519 power of Python modules can be seen in this slightly exaggerated cartoon |
141 power of Python modules can be seen in this slightly exaggerated cartoon |
520 </p> |
142 </p> |
521 <div class="reference"> |
143 <div class="reference"> |
522 <div class="titlepage"> |
144 <div class="titlepage"><hr /></div>http://xkcd.com/353/</div> |
523 <hr /> |
145 </li> |
524 </div> |
146 <li class="listitem" style="list-style-type: *"><p id="ch2intro_15">Python interfaces well with most other programming languages such as C, C++ |
525 http://xkcd.com/353/ |
147 and FORTRAN.</p></li> |
526 </div> |
148 </ul></div> |
527 </li> |
149 <p id="ch2intro_16">Although, Python has one setback. Python is not fast as some of the compiled |
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 |
|
538 languages like C or C++. Yet, the amount of flexibility and power more than make |
150 languages like C or C++. Yet, the amount of flexibility and power more than make |
539 up for this setback. |
151 up for this setback.</p> |
540 </p> |
152 </div> |
541 </div> |
153 <div class="section" title="2.The Python Interpreter"> |
542 <div class="section" title="2.The Python Interpreter"> |
154 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
543 <div class="titlepage"> |
155 <a name="id2966769"></a>2.The Python Interpreter</h2></div></div></div> |
544 <div> |
156 <div class="section" title="2.1.The Interactive Interpreter"> |
545 <div> |
157 <div class="titlepage"><div><div><h3 class="title"> |
546 <h2 class="title" style="clear: both"> |
158 <a name="id2966777"></a>2.1.The Interactive Interpreter</h3></div></div></div> |
547 <a name="id2817606"> |
159 <p id="ch2intro_17">Typing <span class="emphasis"><em>python</em></span> at the shell prompt on any standard Unix/Gnu-Linux system and |
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="id2817615"> |
|
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 |
|
574 hitting the enter key fires up the Python 'Interactive Interpreter'. The Python |
160 hitting the enter key fires up the Python 'Interactive Interpreter'. The Python |
575 interpreter is one of the most integral features of Python. The prompt obtained |
161 interpreter is one of the most integral features of Python. The prompt obtained |
576 when the interactive interpreter is similar to what is shown below. The exact |
162 when the interactive interpreter is similar to what is shown below. The exact |
577 appearance might differ based on the version of Python being used. The |
163 appearance might differ based on the version of Python being used. The <code class="literal">>>></code> |
578 <code class="literal"> |
164 thing shown is the python prompt. When something is typed at the prompt and the |
579 >>> |
|
580 </code> |
|
581 thing shown is the python prompt. When something is typed at the prompt and the |
|
582 enter key is hit, the python interpreter interprets the command entered and |
165 enter key is hit, the python interpreter interprets the command entered and |
583 performs the appropriate action. All the examples presented in this document are |
166 performs the appropriate action. All the examples presented in this document are |
584 to be tried hands on, on the interactive interpreter. |
167 to be tried hands on, on the interactive interpreter.</p> |
585 </p> |
168 <pre class="programlisting"> |
586 <pre class="programlisting"> |
169 Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) |
587 Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) |
|
588 [GCC 4.3.2] on linux2 |
170 [GCC 4.3.2] on linux2 |
589 Type "help", "copyright", "credits" or "license" for more information. |
171 Type "help", "copyright", "credits" or "license" for more information. |
590 >>> |
172 >>></pre> |
591 </pre> |
173 <p id="ch2intro_18">Lets try with an example, type <code class="literal">print 'Hello, World!'</code> at the prompt and hit |
592 <p id="ch2intro_18"> |
174 the enter key.</p> |
593 Lets try with an example, type |
175 <pre class="programlisting"> |
594 <code class="literal"> |
176 >>> print 'Hello, World!' |
595 print 'Hello, World!' |
177 Hello, World!</pre> |
596 </code> |
178 <p id="ch2intro_19">This example was quite straight forward, and thus we have written our first |
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 |
|
606 line of Python code. Now let us try typing something arbitrary at the prompt. |
179 line of Python code. Now let us try typing something arbitrary at the prompt. |
607 For example: |
180 For example:</p> |
608 </p> |
181 <pre class="programlisting"> |
609 <pre class="programlisting"> |
182 >>> arbit word |
610 >>> arbit word |
|
611 File "<stdin>", line 1 |
183 File "<stdin>", line 1 |
612 arbit word |
184 arbit word |
613 ^ |
185 ^ |
614 SyntaxError: invalid syntax |
186 SyntaxError: invalid syntax |
615 >>> |
187 >>></pre> |
616 </pre> |
188 <p id="ch2intro_1a">The interpreter gave an error message saying that 'arbit word' was invalid |
617 <p id="ch2intro_1a"> |
|
618 The interpreter gave an error message saying that 'arbit word' was invalid |
|
619 syntax which is valid. The interpreter is an amazing tool when learning to |
189 syntax which is valid. The interpreter is an amazing tool when learning to |
620 program in Python. The interpreter provides a help function that provides the |
190 program in Python. The interpreter provides a help function that provides the |
621 necessary documentation regarding all Python syntax, constructs, modules and |
191 necessary documentation regarding all Python syntax, constructs, modules and |
622 objects. Typing |
192 objects. Typing <span class="emphasis"><em>help()</em></span> at the prompt gives the following output:</p> |
623 <span class="emphasis"> |
193 <pre class="programlisting"> |
624 <em> |
194 >>> help() |
625 help() |
|
626 </em> |
|
627 </span> |
|
628 at the prompt gives the following output: |
|
629 </p> |
|
630 <pre class="programlisting"> |
|
631 >>> help() |
|
632 |
195 |
633 Welcome to Python 2.5! This is the online help utility. |
196 Welcome to Python 2.5! This is the online help utility. |
634 |
197 |
635 If this is your first time using Python, you should definitely check out |
198 If this is your first time using Python, you should definitely check out |
636 the tutorial on the Internet at http://www.python.org/doc/tut/. |
199 the tutorial on the Internet at http://www.python.org/doc/tut/. |
642 To get a list of available modules, keywords, or topics, type "modules", |
205 To get a list of available modules, keywords, or topics, type "modules", |
643 "keywords", or "topics". Each module also comes with a one-line summary |
206 "keywords", or "topics". Each module also comes with a one-line summary |
644 of what it does; to list the modules whose summaries contain a given word |
207 of what it does; to list the modules whose summaries contain a given word |
645 such as "spam", type "modules spam". |
208 such as "spam", type "modules spam". |
646 |
209 |
647 help> |
210 help></pre> |
648 </pre> |
211 <p id="ch2intro_1b">As mentioned in the output, entering the name of any module, keyword or topic |
649 <p id="ch2intro_1b"> |
|
650 As mentioned in the output, entering the name of any module, keyword or topic |
|
651 will provide the documentation and help regarding the same through the online |
212 will provide the documentation and help regarding the same through the online |
652 help utility. Pressing |
213 help utility. Pressing <span class="emphasis"><em>Ctrl+d</em></span> exits the help prompt and returns to the |
653 <span class="emphasis"> |
214 python prompt.</p> |
654 <em> |
215 <p id="ch2intro_1c">Let us now try a few examples at the python interpreter.</p> |
655 Ctrl+d |
216 <p id="ch2intro_1d">Eg 1:</p> |
656 </em> |
217 <pre class="programlisting"> |
657 </span> |
218 >>> print 'Hello, python!' |
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!' |
|
669 Hello, python! |
219 Hello, python! |
670 >>> |
220 >>></pre> |
671 </pre> |
221 <p id="ch2intro_1e">Eg 2:</p> |
672 <p id="ch2intro_1e"> |
222 <pre class="programlisting"> |
673 Eg 2: |
223 >>> print 4321*567890 |
674 </p> |
|
675 <pre class="programlisting"> |
|
676 >>> print 4321*567890 |
|
677 2453852690 |
224 2453852690 |
678 >>> |
225 >>></pre> |
679 </pre> |
226 <p id="ch2intro_1f">Eg 3:</p> |
680 <p id="ch2intro_1f"> |
227 <pre class="programlisting"> |
681 Eg 3: |
228 >>> 4321*567890 |
682 </p> |
|
683 <pre class="programlisting"> |
|
684 >>> 4321*567890 |
|
685 2453852690L |
229 2453852690L |
686 >>> |
230 >>></pre> |
687 </pre> |
231 <pre class="programlisting"> |
688 <pre class="programlisting"> |
232 Note: Notice the 'L' at the end of the output. The 'L' signifies that the |
689 Note: Notice the 'L' at the end of the output. The 'L' signifies that the |
|
690 output of the operation is of type *long*. It was absent in the previous |
233 output of the operation is of type *long*. It was absent in the previous |
691 example because we used the print statement. This is because *print* formats |
234 example because we used the print statement. This is because *print* formats |
692 the output before displaying. |
235 the output before displaying.</pre> |
693 </pre> |
236 <p id="ch2intro_20">Eg 4:</p> |
694 <p id="ch2intro_20"> |
237 <pre class="programlisting"> |
695 Eg 4: |
238 >>> big = 12345678901234567890 ** 3 |
696 </p> |
|
697 <pre class="programlisting"> |
|
698 >>> big = 12345678901234567890 ** 3 |
|
699 >>> print big |
239 >>> print big |
700 1881676372353657772490265749424677022198701224860897069000 |
240 1881676372353657772490265749424677022198701224860897069000 |
701 >>> |
241 >>></pre> |
702 </pre> |
242 <pre class="programlisting"> |
703 <pre class="programlisting"> |
243 This example is to show that unlike in C or C++ there is no limit on the |
704 This example is to show that unlike in C or C++ there is no limit on the |
244 value of an integer.</pre> |
705 value of an integer. |
245 <p id="ch2intro_21">Try this on the interactive interpreter: |
706 </pre> |
246 <code class="literal">import this</code></p> |
707 <p id="ch2intro_21"> |
247 <p id="ch2intro_22"><span class="emphasis"><em>Hint: The output gives an idea of Power of Python</em></span></p> |
708 Try this on the interactive interpreter: |
248 </div> |
709 <code class="literal"> |
249 <div class="section" title="2.2.ipython - An enhanced interactive Python interpreter"> |
710 import this |
250 <div class="titlepage"><div><div><h3 class="title"> |
711 </code> |
251 <a name="id2966950"></a>2.2.<span class="emphasis"><em>ipython</em></span> - An enhanced interactive Python interpreter</h3></div></div></div> |
712 </p> |
252 <p id="ch2intro_23">The power and the importance of the interactive interpreter was the highlight |
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="id2817788"> |
|
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 |
|
741 of the previous section. This section provides insight into the enhanced |
253 of the previous section. This section provides insight into the enhanced |
742 interpreter with more advanced set of features called |
254 interpreter with more advanced set of features called <span class="strong"><strong>ipython</strong></span>. Entering |
743 <span class="strong"> |
255 <span class="emphasis"><em>ipython</em></span> at the shell prompt fires up the interactive interpreter.</p> |
744 <strong> |
256 <pre class="programlisting"> |
745 ipython |
257 $ 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 |
|
758 Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) |
258 Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) |
759 Type "copyright", "credits" or "license" for more information. |
259 Type "copyright", "credits" or "license" for more information. |
760 |
260 |
761 IPython 0.8.4 -- An enhanced Interactive Python. |
261 IPython 0.8.4 -- An enhanced Interactive Python. |
762 ? -> Introduction and overview of IPython's features. |
262 ? -> Introduction and overview of IPython's features. |
763 %quickref -> Quick reference. |
263 %quickref -> Quick reference. |
764 help -> Python's own help system. |
264 help -> Python's own help system. |
765 object? -> Details about 'object'. ?object also works, ?? prints more. |
265 object? -> Details about 'object'. ?object also works, ?? prints more. |
766 |
266 |
767 In [1]: |
267 In [1]:</pre> |
768 </pre> |
268 <p id="ch2intro_24">This is the output obtained upon firing ipython. The exact appearance may |
769 <p id="ch2intro_24"> |
|
770 This is the output obtained upon firing ipython. The exact appearance may |
|
771 change based on the Python version installed. The following are some of the |
269 change based on the Python version installed. The following are some of the |
772 various features provided by |
270 various features provided by <span class="strong"><strong>ipython</strong></span>:</p> |
773 <span class="strong"> |
271 <p id="ch2intro_25">Suggestions - ipython provides suggestions of the possible methods and |
774 <strong> |
272 operations available for the given python object.</p> |
775 ipython |
273 <p id="ch2intro_26">Eg 5:</p> |
776 </strong> |
274 <pre class="programlisting"> |
777 </span> |
275 In [4]: a = 6 |
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 |
|
789 |
276 |
790 In [5]: a. |
277 In [5]: a. |
791 a.__abs__ a.__divmod__ a.__index__ a.__neg__ a.__rand__ a.__rmod__ a.__rxor__ |
278 a.__abs__ a.__divmod__ a.__index__ a.__neg__ a.__rand__ a.__rmod__ a.__rxor__ |
792 a.__add__ a.__doc__ a.__init__ a.__new__ a.__rdiv__ a.__rmul__ a.__setattr__ |
279 a.__add__ a.__doc__ a.__init__ a.__new__ a.__rdiv__ a.__rmul__ a.__setattr__ |
793 a.__and__ a.__float__ a.__int__ a.__nonzero__ a.__rdivmod__ a.__ror__ a.__str__ |
280 a.__and__ a.__float__ a.__int__ a.__nonzero__ a.__rdivmod__ a.__ror__ a.__str__ |
794 a.__class__ a.__floordiv__ a.__invert__ a.__oct__ a.__reduce__ a.__rpow__ a.__sub__ |
281 a.__class__ a.__floordiv__ a.__invert__ a.__oct__ a.__reduce__ a.__rpow__ a.__sub__ |
795 a.__cmp__ a.__getattribute__ a.__long__ a.__or__ a.__reduce_ex__ a.__rrshift__ a.__truediv__ |
282 a.__cmp__ a.__getattribute__ a.__long__ a.__or__ a.__reduce_ex__ a.__rrshift__ a.__truediv__ |
796 a.__coerce__ a.__getnewargs__ a.__lshift__ a.__pos__ a.__repr__ a.__rshift__ a.__xor__ |
283 a.__coerce__ a.__getnewargs__ a.__lshift__ a.__pos__ a.__repr__ a.__rshift__ a.__xor__ |
797 a.__delattr__ a.__hash__ a.__mod__ a.__pow__ a.__rfloordiv__ a.__rsub__ |
284 a.__delattr__ a.__hash__ a.__mod__ a.__pow__ a.__rfloordiv__ a.__rsub__ |
798 a.__div__ a.__hex__ a.__mul__ a.__radd__ a.__rlshift__ a.__rtruediv__ |
285 a.__div__ a.__hex__ a.__mul__ a.__radd__ a.__rlshift__ a.__rtruediv__</pre> |
799 </pre> |
286 <p id="ch2intro_27">In this example, we initialized 'a' (a variable - a concept that will be |
800 <p id="ch2intro_27"> |
287 discussed in the subsequent sections.) to 6. In the next line when the <span class="emphasis"><em>tab</em></span> key |
801 In this example, we initialized 'a' (a variable - a concept that will be |
288 is pressed after typing '<span class="emphasis"><em>a.</em></span>' ipython displays the set of all possible methods |
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 |
|
816 that are applicable on the object 'a' (an integer in this context). Ipython |
289 that are applicable on the object 'a' (an integer in this context). Ipython |
817 provides many such datatype specific features which will be presented in the |
290 provides many such datatype specific features which will be presented in the |
818 further sections as and when the datatypes are introduced. |
291 further sections as and when the datatypes are introduced.</p> |
819 </p> |
292 </div> |
820 </div> |
293 </div> |
821 </div> |
294 <div class="section" title="3.Editing and running a python file"> |
822 <div class="section" title="3.Editing and running a python file"> |
295 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
823 <div class="titlepage"> |
296 <a name="id2967075"></a>3.Editing and running a python file</h2></div></div></div> |
824 <div> |
297 <p id="ch2intro_28">The previous sections focused on the use of the interpreter to run python code. |
825 <div> |
|
826 <h2 class="title" style="clear: both"> |
|
827 <a name="id2817912"> |
|
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. |
|
836 While the interpeter is an excellent tool to test simple solutions and |
298 While the interpeter is an excellent tool to test simple solutions and |
837 experiment with small code snippets, its main disadvantage is that everything |
299 experiment with small code snippets, its main disadvantage is that everything |
838 written in the interpreter is lost once its quit. Most of the times a program is |
300 written in the interpreter is lost once its quit. Most of the times a program is |
839 used by people other than the author. So the programs have to be available in |
301 used by people other than the author. So the programs have to be available in |
840 some form suitable for distribution, and hence they are written in files. This |
302 some form suitable for distribution, and hence they are written in files. This |
841 section will focus on editing and running python files. Start by opening a text |
303 section will focus on editing and running python files. Start by opening a text |
842 editor ( it is recommended you choose one from the list at the top of this page ). |
304 editor ( it is recommended you choose one from the list at the top of this page ). |
843 In the editor type down python code and save the file with an extension |
305 In the editor type down python code and save the file with an extension <span class="strong"><strong>.py</strong></span> |
844 <span class="strong"> |
306 (python files have an extension of .py). Once done with the editing, save the |
845 <strong> |
307 file and exit the editor.</p> |
846 .py |
308 <p id="ch2intro_29">Let us look at a simple example of calculating the gcd of 2 numbers using Python:</p> |
847 </strong> |
309 <p id="ch2intro_2a"><span class="strong"><strong>Creating the first python script(file)</strong></span></p> |
848 </span> |
310 <pre class="programlisting"> |
849 (python files have an extension of .py). Once done with the editing, save the |
311 $ emacs gcd.py |
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 |
|
864 def gcd(x,y): |
312 def gcd(x,y): |
865 if x % y == 0: |
313 if x % y == 0: |
866 return y |
314 return y |
867 return gcd(y, x%y) |
315 return gcd(y, x%y) |
868 |
316 |
869 print gcd(72, 92) |
317 print gcd(72, 92)</pre> |
870 </pre> |
318 <p id="ch2intro_2b">To run the script, open the shell prompt, navigate to the directory that |
871 <p id="ch2intro_2b"> |
319 contains the python file and run <code class="literal">python <filename.py></code> at the prompt ( in this |
872 To run the script, open the shell prompt, navigate to the directory that |
320 case filename is gcd.py )</p> |
873 contains the python file and run |
321 <p id="ch2intro_2c"><span class="strong"><strong>Running the python script</strong></span></p> |
874 <code class="literal"> |
322 <pre class="programlisting"> |
875 python <filename.py> |
323 $ python gcd.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 |
|
889 4 |
324 4 |
890 $ |
325 $</pre> |
891 </pre> |
326 <p id="ch2intro_2d">Another method to run a python script would be to include the line</p> |
892 <p id="ch2intro_2d"> |
327 <p id="ch2intro_2e"><code class="literal">#! /usr/bin/python</code></p> |
893 Another method to run a python script would be to include the line |
328 <p id="ch2intro_2f">at the beginning of the python file and then make the file executable by</p> |
894 </p> |
329 <p id="ch2intro_30">$ chmod a+x <span class="emphasis"><em>filename.py</em></span></p> |
895 <p id="ch2intro_2e"> |
330 <p id="ch2intro_31">Once this is done, the script can be run as a standalone program as follows:</p> |
896 <code class="literal"> |
331 <p id="ch2intro_32">$ ./<span class="emphasis"><em>filename.py</em></span></p> |
897 #! /usr/bin/python |
332 </div> |
898 </code> |
333 <div class="section" title="4.Basic Datatypes and operators in Python"> |
899 </p> |
334 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
900 <p id="ch2intro_2f"> |
335 <a name="id2967182"></a>4.Basic Datatypes and operators in Python</h2></div></div></div> |
901 at the beginning of the python file and then make the file executable by |
336 <p id="ch2intro_33">Python provides the following set of basic datatypes.</p> |
902 </p> |
337 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
903 <p id="ch2intro_30"> |
338 <li class="listitem" style="list-style-type: *"><p id="ch2intro_34">Numbers: int, float, long, complex</p></li> |
904 $ chmod a+x |
339 <li class="listitem" style="list-style-type: *"><p id="ch2intro_35">Strings</p></li> |
905 <span class="emphasis"> |
340 <li class="listitem" style="list-style-type: *"><p id="ch2intro_36">Boolean</p></li> |
906 <em> |
341 </ul></div> |
907 filename.py |
342 <div class="section" title="4.1.Numbers"> |
908 </em> |
343 <div class="titlepage"><div><div><h3 class="title"> |
909 </span> |
344 <a name="id2967216"></a>4.1.Numbers</h3></div></div></div> |
910 </p> |
345 <p id="ch2intro_37">Numbers were introduced in the examples presented in the interactive interpreter |
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="id2818019"> |
|
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="id2818053"> |
|
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 |
|
971 section. Numbers include types as mentioned earlier viz., int (integers), float |
346 section. Numbers include types as mentioned earlier viz., int (integers), float |
972 (floating point numbers), long (large integers), complex (complex numbers with |
347 (floating point numbers), long (large integers), complex (complex numbers with |
973 real and imaginary parts). Python is not a strongly typed language, which means |
348 real and imaginary parts). Python is not a strongly typed language, which means |
974 the type of a variable need not mentioned during its initialization. Let us look |
349 the type of a variable need not mentioned during its initialization. Let us look |
975 at a few examples. |
350 at a few examples.</p> |
976 </p> |
351 <p id="ch2intro_38">Eg 6:</p> |
977 <p id="ch2intro_38"> |
352 <pre class="programlisting"> |
978 Eg 6: |
353 >>> a = 1 #here a is an integer variable</pre> |
979 </p> |
354 <p id="ch2intro_39">Eg 7:</p> |
980 <pre class="programlisting"> |
355 <pre class="programlisting"> |
981 >>> a = 1 #here a is an integer variable |
356 >>> lng = 122333444455555666666777777788888888999999999 #here lng is a variable of type long |
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 |
|
988 >>> lng |
357 >>> lng |
989 122333444455555666666777777788888888999999999L #notice the trailing 'L' |
358 122333444455555666666777777788888888999999999L #notice the trailing 'L' |
990 >>> print lng |
359 >>> print lng |
991 122333444455555666666777777788888888999999999 #notice the absence of the trailing 'L' |
360 122333444455555666666777777788888888999999999 #notice the absence of the trailing 'L' |
992 >>> lng+1 |
361 >>> lng+1 |
993 122333444455555666666777777788888889000000000L |
362 122333444455555666666777777788888889000000000L</pre> |
994 </pre> |
363 <p id="ch2intro_3a">Long numbers are the same as integers in almost all aspects. They can be used in |
995 <p id="ch2intro_3a"> |
|
996 Long numbers are the same as integers in almost all aspects. They can be used in |
|
997 operations just like integers and along with integers without any distinction. |
364 operations just like integers and along with integers without any distinction. |
998 The only distinction comes during type checking (which is not a healthy practice). |
365 The only distinction comes during type checking (which is not a healthy practice). |
999 Long numbers are tucked with a trailing 'L' just to signify that they are long. |
366 Long numbers are tucked with a trailing 'L' just to signify that they are long. |
1000 Notice that in the example just lng at the prompt displays the value of the variable |
367 Notice that in the example just lng at the prompt displays the value of the variable |
1001 with the 'L' whereas |
368 with the 'L' whereas <code class="literal">print lng</code> displays without the 'L'. This is because print |
1002 <code class="literal"> |
|
1003 print lng |
|
1004 </code> |
|
1005 displays without the 'L'. This is because print |
|
1006 formats the output before printing. Also in the example, notice that adding an |
369 formats the output before printing. Also in the example, notice that adding an |
1007 integer to a long does not give any errors and the result is as expected. So for |
370 integer to a long does not give any errors and the result is as expected. So for |
1008 all practical purposes longs can be treated as ints. |
371 all practical purposes longs can be treated as ints.</p> |
1009 </p> |
372 <p id="ch2intro_3b">Eg 8:</p> |
1010 <p id="ch2intro_3b"> |
373 <pre class="programlisting"> |
1011 Eg 8: |
374 >>> fl = 3.14159 #fl is a float variable |
1012 </p> |
|
1013 <pre class="programlisting"> |
|
1014 >>> fl = 3.14159 #fl is a float variable |
|
1015 >>> e = 1.234e-4 #e is also a float variable, specified in the exponential form |
375 >>> e = 1.234e-4 #e is also a float variable, specified in the exponential form |
1016 >>> a = 1 |
376 >>> a = 1 |
1017 >>> b = 2 |
377 >>> b = 2 |
1018 >>> a/b #integer division |
378 >>> a/b #integer division |
1019 0 |
379 0 |
1020 >>> a/fl #floating point division |
380 >>> a/fl #floating point division |
1021 0.31831015504887655 |
381 0.31831015504887655 |
1022 >>> e/fl |
382 >>> e/fl |
1023 3.9279473133031364e-05 |
383 3.9279473133031364e-05</pre> |
1024 </pre> |
384 <p id="ch2intro_3c">Floating point numbers, simply called floats are real numbers with a decimal point. |
1025 <p id="ch2intro_3c"> |
|
1026 Floating point numbers, simply called floats are real numbers with a decimal point. |
|
1027 The example above shows the initialization of a float variable. Shown also in this |
385 The example above shows the initialization of a float variable. Shown also in this |
1028 example is the difference between integer division and floating point division. |
386 example is the difference between integer division and floating point division. |
1029 'a' and 'b' here are integer variables and hence the division gives 0 as the quotient. |
387 'a' and 'b' here are integer variables and hence the division gives 0 as the quotient. |
1030 When either of the operands is a float, the operation is a floating point division, |
388 When either of the operands is a float, the operation is a floating point division, |
1031 and the result is also a float as illustrated. |
389 and the result is also a float as illustrated.</p> |
1032 </p> |
390 <p id="ch2intro_3d">Eg 9:</p> |
1033 <p id="ch2intro_3d"> |
391 <pre class="programlisting"> |
1034 Eg 9: |
392 >>> cplx = 3 + 4j #cplx is a complex variable |
1035 </p> |
|
1036 <pre class="programlisting"> |
|
1037 >>> cplx = 3 + 4j #cplx is a complex variable |
|
1038 >>> cplx |
393 >>> cplx |
1039 (3+4j) |
394 (3+4j) |
1040 >>> print cplx.real #prints the real part of the complex number |
395 >>> print cplx.real #prints the real part of the complex number |
1041 3.0 |
396 3.0 |
1042 >>> print cplx.imag #prints the imaginary part of the complex number |
397 >>> print cplx.imag #prints the imaginary part of the complex number |
1043 4.0 |
398 4.0 |
1044 >>> print cplx*fl #multiplies the real and imag parts of the complex number with the multiplier |
399 >>> print cplx*fl #multiplies the real and imag parts of the complex number with the multiplier |
1045 (9.42477+12.56636j) |
400 (9.42477+12.56636j) |
1046 >>> abs(cplx) #returns the absolute value of the complex number |
401 >>> abs(cplx) #returns the absolute value of the complex number |
1047 5.0 |
402 5.0</pre> |
1048 </pre> |
403 <p id="ch2intro_3e">Python provides a datatype for complex numbers. Complex numbers are initialized |
1049 <p id="ch2intro_3e"> |
404 as shown in the example above. The <span class="emphasis"><em>real</em></span> and <span class="emphasis"><em>imag</em></span> operators return the real and |
1050 Python provides a datatype for complex numbers. Complex numbers are initialized |
405 imaginary parts of the complex number as shown. The <span class="emphasis"><em>abs()</em></span> returns the absolute |
1051 as shown in the example above. The |
406 value of the complex number.</p> |
1052 <span class="emphasis"> |
407 </div> |
1053 <em> |
408 <div class="section" title="4.2.Variables"> |
1054 real |
409 <div class="titlepage"><div><div><h3 class="title"> |
1055 </em> |
410 <a name="id2967346"></a>4.2.Variables</h3></div></div></div> |
1056 </span> |
411 <p id="ch2intro_3f">Variables are just names that represent a value. Variables have already been |
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="id2818183"> |
|
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 |
|
1088 introduced in the various examples from the previous sections. Certain rules about |
412 introduced in the various examples from the previous sections. Certain rules about |
1089 using variables: |
413 using variables:</p> |
1090 </p> |
414 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
1091 <div class="itemizedlist"> |
415 <li class="listitem" style="list-style-type: *"><p id="ch2intro_40">Variables have to be initialized or assigned a value before being used.</p></li> |
1092 <ul class="itemizedlist" type="*"> |
416 <li class="listitem" style="list-style-type: *"><p id="ch2intro_41">Variable names can consist of letters, digits and underscores(_).</p></li> |
1093 <li class="listitem" style="list-style-type: *"> |
417 <li class="listitem" style="list-style-type: *"><p id="ch2intro_42">Variable names cannot begin with digits, but can contain digits in them.</p></li> |
1094 <p id="ch2intro_40"> |
418 </ul></div> |
1095 Variables have to be initialized or assigned a value before being used. |
419 <p id="ch2intro_43">In reference to the previous section examples, 'a', 'b', 'lng', 'fl', 'e' and 'cplx' |
1096 </p> |
420 are all variables of various datatypes.</p> |
1097 </li> |
421 <pre class="programlisting"> |
1098 <li class="listitem" style="list-style-type: *"> |
422 Note: Python is not a strongly typed language and hence an integer variable can at a |
1099 <p id="ch2intro_41"> |
423 later stage be used as a float variable as well.</pre> |
1100 Variable names can consist of letters, digits and underscores(_). |
424 </div> |
1101 </p> |
425 <div class="section" title="4.3.Strings"> |
1102 </li> |
426 <div class="titlepage"><div><div><h3 class="title"> |
1103 <li class="listitem" style="list-style-type: *"> |
427 <a name="id2967394"></a>4.3.Strings</h3></div></div></div> |
1104 <p id="ch2intro_42"> |
428 <p id="ch2intro_44">Strings are one of the essential data structures of any programming language. |
1105 Variable names cannot begin with digits, but can contain digits in them. |
429 The <code class="literal">print "Hello, World!"</code> program was introduced in the earlier section, and |
1106 </p> |
430 the <span class="emphasis"><em>"Hello, World!"</em></span> in the print statement is a string. A string is basically |
1107 </li> |
431 a set of characters. Strings can be represented in various ways shown below:</p> |
1108 </ul> |
432 <pre class="programlisting"> |
1109 </div> |
433 s = 'this is a string' # a string variable can be represented using single quotes |
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="id2818231"> |
|
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 |
|
1149 s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown |
434 s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown |
1150 s = "I have 'single-quotes' inside!" |
435 s = "I have 'single-quotes' inside!" |
1151 l = "A string spanning many lines |
436 l = "A string spanning many lines |
1152 one more line |
437 one more line |
1153 yet another" # a string can span more than a single line. |
438 yet another" # a string can span more than a single line. |
1154 t = """A triple quoted string does # another way of representing multiline strings. |
439 t = """A triple quoted string does # another way of representing multiline strings. |
1155 not need to be escaped at the end and |
440 not need to be escaped at the end and |
1156 "can have nested quotes" etc.""" |
441 "can have nested quotes" etc."""</pre> |
1157 </pre> |
442 <p id="ch2intro_45">Try the following on the interpreter: |
1158 <p id="ch2intro_45"> |
443 <code class="literal">s = 'this is a string with 'quotes' of similar kind'</code></p> |
1159 Try the following on the interpreter: |
444 <p id="ch2intro_46"><span class="strong"><strong>Exercise: How to use single quotes within single quotes in a string as shown |
1160 <code class="literal"> |
445 in the above example without getting an error?</strong></span></p> |
1161 s = 'this is a string with 'quotes' of similar kind' |
446 <div class="section" title="4.3.1.String operations"> |
1162 </code> |
447 <div class="titlepage"><div><div><h4 class="title"> |
1163 </p> |
448 <a name="id2967445"></a>4.3.1.String operations</h4></div></div></div> |
1164 <p id="ch2intro_46"> |
449 <p id="ch2intro_47">A few basic string operations are presented here.</p> |
1165 <span class="strong"> |
450 <p id="ch2intro_48"><span class="strong"><strong>String concatenation</strong></span> |
1166 <strong> |
451 String concatenation is done by simple addition of two strings.</p> |
1167 Exercise: How to use single quotes within single quotes in a string as shown |
452 <pre class="programlisting"> |
1168 in the above example without getting an error? |
453 >>> x = 'Hello' |
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="id2818283"> |
|
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' |
|
1197 >>> y = ' Python' |
454 >>> y = ' Python' |
1198 >>> print x+y |
455 >>> print x+y |
1199 Hello Python |
456 Hello Python</pre> |
1200 </pre> |
457 <p id="ch2intro_49"><span class="emphasis"><em>Try this yourself:</em></span></p> |
1201 <p id="ch2intro_49"> |
458 <pre class="programlisting"> |
1202 <span class="emphasis"> |
459 >>> somenum = 13 |
1203 <em> |
460 >>> print x+somenum</pre> |
1204 Try this yourself: |
461 <p id="ch2intro_4a">The problem with the above example is that here a string variable and an integer |
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 |
|
1214 variable are trying to be concantenated. To obtain the desired result from the |
462 variable are trying to be concantenated. To obtain the desired result from the |
1215 above example the str(), repr() and the `` can be used. |
463 above example the str(), repr() and the `` can be used.</p> |
1216 </p> |
464 <p id="ch2intro_4b"><span class="strong"><strong>str()</strong></span> simply converts a value to a string in a reasonable form. |
1217 <p id="ch2intro_4b"> |
465 <span class="strong"><strong>repr()</strong></span> creates a string that is a representation of the value.</p> |
1218 <span class="strong"> |
466 <p id="ch2intro_4c">The difference can be seen in the example shown below:</p> |
1219 <strong> |
467 <pre class="programlisting"> |
1220 str() |
468 >>> str(1000000000000000000000000000000000000000000000000L) |
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) |
|
1236 '1000000000000000000000000000000000000000000000000' |
469 '1000000000000000000000000000000000000000000000000' |
1237 >>> repr(1000000000000000000000000000000000000000000000000L) |
470 >>> repr(1000000000000000000000000000000000000000000000000L) |
1238 '1000000000000000000000000000000000000000000000000L' |
471 '1000000000000000000000000000000000000000000000000L'</pre> |
1239 </pre> |
472 <p id="ch2intro_4d">It can be observed that the 'L' in the long value shown was omitted by str(), |
1240 <p id="ch2intro_4d"> |
|
1241 It can be observed that the 'L' in the long value shown was omitted by str(), |
|
1242 whereas repr() converted that into a string too. An alternative way of using |
473 whereas repr() converted that into a string too. An alternative way of using |
1243 repr(value) is |
474 repr(value) is <code class="literal">`value`</code>.</p> |
1244 <code class="literal"> |
475 <p id="ch2intro_4e">A few more examples:</p> |
1245 `value` |
476 <pre class="programlisting"> |
1246 </code> |
477 >>> x = "Let's go nto Pycon" |
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" |
|
1254 >>> print x |
478 >>> print x |
1255 Let's go |
479 Let's go |
1256 to Pycon |
480 to Pycon</pre> |
1257 </pre> |
481 <p id="ch2intro_4f">In the above example, notice that the 'n'(newline) character is formatted and |
1258 <p id="ch2intro_4f"> |
|
1259 In the above example, notice that the 'n'(newline) character is formatted and |
|
1260 the string is printed on two lines. The strings discussed until now were normal |
482 the string is printed on two lines. The strings discussed until now were normal |
1261 strings. Other than these there are two other types of strings namely, raw strings |
483 strings. Other than these there are two other types of strings namely, raw strings |
1262 and unicode strings. |
484 and unicode strings.</p> |
1263 </p> |
485 <p id="ch2intro_50"><span class="strong"><strong>Raw strings</strong></span> are strings which are unformatted, that is the backslashes() are |
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 |
|
1271 not parsed and are left as it is in the string. Raw strings are represented with |
486 not parsed and are left as it is in the string. Raw strings are represented with |
1272 an 'r' at the start of a string. |
487 an 'r' at the start of a string. |
1273 Let us look at an example |
488 Let us look at an example</p> |
1274 </p> |
489 <pre class="programlisting"> |
1275 <pre class="programlisting"> |
490 >>> x = r"Let's go nto Pycon" |
1276 >>> x = r"Let's go nto Pycon" |
|
1277 >>> print x |
491 >>> print x |
1278 Let's go nto Pycon |
492 Let's go nto Pycon</pre> |
1279 </pre> |
493 <p id="ch2intro_51">Note: The 'n' is not being parsed into a new line and is left as it is.</p> |
1280 <p id="ch2intro_51"> |
494 <p id="ch2intro_52"><span class="emphasis"><em>Try this yourself:</em></span></p> |
1281 Note: The 'n' is not being parsed into a new line and is left as it is. |
495 <pre class="programlisting"> |
1282 </p> |
496 >>> x = r"Let's go to Pycon "</pre> |
1283 <p id="ch2intro_52"> |
497 <p id="ch2intro_53"><span class="strong"><strong>Unicode strings</strong></span> are strings where the characters are Unicode characters as |
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 |
|
1300 opposed to ASCII characters. Unicode strings are represented with a 'u' at the |
498 opposed to ASCII characters. Unicode strings are represented with a 'u' at the |
1301 start of the string. |
499 start of the string. |
1302 Let us look at an example: |
500 Let us look at an example:</p> |
1303 </p> |
501 <pre class="programlisting"> |
1304 <pre class="programlisting"> |
502 >>> x = u"Let's go to Pycon!" |
1305 >>> x = u"Let's go to Pycon!" |
|
1306 >>> print x |
503 >>> print x |
1307 Let's go to Pycon! |
504 Let's go to Pycon!</pre> |
1308 </pre> |
505 </div> |
1309 </div> |
506 </div> |
1310 </div> |
507 <div class="section" title="4.4.Boolean"> |
1311 <div class="section" title="4.4.Boolean"> |
508 <div class="titlepage"><div><div><h3 class="title"> |
1312 <div class="titlepage"> |
509 <a name="id2967588"></a>4.4.Boolean</h3></div></div></div> |
1313 <div> |
510 <p id="ch2intro_54">Python also provides special Boolean datatype. A boolean variable can assume a |
1314 <div> |
511 value of either <span class="emphasis"><em>True</em></span> or <span class="emphasis"><em>False</em></span> (Note the capitalizations).</p> |
1315 <h3 class="title"> |
512 <p id="ch2intro_55">Let us look at examples:</p> |
1316 <a name="id2818425"> |
513 <pre class="programlisting"> |
1317 </a> |
514 >>> t = True |
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 |
|
1344 >>> f = not t |
515 >>> f = not t |
1345 >>> print f |
516 >>> print f |
1346 False |
517 False |
1347 >>> f or t |
518 >>> f or t |
1348 True |
519 True |
1349 >>> f and t |
520 >>> f and t |
1350 False |
521 False</pre> |
1351 </pre> |
522 </div> |
1352 </div> |
523 </div> |
1353 </div> |
524 <div class="section" title="5.The while loop"> |
1354 <div class="section" title="5.The while loop"> |
525 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1355 <div class="titlepage"> |
526 <a name="id2967617"></a>5.The <span class="strong"><strong>while</strong></span> loop</h2></div></div></div> |
1356 <div> |
527 <p id="ch2intro_56">The Python <span class="strong"><strong>while</strong></span> loop is similar to the C/C++ while loop. The syntax is as |
1357 <div> |
528 follows:</p> |
1358 <h2 class="title" style="clear: both"> |
529 <pre class="programlisting"> |
1359 <a name="id2818454"> |
530 statement 0 |
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 |
|
1384 while condition: |
531 while condition: |
1385 statement 1 #while block |
532 statement 1 #while block |
1386 statement 2 #while block |
533 statement 2 #while block |
1387 statement 3 #outside the while block. |
534 statement 3 #outside the while block.</pre> |
1388 </pre> |
535 <p id="ch2intro_57">Let us look at an example:</p> |
1389 <p id="ch2intro_57"> |
536 <pre class="programlisting"> |
1390 Let us look at an example: |
537 >>> x = 1 |
1391 </p> |
|
1392 <pre class="programlisting"> |
|
1393 >>> x = 1 |
|
1394 >>> while x <= 5: |
538 >>> while x <= 5: |
1395 ... print x |
539 ... print x |
1396 ... x += 1 |
540 ... x += 1 |
1397 ... |
541 ... |
1398 1 |
542 1 |
1399 2 |
543 2 |
1400 3 |
544 3 |
1401 4 |
545 4 |
1402 5 |
546 5</pre> |
1403 </pre> |
547 </div> |
1404 </div> |
548 <div class="section" title="6.The if conditional"> |
1405 <div class="section" title="6.The if conditional"> |
549 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1406 <div class="titlepage"> |
550 <a name="id2967654"></a>6.The <span class="strong"><strong>if</strong></span> conditional</h2></div></div></div> |
1407 <div> |
551 <p id="ch2intro_58">The Python <span class="strong"><strong>if</strong></span> block provides the conditional execution of statements. |
1408 <div> |
|
1409 <h2 class="title" style="clear: both"> |
|
1410 <a name="id2818491"> |
|
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. |
|
1431 If the condition evaluates as true the block of statements defined under the if |
552 If the condition evaluates as true the block of statements defined under the if |
1432 block are executed. |
553 block are executed.</p> |
1433 </p> |
554 <p id="ch2intro_59">If the first block is not executed on account of the condition not being satisfied, |
1434 <p id="ch2intro_59"> |
555 the set of statements in the <span class="strong"><strong>else</strong></span> block are executed.</p> |
1435 If the first block is not executed on account of the condition not being satisfied, |
556 <p id="ch2intro_5a">The <span class="strong"><strong>elif</strong></span> block provides the functionality of evaluation of multiple conditions |
1436 the set of statements in the |
557 as shown in the example.</p> |
1437 <span class="strong"> |
558 <p id="ch2intro_5b">The syntax is as follows:</p> |
1438 <strong> |
559 <pre class="programlisting"> |
1439 else |
560 if condition : |
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 : |
|
1459 statement_1 |
561 statement_1 |
1460 statement_2 |
562 statement_2 |
1461 |
563 |
1462 elif condition: |
564 elif condition: |
1463 statement_3 |
565 statement_3 |
1464 statement_4 |
566 statement_4 |
1465 else: |
567 else: |
1466 statement_5 |
568 statement_5 |
1467 statement_6 |
569 statement_6</pre> |
1468 </pre> |
570 <p id="ch2intro_5c">Let us look at an example:</p> |
1469 <p id="ch2intro_5c"> |
571 <pre class="programlisting"> |
1470 Let us look at an example: |
572 >>> n = raw_input("Input a number:") |
1471 </p> |
|
1472 <pre class="programlisting"> |
|
1473 >>> n = raw_input("Input a number:") |
|
1474 >>> if n < 0: |
573 >>> if n < 0: |
1475 print n," is negative" |
574 print n," is negative" |
1476 elif n > 0: |
575 elif n > 0: |
1477 print n," is positive" |
576 print n," is positive" |
1478 else: |
577 else: |
1479 print n, " is 0" |
578 print n, " is 0"</pre> |
1480 </pre> |
579 </div> |
1481 </div> |
580 <div class="section" title="7.raw_input()"> |
1482 <div class="section" title="7.raw_input()"> |
581 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1483 <div class="titlepage"> |
582 <a name="id2967723"></a>7.<span class="strong"><strong>raw_input()</strong></span> |
1484 <div> |
583 </h2></div></div></div> |
1485 <div> |
584 <p id="ch2intro_5d">In the previous example we saw the call to the raw_input() subroutine. |
1486 <h2 class="title" style="clear: both"> |
585 The <span class="strong"><strong>raw_input()</strong></span> method is used to take user inputs through the console. |
1487 <a name="id2818560"> |
586 Unlike <span class="strong"><strong>input()</strong></span> which assumes the data entered by the user as a standard python |
1488 </a> |
587 expression, <span class="strong"><strong>raw_input()</strong></span> treats all the input data as raw data and converts |
1489 7. |
588 everything into a string. To illustrate this let us look at an example.</p> |
1490 <span class="strong"> |
589 <pre class="programlisting"> |
1491 <strong> |
590 >>> input("Enter a number thats a palindrome:") |
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:") |
|
1526 Enter a number thats a palindrome:121 |
591 Enter a number thats a palindrome:121 |
1527 121 |
592 121 |
1528 |
593 |
1529 >>> input("Enter your name:") |
594 >>> input("Enter your name:") |
1530 Enter your name:PythonFreak |
595 Enter your name:PythonFreak |
1531 Traceback (most recent call last): |
596 Traceback (most recent call last): |
1532 File "<stdin>", line 1, in <module> |
597 File "<stdin>", line 1, in <module> |
1533 File "<string>", line 1, in <module> |
598 File "<string>", line 1, in <module> |
1534 NameError: name 'PythonFreak' is not defined |
599 NameError: name 'PythonFreak' is not defined</pre> |
1535 </pre> |
600 <p id="ch2intro_5e">As shown above the <span class="strong"><strong>input()</strong></span> assumes that the data entered is a valid Python |
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 |
|
1544 expression. In the first call it prompts for an integer input and when entered |
601 expression. In the first call it prompts for an integer input and when entered |
1545 it accepts the integer as an integer, whereas in the second call, when the string |
602 it accepts the integer as an integer, whereas in the second call, when the string |
1546 is entered without the quotes, |
603 is entered without the quotes, <span class="strong"><strong>input()</strong></span> assumes that the entered data is a valid |
1547 <span class="strong"> |
|
1548 <strong> |
|
1549 input() |
|
1550 </strong> |
|
1551 </span> |
|
1552 assumes that the entered data is a valid |
|
1553 Python expression and hence it raises and exception saying PythonFreak is not |
604 Python expression and hence it raises and exception saying PythonFreak is not |
1554 defined. |
605 defined.</p> |
1555 </p> |
606 <pre class="programlisting"> |
1556 <pre class="programlisting"> |
607 >>> input("Enter your name:") |
1557 >>> input("Enter your name:") |
|
1558 Enter your name:'PythonFreak' |
608 Enter your name:'PythonFreak' |
1559 'PythonFreak' |
609 'PythonFreak' |
1560 >>> |
610 >>></pre> |
1561 </pre> |
611 <p id="ch2intro_5f">Here the name is accepted because its entered as a string (within quotes). But |
1562 <p id="ch2intro_5f"> |
|
1563 Here the name is accepted because its entered as a string (within quotes). But |
|
1564 its unreasonable to go on using quotes each time a string is entered. Hence the |
612 its unreasonable to go on using quotes each time a string is entered. Hence the |
1565 alternative is to use |
613 alternative is to use <span class="strong"><strong>raw_input()</strong></span>.</p> |
1566 <span class="strong"> |
614 <p id="ch2intro_60">Let us now look at how <span class="strong"><strong>raw_input()</strong></span> operates with an example.</p> |
1567 <strong> |
615 <pre class="programlisting"> |
1568 raw_input() |
616 >>> raw_input("Enter your name:") |
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:") |
|
1584 Enter your name:PythonFreak |
617 Enter your name:PythonFreak |
1585 'PythonFreak' |
618 'PythonFreak'</pre> |
1586 </pre> |
619 <p id="ch2intro_61">Observe that the <span class="strong"><strong>raw_input()</strong></span> is converting it into a string all by itself.</p> |
1587 <p id="ch2intro_61"> |
620 <pre class="programlisting"> |
1588 Observe that the |
621 >>> pal = raw_input("Enter a number thats a palindrome:") |
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:") |
|
1598 Enter a number thats a palindrome:121 |
622 Enter a number thats a palindrome:121 |
1599 '121' |
623 '121'</pre> |
1600 </pre> |
624 <p id="ch2intro_62">Observe that <span class="strong"><strong>raw_input()</strong></span> is converting the integer 121 also to a string as |
1601 <p id="ch2intro_62"> |
625 '121'. Let us look at another example:</p> |
1602 Observe that |
626 <pre class="programlisting"> |
1603 <span class="strong"> |
627 >>> pal = raw_input("Enter a number thats a palindrome:") |
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:") |
|
1613 Enter a number thats a palindrome:121 |
628 Enter a number thats a palindrome:121 |
1614 >>> pal + 2 |
629 >>> pal + 2 |
1615 Traceback (most recent call last): |
630 Traceback (most recent call last): |
1616 File "<stdin>", line 1, in <module> |
631 File "<stdin>", line 1, in <module> |
1617 TypeError: cannot concatenate 'str' and 'int' objects |
632 TypeError: cannot concatenate 'str' and 'int' objects |
1618 >>> pal |
633 >>> pal |
1619 '121' |
634 '121'</pre> |
1620 </pre> |
635 <p id="ch2intro_63">Observe here that the variable <span class="emphasis"><em>pal</em></span> is a string and hence integer operations |
1621 <p id="ch2intro_63"> |
636 cannot be performed on it. Hence the exception is raised.</p> |
1622 Observe here that the variable |
637 </div> |
1623 <span class="emphasis"> |
638 <div class="section" title="8.int() method"> |
1624 <em> |
639 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1625 pal |
640 <a name="id2967877"></a>8.<span class="strong"><strong>int()</strong></span> method</h2></div></div></div> |
1626 </em> |
641 <p id="ch2intro_64">Generally for computing purposes, the data used is not strings or raw data but |
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="id2818715"> |
|
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 |
|
1652 on integers, floats and similar mathematical data structures. The data obtained |
642 on integers, floats and similar mathematical data structures. The data obtained |
1653 from |
643 from <span class="strong"><strong>raw_input()</strong></span> is raw data in the form of strings. In order to obtain integers |
1654 <span class="strong"> |
644 from strings we use the method <span class="strong"><strong>int()</strong></span>.</p> |
1655 <strong> |
645 <p id="ch2intro_65">Let us look at an example.</p> |
1656 raw_input() |
646 <pre class="programlisting"> |
1657 </strong> |
647 >>> intpal = int(pal) |
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) |
|
1673 >>> intpal |
648 >>> intpal |
1674 121 |
649 121</pre> |
1675 </pre> |
650 <p id="ch2intro_66">In the previous example it was observed that <span class="emphasis"><em>pal</em></span> was a string variable. Here |
1676 <p id="ch2intro_66"> |
651 using the <span class="strong"><strong>int()</strong></span> method the string <span class="emphasis"><em>pal</em></span> was converted to an integer variable.</p> |
1677 In the previous example it was observed that |
652 <p id="ch2intro_67"><span class="emphasis"><em>Try This Yourself:</em></span></p> |
1678 <span class="emphasis"> |
653 <pre class="programlisting"> |
1679 <em> |
654 >>> stringvar = raw_input("Enter a name:") |
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:") |
|
1707 Enter a name:Guido Van Rossum |
655 Enter a name:Guido Van Rossum |
1708 >>> stringvar |
656 >>> stringvar |
1709 'Guido Van Rossum' |
657 'Guido Van Rossum' |
1710 >>> numvar = int(stringvar) |
658 >>> numvar = int(stringvar)</pre> |
1711 </pre> |
659 </div> |
1712 </div> |
660 </div> |
1713 </div> |
661 </div></body> |
1714 </div> |
|
1715 </body> |
|
1716 </html> |
662 </html> |
|
663 |