1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 %Tutorial slides on Python. |
|
3 % |
|
4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in> |
|
5 % Copyright (c) 2005-2009, Prabhu Ramachandran |
|
6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
7 |
|
8 \documentclass[14pt,compress]{beamer} |
|
9 %\documentclass[draft]{beamer} |
|
10 %\documentclass[compress,handout]{beamer} |
|
11 %\usepackage{pgfpages} |
|
12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] |
|
13 |
|
14 % Modified from: generic-ornate-15min-45min.de.tex |
|
15 \mode<presentation> |
|
16 { |
|
17 \usetheme{Warsaw} |
|
18 \useoutertheme{split} |
|
19 \setbeamercovered{transparent} |
|
20 } |
|
21 |
|
22 \usepackage[english]{babel} |
|
23 \usepackage[latin1]{inputenc} |
|
24 %\usepackage{times} |
|
25 \usepackage[T1]{fontenc} |
|
26 |
|
27 % Taken from Fernando's slides. |
|
28 \usepackage{ae,aecompl} |
|
29 \usepackage{mathpazo,courier,euler} |
|
30 \usepackage[scaled=.95]{helvet} |
|
31 |
|
32 \definecolor{darkgreen}{rgb}{0,0.5,0} |
|
33 |
|
34 \usepackage{listings} |
|
35 \lstset{language=Python, |
|
36 basicstyle=\ttfamily\bfseries, |
|
37 commentstyle=\color{red}\itshape, |
|
38 stringstyle=\color{darkgreen}, |
|
39 showstringspaces=false, |
|
40 keywordstyle=\color{blue}\bfseries} |
|
41 |
|
42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
43 % Macros |
|
44 \setbeamercolor{emphbar}{bg=blue!20, fg=black} |
|
45 \newcommand{\emphbar}[1] |
|
46 {\begin{beamercolorbox}[rounded=true]{emphbar} |
|
47 {#1} |
|
48 \end{beamercolorbox} |
|
49 } |
|
50 \newcounter{time} |
|
51 \setcounter{time}{0} |
|
52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}} |
|
53 |
|
54 \newcommand{\typ}[1]{\texttt{#1}} |
|
55 |
|
56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } |
|
57 |
|
58 %%% This is from Fernando's setup. |
|
59 % \usepackage{color} |
|
60 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2} |
|
61 % % Use and configure listings package for nicely formatted code |
|
62 % \usepackage{listings} |
|
63 % \lstset{ |
|
64 % language=Python, |
|
65 % basicstyle=\small\ttfamily, |
|
66 % commentstyle=\ttfamily\color{blue}, |
|
67 % stringstyle=\ttfamily\color{orange}, |
|
68 % showstringspaces=false, |
|
69 % breaklines=true, |
|
70 % postbreak = \space\dots |
|
71 % } |
|
72 |
|
73 |
|
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
75 % Title page |
|
76 \title[Basic Python]{Python:\\A great programming toolkit} |
|
77 |
|
78 \author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran} |
|
79 |
|
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
81 \date[] {10, October 2009\\Day 1, Session 1} |
|
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
83 |
|
84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} |
|
85 %\logo{\pgfuseimage{iitmlogo}} |
|
86 |
|
87 |
|
88 %% Delete this, if you do not want the table of contents to pop up at |
|
89 %% the beginning of each subsection: |
|
90 \AtBeginSubsection[] |
|
91 { |
|
92 \begin{frame}<beamer> |
|
93 \frametitle{Outline} |
|
94 \tableofcontents[currentsection,currentsubsection] |
|
95 \end{frame} |
|
96 } |
|
97 |
|
98 |
|
99 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
100 % the following command: |
|
101 %\beamerdefaultoverlayspecification{<+->} |
|
102 |
|
103 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
104 |
|
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
106 % DOCUMENT STARTS |
|
107 \begin{document} |
|
108 |
|
109 \begin{frame} |
|
110 \titlepage |
|
111 \end{frame} |
|
112 \begin{frame} |
|
113 {Acknowledgements} |
|
114 \begin{center} |
|
115 This program is conducted by\\ |
|
116 IIT, Bombay\\ |
|
117 as part of the open source initiatives\\ |
|
118 under the aegis of\\ |
|
119 \alert{National Mission on Education through ICT,} \\ |
|
120 Ministry of HRD. |
|
121 \end{center} |
|
122 \end{frame} |
|
123 |
|
124 \begin{frame} |
|
125 \frametitle{Outline} |
|
126 \tableofcontents |
|
127 % You might wish to add the option [pausesections] |
|
128 \end{frame} |
|
129 |
|
130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
131 % TODO |
|
132 % |
|
133 % * Add slide on Python packages (modules) |
|
134 % * Add slides on reference counting. |
|
135 |
|
136 \section{Agenda} |
|
137 \begin{frame}{About the Workshop, Day 1} |
|
138 \begin{description} |
|
139 \item[Day 1, Session 1] Sat 09:30--11:00 |
|
140 \item[Day 1, Session 2] Sat 11:15--12:45 |
|
141 \item[Day 1, Session 3] Sat 13:45--15:15 |
|
142 \item[Day 1, Session 4] Sat 15:30--17:00 |
|
143 \end{description} |
|
144 \end{frame} |
|
145 \begin{frame}{About the Workshop, Day 2} |
|
146 \begin{description} |
|
147 \item[Day 2, Quiz] Sun 09:00--09:30 |
|
148 \item[Day 2, Session 1] Sun 09:30--11:00 |
|
149 \item[Day 2, Session 2] Sun 11:15--12:45 |
|
150 \item[Day 2, Session 3] Sun 13:45--15:15 |
|
151 \item[Day 2, Session 4] Sun 15:30--17:00 |
|
152 \end{description} |
|
153 \end{frame} |
|
154 |
|
155 \begin{frame}{About the Workshop} |
|
156 \begin{block}{Intended Audience} |
|
157 \begin{itemize} |
|
158 \item Engg., Mathematics and Science teachers. |
|
159 \item Interested students from similar streams. |
|
160 \end{itemize} |
|
161 \end{block} |
|
162 |
|
163 \begin{block}{Goal:} |
|
164 Successful participants will be able to |
|
165 \begin{itemize} |
|
166 \item use Python as their scripting and problem solving language. |
|
167 \item train the students to use Python for the same |
|
168 \end{itemize} |
|
169 \end{block} |
|
170 \end{frame} |
|
171 |
|
172 \begin{frame}{Checklist} |
|
173 \begin{block}{python} |
|
174 Type python at the command line. Do you see version 2.5 or later? |
|
175 \end{block} |
|
176 \begin{block}{IPython} |
|
177 Type ipython at the command line. Is it available? |
|
178 \end{block} |
|
179 \begin{block}{Editor} |
|
180 We recommend scite. |
|
181 \end{block} |
|
182 \end{frame} |
|
183 |
|
184 \section{Overview} |
|
185 \begin{frame} |
|
186 \frametitle{Introduction} |
|
187 \begin{itemize} |
|
188 \item Creator and BDFL: Guido van Rossum |
|
189 \item December 1989 |
|
190 \item ``Python'' as in \emph {Monty Python's Flying Circus} |
|
191 \item 2.6.x |
|
192 \item PSF license (like BSD: no strings attached) |
|
193 \item Highly cross platform |
|
194 \item Nokia series 60! |
|
195 \item \alert{Philosophy:} Simple and complete by design |
|
196 \end{itemize} |
|
197 \end{frame} |
|
198 |
|
199 \begin{frame} |
|
200 \frametitle{Resources} |
|
201 \begin{itemize} |
|
202 \item Part of many GNU/Linux distributions |
|
203 \item Web: \url{http://www.python.org} |
|
204 \item Doc: \url{http://www.python.org/doc} |
|
205 \item Free Tutorials: |
|
206 \begin{itemize} |
|
207 \item Official Python tutorial: \url{http://docs.python.org/tut/tut.html} |
|
208 \item Byte of Python: \url{http://www.byteofpython.info/} |
|
209 \item Dive into Python: \url{http://diveintopython.org/} |
|
210 \end{itemize} |
|
211 \end{itemize} |
|
212 \end{frame} |
|
213 |
|
214 \begin{frame} |
|
215 \frametitle{Why Python?} |
|
216 \begin{itemize} |
|
217 \item Readable and easy to use |
|
218 \item High level, interpreted, modular, OO |
|
219 \item Much faster development cycle |
|
220 \item Powerful interactive environment |
|
221 \item Rapid application development |
|
222 \item Rich standard library and modules |
|
223 \item Interfaces well with C++, C and FORTRAN |
|
224 \item \alert{More than a math package $\Rightarrow$ some extra work compared to math packages} |
|
225 \end{itemize} |
|
226 \end{frame} |
|
227 |
|
228 \begin{frame} |
|
229 \frametitle{Use cases} |
|
230 \begin{itemize} |
|
231 \item NASA: Space Shuttle Mission Design |
|
232 \item AstraZeneca: Collaborative Drug Discovery |
|
233 \item ForecastWatch.com: Helps Meteorologists |
|
234 \item Industrial Light \& Magic: Runs on Python |
|
235 \item Zope: Commercial grade Toolkit |
|
236 \item Plone: Professional high feature CMS |
|
237 \item RedHat: install scripts, sys-admin tools |
|
238 \item Django: A great web application framework |
|
239 \item Google: A strong python shop |
|
240 \end{itemize} |
|
241 \end{frame} |
|
242 |
|
243 \begin{frame} |
|
244 \frametitle{To sum up, python is\ldots} |
|
245 \begin{itemize} |
|
246 \item dynamically typed, interpreted $\rightarrow$ rapid testing/prototyping |
|
247 \item powerful, very high level |
|
248 \item has full introspection |
|
249 \item Did we mention powerful? |
|
250 \end{itemize} |
|
251 \begin{block}{But \ldots} |
|
252 may be wanting in performance. specialised resources such as SWIG, \alert{Cython} are available |
|
253 \end{block} |
|
254 \inctime{15} |
|
255 \end{frame} |
|
256 |
|
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
258 % TIME: 10 m, running 10m |
|
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
260 |
|
261 \section{Python} |
|
262 |
|
263 \subsection{Getting Started} |
|
264 |
|
265 \begin{frame}[fragile] |
|
266 \frametitle{At the prompt, type the following} |
|
267 \begin{lstlisting} |
|
268 >>> print 'Hello Python' |
|
269 >>> print 3124 * 126789 |
|
270 >>> 1786 % 12 |
|
271 >>> 3124 * 126789 |
|
272 >>> a = 3124 * 126789 |
|
273 >>> big = 12345678901234567890 ** 3 |
|
274 >>> verybig = big * big * big * big |
|
275 >>> 12345**6, 12345**67, 12345**678 |
|
276 \end{lstlisting} |
|
277 \end{frame} |
|
278 |
|
279 \begin{frame}[fragile] |
|
280 \frametitle{At the prompt, type the following} |
|
281 \begin{lstlisting} |
|
282 >>> s = 'Hello ' |
|
283 >>> p = 'World' |
|
284 >>> s + p |
|
285 >>> s * 12 |
|
286 >>> s * s |
|
287 >>> s + p * 12, (s + p)* 12 |
|
288 >>> s * 12 + p * 12 |
|
289 >>> 12 * s |
|
290 \end{lstlisting} |
|
291 \end{frame} |
|
292 |
|
293 \begin{frame}[fragile] |
|
294 \frametitle{At the prompt, type the following} |
|
295 \begin{lstlisting} |
|
296 >>> 17/2 |
|
297 >>> 17/2.0 |
|
298 >>> 17.0/2 |
|
299 >>> 17.0/8.5 |
|
300 >>> int(17/2.0) |
|
301 >>> float(17/2) |
|
302 >>> str(17/2.0) |
|
303 >>> round( 7.5 ) |
|
304 \end{lstlisting} |
|
305 \begin{block}{Mini exercise} |
|
306 Round a float to the nearest integer, using \texttt{int()}? |
|
307 \end{block} |
|
308 \end{frame} |
|
309 |
|
310 \begin{frame}\frametitle{Midi exercises} |
|
311 \begin{center} |
|
312 \begin{itemize} |
|
313 \item What does this do? |
|
314 \item \texttt{round(amount * 10) /10.0 } |
|
315 \end{itemize} |
|
316 \end{center} |
|
317 \end{frame} |
|
318 |
|
319 \begin{frame}\frametitle{More exercises} |
|
320 \begin{center} |
|
321 \begin{block}{Round sums} |
|
322 How to round a number to the nearest 5 paise?\\ |
|
323 \begin{description} |
|
324 \item[Remember] 17.23 $\rightarrow$ 17.25,\\ while 17.22 $\rightarrow$ 17.20\\ |
|
325 \end{description} |
|
326 How to round a number to the nearest 20 paise? |
|
327 \end{block} |
|
328 \end{center} |
|
329 \end{frame} |
|
330 |
|
331 \begin{frame}[fragile] \frametitle{A question of good style} |
|
332 \begin{lstlisting} |
|
333 amount = 12.68 |
|
334 denom = 0.05 |
|
335 nCoins = round(amount/denom) |
|
336 rAmount = nCoins * denom |
|
337 \end{lstlisting} |
|
338 \pause |
|
339 \begin{block}{Style Rule \#1} |
|
340 Naming is 80\% of programming |
|
341 \end{block} |
|
342 \end{frame} |
|
343 |
|
344 |
|
345 \begin{frame}[fragile] |
|
346 \frametitle{Odds and ends} |
|
347 \begin{itemize} |
|
348 \item Case sensitive |
|
349 \item Dynamically typed $\Rightarrow$ need not specify a type |
|
350 \begin{lstlisting} |
|
351 a = 1 |
|
352 a = 1.1 |
|
353 a = "Now I am a string!" |
|
354 \end{lstlisting} |
|
355 \item Comments: |
|
356 \begin{lstlisting} |
|
357 a = 1 # In-line comments |
|
358 # Comment in a line to itself. |
|
359 a = "# This is not a comment!" |
|
360 \end{lstlisting} |
|
361 \end{itemize} |
|
362 \inctime{15} |
|
363 \end{frame} |
|
364 |
|
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
366 % TIME: 10 m, running 20m |
|
367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
368 |
|
369 \subsection{Data types} |
|
370 \begin{frame} |
|
371 \frametitle{Basic types} |
|
372 \begin{itemize} |
|
373 \item Numbers: float, int, long, complex |
|
374 \item Strings |
|
375 \item Boolean |
|
376 \end{itemize} |
|
377 \begin{block}{Also to be discussed later} |
|
378 tuples, lists, dictionaries, functions, objects\ldots |
|
379 \end{block} |
|
380 \end{frame} |
|
381 |
|
382 \begin{frame}[fragile] |
|
383 \frametitle{Numbers} |
|
384 \vspace*{-0.25in} |
|
385 \begin{lstlisting} |
|
386 >>> a = 1 # Int. |
|
387 >>> l = 1000000L # Long |
|
388 >>> e = 1.01325e5 # float |
|
389 >>> f = 3.14159 # float |
|
390 >>> c = 1+1j # Complex! |
|
391 >>> print f*c/a |
|
392 (3.14159+3.14159j) |
|
393 >>> print c.real, c.imag |
|
394 1.0 1.0 |
|
395 >>> abs(c) |
|
396 1.4142135623730951 |
|
397 >>> abs( 8 - 9.5 ) |
|
398 1.5 |
|
399 \end{lstlisting} |
|
400 \end{frame} |
|
401 |
|
402 \begin{frame}[fragile] |
|
403 \frametitle{Boolean} |
|
404 \begin{lstlisting} |
|
405 >>> t = True |
|
406 >>> f = not t |
|
407 False |
|
408 >>> f or t |
|
409 True |
|
410 >>> f and t |
|
411 False |
|
412 \end{lstlisting} |
|
413 \begin{block}{Try:} |
|
414 NOT True\\ |
|
415 not TRUE |
|
416 \end{block} |
|
417 \end{frame} |
|
418 |
|
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
420 % TIME: 10 m, running 30m |
|
421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
422 |
|
423 \begin{frame}[fragile] |
|
424 \frametitle{Relational and logical operators} |
|
425 \begin{lstlisting} |
|
426 >>> a, b, c = -1, 0, 1 |
|
427 >>> a == b |
|
428 False |
|
429 >>> a <= b |
|
430 True |
|
431 >>> a + b != c |
|
432 True |
|
433 >>> a < b < c |
|
434 True |
|
435 >>> c >= a + b |
|
436 True |
|
437 \end{lstlisting} |
|
438 \end{frame} |
|
439 |
|
440 \begin{frame}[fragile] |
|
441 \frametitle{Strings} |
|
442 \begin{lstlisting} |
|
443 s = 'this is a string' |
|
444 s = 'This one has "quotes" inside!' |
|
445 s = "I have 'single-quotes' inside!" |
|
446 l = "A string spanning many lines\ |
|
447 one more line\ |
|
448 yet another" |
|
449 t = """A triple quoted string does |
|
450 not need to be escaped at the end and |
|
451 "can have nested quotes" etc.""" |
|
452 \end{lstlisting} |
|
453 \end{frame} |
|
454 |
|
455 \begin{frame}[fragile] |
|
456 \frametitle{More Strings} |
|
457 \vspace*{-0.2in} |
|
458 \begin{lstlisting} |
|
459 >>> w = "hello" |
|
460 >>> print w[0] + w[2] + w[-1] |
|
461 hlo |
|
462 >>> len(w) # guess what |
|
463 5 |
|
464 >>> s = u'Unicode strings!' |
|
465 >>> # Raw strings (note the leading 'r') |
|
466 ... r_s = r'A string $\alpha \nu$' |
|
467 \end{lstlisting} |
|
468 \pause |
|
469 \begin{lstlisting} |
|
470 >>> w[0] = 'H' # Can't do that! |
|
471 Traceback (most recent call last): |
|
472 File "<stdin>", line 1, in ? |
|
473 TypeError: object does not support item assignment |
|
474 \end{lstlisting} |
|
475 \end{frame} |
|
476 |
|
477 \begin{frame} |
|
478 \frametitle{Let us switch to IPython} |
|
479 Why? |
|
480 \begin{block} |
|
481 {Better help (and a lot more)} |
|
482 Tab completion\\ |
|
483 ?\\ |
|
484 .?\\ |
|
485 object.function? |
|
486 \end{block} |
|
487 \end{frame} |
|
488 |
|
489 \begin{frame}[fragile] |
|
490 \frametitle{More on strings} |
|
491 \begin{lstlisting} |
|
492 In [1]: a = 'hello world' |
|
493 In [2]: a.startswith('hell') |
|
494 Out[2]: True |
|
495 In [3]: a.endswith('ld') |
|
496 Out[3]: True |
|
497 In [4]: a.upper() |
|
498 Out[4]: 'HELLO WORLD' |
|
499 In [5]: a.upper().lower() |
|
500 Out[5]: 'hello world' |
|
501 \end{lstlisting} |
|
502 \end{frame} |
|
503 |
|
504 \begin{frame}[fragile]\frametitle{Still with strings} |
|
505 \begin{lstlisting} |
|
506 In [6]: a.split() |
|
507 Out[6]: ['hello', 'world'] |
|
508 In [7]: ''.join(['a', 'b', 'c']) |
|
509 Out[7]: 'abc' |
|
510 In [8] 'd' in ''.join(['a', 'b', 'c']) |
|
511 Out[8]: False |
|
512 \end{lstlisting} |
|
513 \begin{block}{Try:} |
|
514 \texttt{a.split( 'o' )}\\ |
|
515 \texttt{'x'.join( a.split( 'o' ) )} |
|
516 \end{block} |
|
517 \end{frame} |
|
518 |
|
519 \begin{frame}[fragile]\frametitle{String formatting} |
|
520 \begin{lstlisting} |
|
521 In [11]: x, y = 1, 1.234 |
|
522 In [12]: 'x is %s, y is %s' %(x, y) |
|
523 Out[12]: 'x is 1, y is 1.234' |
|
524 \end{lstlisting} |
|
525 \begin{block}{Try:} |
|
526 \texttt{'x is \%d, y is \%f' \%(x, y) }\\ |
|
527 \texttt{'x is \%3d, y is \%4.2f' \%(x, y) } |
|
528 \end{block} |
|
529 \small |
|
530 \url{docs.python.org/lib/typesseq-strings.html}\\ |
|
531 \end{frame} |
|
532 |
|
533 \begin{frame} |
|
534 {A classic problem} |
|
535 \begin{block} |
|
536 {Interchange values} |
|
537 How to interchange values of two variables? |
|
538 \end{block} |
|
539 \pause |
|
540 \begin{block}{Note:} |
|
541 This Python idiom works for all types of variables.\\ |
|
542 They need not be of the same type! |
|
543 \end{block} |
|
544 \inctime{30} |
|
545 \end{frame} |
|
546 |
|
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
548 % TIME: 25 m+ Interlude break 5 mins, running 60m |
|
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
550 |
|
551 \subsection{Control flow} |
|
552 \begin{frame} |
|
553 \frametitle{Control flow constructs} |
|
554 \begin{itemize} |
|
555 \item \kwrd{if/elif/else}: branching |
|
556 \item \kwrd{while}: looping |
|
557 \item \kwrd{for}: iterating |
|
558 \item \kwrd{break, continue}: modify loop |
|
559 \item \kwrd{pass}: syntactic filler |
|
560 \end{itemize} |
|
561 \end{frame} |
|
562 |
|
563 \begin{frame}[fragile] |
|
564 \frametitle{Basic conditional flow} |
|
565 \begin{lstlisting} |
|
566 In [21]: a = 7 |
|
567 In [22]: b = 8 |
|
568 In [23]: if a > b: |
|
569 ....: print 'Hello' |
|
570 ....: else: |
|
571 ....: print 'World' |
|
572 ....: |
|
573 ....: |
|
574 World |
|
575 \end{lstlisting} |
|
576 Let us switch to creating a file |
|
577 \end{frame} |
|
578 |
|
579 \begin{frame} |
|
580 {Creating python files} |
|
581 \begin{itemize} |
|
582 \item aka scripts |
|
583 \item use your editor |
|
584 \item extension \typ{.py} |
|
585 \item run with \texttt{python hello.py} at the command line |
|
586 \item in IPython using \kwrd{\%run} |
|
587 \end{itemize} |
|
588 \end{frame} |
|
589 |
|
590 \begin{frame}[fragile] |
|
591 \frametitle{\typ{If...elif...else} example} |
|
592 \begin{lstlisting} |
|
593 x = int(raw_input("Enter an integer:")) |
|
594 if x < 0: |
|
595 print 'Be positive!' |
|
596 elif x == 0: |
|
597 print 'Zero' |
|
598 elif x == 1: |
|
599 print 'Single' |
|
600 else: |
|
601 print 'More' |
|
602 \end{lstlisting} |
|
603 \end{frame} |
|
604 |
|
605 \begin{frame}{Simple IO} |
|
606 \begin{block} |
|
607 {Console Input} |
|
608 \texttt{raw\_input()} waits for user input.\\Prompt string is optional.\\ |
|
609 All keystrokes are Strings!\\\texttt{int()} converts string to int. |
|
610 \end{block} |
|
611 \begin{block} |
|
612 {Console output} |
|
613 \texttt{print} is straight forward. Note the distinction between \texttt{print x} and \texttt{print x,} |
|
614 \end{block} |
|
615 \end{frame} |
|
616 |
|
617 \begin{frame}[fragile] |
|
618 \frametitle{Basic looping} |
|
619 \begin{lstlisting} |
|
620 # Fibonacci series: |
|
621 # the sum of two elements |
|
622 # defines the next |
|
623 a, b = 0, 1 |
|
624 while b < 10: |
|
625 print b, |
|
626 a, b = b, a + b |
|
627 |
|
628 \end{lstlisting} |
|
629 \typ{1 1 2 3 5 8}\\ |
|
630 \alert{Recall it is easy to write infinite loops with \kwrd{while}} |
|
631 \inctime{20} |
|
632 \end{frame} |
|
633 |
|
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
635 % TIME: 20 m, running 80m |
|
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
637 |
|
638 \section{Session Summary} |
|
639 \begin{frame}{So what have we learnt so far?} |
|
640 \begin{itemize} |
|
641 \item The interactive interpreter |
|
642 \item Basic Data Types |
|
643 \item Creating and running a Python script |
|
644 \item \kwrd{if/elif/else} |
|
645 \item Simple IO |
|
646 \item Basic Looping with \kwrd{while} |
|
647 \end{itemize} |
|
648 \end{frame} |
|
649 \end{document} |
|