|
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 formal approach} |
|
77 |
|
78 \author[FOSSEE Team] {The FOSSEE Group} |
|
79 |
|
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
81 \date[] {1, November 2009\\Day 2, 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 \AtBeginSection[] |
|
99 { |
|
100 \begin{frame}<beamer> |
|
101 \frametitle{Outline} |
|
102 \tableofcontents[currentsection,currentsubsection] |
|
103 \end{frame} |
|
104 } |
|
105 |
|
106 % If you wish to uncover everything in a step-wise fashion, uncomment |
|
107 % the following command: |
|
108 %\beamerdefaultoverlayspecification{<+->} |
|
109 |
|
110 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6} |
|
111 |
|
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
113 % DOCUMENT STARTS |
|
114 \begin{document} |
|
115 |
|
116 \begin{frame} |
|
117 \titlepage |
|
118 \end{frame} |
|
119 |
|
120 \begin{frame} |
|
121 \frametitle{Outline} |
|
122 \tableofcontents |
|
123 % You might wish to add the option [pausesections] |
|
124 \end{frame} |
|
125 |
|
126 \section{Data types} |
|
127 |
|
128 \begin{frame} |
|
129 \frametitle{Primitive Data types} |
|
130 \begin{itemize} |
|
131 \item Numbers: float, int, complex |
|
132 \item Strings |
|
133 \item Boolean |
|
134 \end{itemize} |
|
135 \end{frame} |
|
136 |
|
137 \subsection{Numbers} |
|
138 \begin{frame}[fragile] |
|
139 \frametitle{Numbers} |
|
140 \begin{itemize} |
|
141 \item \kwrd{int}\\ Any whole number is an \kwrd{int}, no matter what the size! |
|
142 \begin{lstlisting} |
|
143 In []: a = 13 |
|
144 |
|
145 In []: a = 99999999999999999999 |
|
146 \end{lstlisting} |
|
147 \item \kwrd{float} |
|
148 \begin{lstlisting} |
|
149 In []: fl = 3.141592 |
|
150 \end{lstlisting} |
|
151 \end{itemize} |
|
152 \end{frame} |
|
153 |
|
154 \begin{frame}[fragile] |
|
155 \frametitle{Complex numbers} |
|
156 \begin{lstlisting} |
|
157 In []: cplx = 3+4j |
|
158 |
|
159 In []: abs(cplx) |
|
160 Out[]: 5.0 |
|
161 |
|
162 In []: cplx.imag |
|
163 Out[]: 4.0 |
|
164 |
|
165 In []: cplx.real |
|
166 Out[]: 3.0 |
|
167 \end{lstlisting} |
|
168 \end{frame} |
|
169 |
|
170 \subsection{Boolean} |
|
171 \begin{frame}{Boolean} |
|
172 \begin{itemize} |
|
173 \item \kwrd{True} |
|
174 \item \kwrd{False} |
|
175 \item \kwrd{not} |
|
176 \item \kwrd{and} |
|
177 \item \kwrd{or} |
|
178 \end{itemize} |
|
179 \end{frame} |
|
180 |
|
181 \subsection{Strings} |
|
182 \begin{frame}[fragile] |
|
183 \frametitle{String methods} |
|
184 Strings were introduced previously, let us now look at them in a little more detail. |
|
185 \begin{lstlisting} |
|
186 In []: a = 'hello world' |
|
187 |
|
188 In []: a.startswith('hell') |
|
189 Out[]: True |
|
190 |
|
191 In []: a.endswith('ld') |
|
192 Out[]: True |
|
193 \end{lstlisting} |
|
194 \end{frame} |
|
195 |
|
196 \begin{frame}[fragile] |
|
197 \frametitle{Still with strings} |
|
198 We saw split() previously. join() is the opposite of split() |
|
199 \begin{lstlisting} |
|
200 In []: ''.join(['a', 'b', 'c']) |
|
201 Out[]: 'abc' |
|
202 \end{lstlisting} |
|
203 \begin{block}{Note:} |
|
204 Strings are immutable.\\ That is string variables cannot be changed. |
|
205 \end{block} |
|
206 \end{frame} |
|
207 |
|
208 \begin{frame}[fragile] |
|
209 \frametitle{String formatting} |
|
210 \begin{lstlisting} |
|
211 In []: x, y = 1, 1.234 |
|
212 In []: 'x is %s, y is %s' %(x, y) |
|
213 Out[]: 'x is 1, y is 1.234' |
|
214 \end{lstlisting} |
|
215 \small |
|
216 \url{docs.python.org/lib/typesseq-strings.html}\\ |
|
217 \inctime{10} |
|
218 \end{frame} |
|
219 |
|
220 \section{Relational and logical operators} |
|
221 \begin{frame}[fragile] |
|
222 \frametitle{Relational and logical operators} |
|
223 \begin{lstlisting} |
|
224 In []: pos, zer, neg = 1, 0, -1 |
|
225 In []: pos == neg |
|
226 Out[]: False |
|
227 |
|
228 In []: pos >= neg |
|
229 Out[]: True |
|
230 |
|
231 In []: neg < zer < pos |
|
232 Out[]: True |
|
233 |
|
234 In []: pos + neg != zer |
|
235 Out[]: False |
|
236 \end{lstlisting} |
|
237 \inctime{5} |
|
238 \end{frame} |
|
239 |
|
240 \begin{frame} |
|
241 {A classic problem} |
|
242 \begin{block} |
|
243 {Interchange values} |
|
244 How to interchange values of two variables? |
|
245 \end{block} |
|
246 \pause |
|
247 \begin{block}{Note:} |
|
248 This Python idiom works for all types of variables.\\ |
|
249 They need not be of the same type! |
|
250 \end{block} |
|
251 \inctime{} |
|
252 \end{frame} |
|
253 |
|
254 \section{Control flow} |
|
255 \begin{frame} |
|
256 \frametitle{Control flow constructs} |
|
257 \begin{itemize} |
|
258 \item \kwrd{if/elif/else}: branching |
|
259 \item \kwrd{while}: looping |
|
260 \item \kwrd{for}: iterating |
|
261 \item \kwrd{break, continue}: modify loop |
|
262 \item \kwrd{pass}: syntactic filler |
|
263 \end{itemize} |
|
264 \end{frame} |
|
265 |
|
266 \subsection{Basic Conditional flow} |
|
267 \begin{frame}[fragile] |
|
268 \frametitle{\typ{If...elif...else} example} |
|
269 \begin{lstlisting} |
|
270 x = int(raw_input("Enter an integer:")) |
|
271 if x < 0: |
|
272 print 'Be positive!' |
|
273 elif x == 0: |
|
274 print 'Zero' |
|
275 elif x == 1: |
|
276 print 'Single' |
|
277 else: |
|
278 print 'More' |
|
279 \end{lstlisting} |
|
280 \end{frame} |
|
281 |
|
282 \begin{frame}{Simple IO} |
|
283 \begin{block} |
|
284 {Console Input} |
|
285 \texttt{raw\_input()} waits for user input.\\Prompt string is optional.\\ |
|
286 All keystrokes are Strings!\\\texttt{int()} converts string to int. |
|
287 \end{block} |
|
288 \begin{block} |
|
289 {Console output} |
|
290 \texttt{print} is straight forward. Note the distinction between \texttt{print x} and \texttt{print x,} |
|
291 \end{block} |
|
292 \end{frame} |
|
293 |
|
294 \subsection{Basic Looping} |
|
295 \begin{frame}[fragile] |
|
296 \frametitle{\kwrd{while}} |
|
297 Example: Fibonacci series |
|
298 \begin{lstlisting} |
|
299 # the sum of two elements |
|
300 # defines the next |
|
301 a, b = 0, 1 |
|
302 while b < 10: |
|
303 print b, |
|
304 a, b = b, a + b |
|
305 \end{lstlisting} |
|
306 \typ{1 1 2 3 5 8}\\ |
|
307 \end{frame} |
|
308 |
|
309 \begin{frame}[fragile] |
|
310 \frametitle{\kwrd{range()}} |
|
311 \kwrd{range([start,] stop[, step])}\\ |
|
312 \begin{itemize} |
|
313 \item \alert {range() returns a list of integers} |
|
314 \item \alert {The start and the step arguments are optional} |
|
315 \end{itemize} |
|
316 \end{frame} |
|
317 |
|
318 \begin{frame}[fragile] |
|
319 \frametitle{\kwrd{for}} |
|
320 Example: print squares of first \typ{n} numbers |
|
321 \begin{lstlisting} |
|
322 In []: for i in range(5): |
|
323 ....: print i, i * i |
|
324 ....: |
|
325 ....: |
|
326 0 0 |
|
327 1 1 |
|
328 2 4 |
|
329 3 9 |
|
330 4 16 |
|
331 \end{lstlisting} |
|
332 \inctime{15} |
|
333 \end{frame} |
|
334 |
|
335 \section{Lists} |
|
336 \begin{frame}[fragile] |
|
337 \frametitle{More List methods} |
|
338 \begin{lstlisting} |
|
339 In []: lst = [1,2,3,4,5,6,7,8] |
|
340 In []: lst.reverse() |
|
341 In []: lst |
|
342 Out[]: [8, 7, 6, 5, 4, 3, 2, 1] |
|
343 |
|
344 In []: lst.extend([0, -1, -2]) |
|
345 In []: lst |
|
346 Out[]: [8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2] |
|
347 |
|
348 In []: lst.remove(0) |
|
349 In []: lst |
|
350 Out[]: [8, 7, 6, 5, 4, 3, 2, 1, -1, -2] |
|
351 \end{lstlisting} |
|
352 \end{frame} |
|
353 |
|
354 \begin{frame}[fragile] |
|
355 \frametitle{List containership} |
|
356 \begin{lstlisting} |
|
357 In []: a = 8 |
|
358 |
|
359 In []: a in lst |
|
360 Out[]: True |
|
361 |
|
362 In []: b = 10 |
|
363 In []: b in lst |
|
364 Out[]: False |
|
365 |
|
366 In []: b not in lst |
|
367 Out[]: True |
|
368 \end{lstlisting} |
|
369 \inctime{10} |
|
370 \end{frame} |
|
371 |
|
372 \section{Tuples} |
|
373 \begin{frame}[fragile] |
|
374 \frametitle{Tuples: Immutable lists} |
|
375 \begin{lstlisting} |
|
376 In []: tup = (1,2,3,4,5,6,7,8) |
|
377 In []: tup[0]+tup[3]+tup[-1] |
|
378 Out[]: 13 |
|
379 \end{lstlisting} |
|
380 \begin{block}{Note:} |
|
381 \begin{itemize} |
|
382 \item Tuples are immutable - cannot be changed |
|
383 \item Multiple return values in a function are actually tuples |
|
384 \item Tuples are working behind the scenes in exchanging values - Tuple Unpacking |
|
385 \end{itemize} |
|
386 \end{block} |
|
387 \inctime{5} |
|
388 \end{frame} |
|
389 |
|
390 \section{Dictionaries} |
|
391 \begin{frame}[fragile] |
|
392 \frametitle{Dictionaries} |
|
393 \alert {lists and tuples: integer indexes :: dictionaries: string indexes} |
|
394 \begin{lstlisting} |
|
395 In []: player = {'Mat': 134, 'Inn': 233, 'Runs': 10823, 'Avg': 52.53} |
|
396 |
|
397 In []: player['Avg'] |
|
398 Out[]: 52.530000000000001 |
|
399 |
|
400 In []: player.keys() |
|
401 Out[]: ['Runs', 'Inn', 'Avg', 'Mat'] |
|
402 |
|
403 In []: player.values() |
|
404 Out[]: [10823, 233, 52.530000000000001, 134] |
|
405 \end{lstlisting} |
|
406 \end{frame} |
|
407 |
|
408 \begin{frame}{Dictionaries} |
|
409 \begin{itemize} |
|
410 \item Duplicate keys are not allowed! |
|
411 \item Dictionaries are iterable through keys. |
|
412 \end{itemize} |
|
413 \inctime{5} |
|
414 \end{frame} |
|
415 |
|
416 \end{document} |