|
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
2 %Tutorial slides on Python. |
|
3 % |
|
4 % Author: The FOSSEE Group |
|
5 % Copyright (c) 2009, The FOSSEE Group, IIT Bombay |
|
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[FOSEE Team] {The FOSSEE Group} |
|
79 |
|
80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} |
|
81 \date[] {31, October 2009\\Day 1, Session 2} |
|
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 \begin{frame} |
|
120 {Acknowledgements} |
|
121 \begin{center} |
|
122 This program is conducted by\\ |
|
123 IIT, Bombay\\ |
|
124 as part of the open source initiatives\\ |
|
125 under the aegis of\\ |
|
126 \alert{National Mission on Education through ICT,} \\ |
|
127 Ministry of HRD. |
|
128 \end{center} |
|
129 \end{frame} |
|
130 |
|
131 \begin{frame} |
|
132 \frametitle{Outline} |
|
133 \tableofcontents |
|
134 % You might wish to add the option [pausesections] |
|
135 \end{frame} |
|
136 |
|
137 \section{Functions} |
|
138 \begin{frame}{Functions: Definition} |
|
139 \begin{itemize} |
|
140 \item \kwrd{def} keyword |
|
141 \end{itemize} |
|
142 \end{frame} |
|
143 |
|
144 \begin{frame}[fragile] |
|
145 \frametitle{Functions: Example 1} |
|
146 \begin{lstlisting} |
|
147 In [1]: def plot_sinx(): |
|
148 ....: x = linspace(0, 2*pi, 100) |
|
149 ....: plt.plot(x, sin(x)) |
|
150 ....: plt.show() |
|
151 ....: |
|
152 |
|
153 In [2]: plot_sinx() |
|
154 \end{lstlisting} |
|
155 \end{frame} |
|
156 |
|
157 \begin{frame}[fragile] |
|
158 \frametitle{Functions: Example 2} |
|
159 \begin{lstlisting} |
|
160 In [3]: def f(x): |
|
161 ....: return sin(x*x*x)+(3*x*x) |
|
162 |
|
163 In [4]: x = linspace(0,2*pi, 1000) |
|
164 |
|
165 In [5]: plt.plot(x, f(x)) |
|
166 \end{lstlisting} |
|
167 \inctime{10} |
|
168 \end{frame} |
|
169 |
|
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
171 % TIME: 10 m, running |
|
172 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
173 |
|
174 \section{Creating and running scripts} |
|
175 \begin{frame} |
|
176 {Creating python files} |
|
177 \begin{itemize} |
|
178 \item aka scripts |
|
179 \item use your editor |
|
180 \item extension \typ{.py} |
|
181 \item run with \texttt{python hello.py} at the command line |
|
182 \item in IPython using \kwrd{\%run} |
|
183 \end{itemize} |
|
184 \inctime{5} |
|
185 \end{frame} |
|
186 |
|
187 \section{Files Handling} |
|
188 \begin{frame}[fragile] |
|
189 \frametitle{Basic File Operations} |
|
190 Opening and Reading files |
|
191 \begin{lstlisting} |
|
192 In [6]: f = open('/path/to/file_name') |
|
193 In [7]: data = f.read() # Read entire file. |
|
194 In [8]: line = f.readline() # Read one line. |
|
195 In [9]: f.close() # close the file. |
|
196 \end{lstlisting} |
|
197 Writing files |
|
198 \begin{lstlisting} |
|
199 In [10]: f = open('/path/to/file_name', 'w') |
|
200 In [11]: f.write('hello world\n') |
|
201 In [12]: f.close() |
|
202 \end{lstlisting} |
|
203 \begin{itemize} |
|
204 \item Everything read or written is a string |
|
205 \end{itemize} |
|
206 \end{frame} |
|
207 |
|
208 \begin{frame}[fragile] |
|
209 \frametitle{File and \kwrd{for}} |
|
210 \begin{lstlisting} |
|
211 In [13]: f = open('dummyfile') |
|
212 |
|
213 In [14]: for line in f: |
|
214 ...: print line |
|
215 ...: |
|
216 \end{lstlisting} |
|
217 \inctime{10} |
|
218 \end{frame} |
|
219 |
|
220 \section{Strings} |
|
221 \begin{frame}[fragile] |
|
222 \frametitle{Strings} |
|
223 \begin{lstlisting} |
|
224 s = 'this is a string' |
|
225 s = 'This one has "quotes" inside!' |
|
226 s = "I have 'single-quotes' inside!" |
|
227 l = "A string spanning many lines\ |
|
228 one more line\ |
|
229 yet another" |
|
230 t = """A triple quoted string does |
|
231 not need to be escaped at the end and |
|
232 "can have nested quotes" etc.""" |
|
233 \end{lstlisting} |
|
234 \end{frame} |
|
235 |
|
236 \begin{frame}[fragile]\frametitle{Strings and \typ{split()}} |
|
237 \begin{lstlisting} |
|
238 In [15]: a = 'hello world' |
|
239 |
|
240 In [16]: a.split() |
|
241 Out[17]: ['hello', 'world'] |
|
242 \end{lstlisting} |
|
243 Now try this: |
|
244 \begin{lstlisting} |
|
245 In [18]: b = 'KD, Madhu, Punchagan, Shantanu, Vattam' |
|
246 |
|
247 In [19]: b.split(',') |
|
248 Out[20]: ['KD', ' Madhu', ' Punchagan', ' Shantanu', ' Vattam'] |
|
249 \end{lstlisting} |
|
250 \inctime{5} |
|
251 \end{frame} |
|
252 |
|
253 \section{Plotting points} |
|
254 \begin{frame}[fragile] |
|
255 \frametitle{How to plot points?} |
|
256 We saw how to plot graphs, lets now look at how to plot points.\\ |
|
257 \begin{lstlisting} |
|
258 In [21]: plt.plot(x, sin(x), 'ro') |
|
259 Out[22]: [<matplotlib.lines.Line2D object at 0xac17e0c>] |
|
260 \end{lstlisting} |
|
261 \begin{itemize} |
|
262 \item \kwrd{'r'},\kwrd{'g'},\kwrd{'b'} for red, green and blue |
|
263 \item \kwrd{'o'} - Dots |
|
264 \item \kwrd{'-'} - Lines |
|
265 \item \kwrd{'- -'} - Dashed lines |
|
266 \end{itemize} |
|
267 \inctime{5} |
|
268 \end{frame} |
|
269 |
|
270 \section{Lists} |
|
271 |
|
272 \begin{frame}[fragile] |
|
273 \frametitle{List creation and indexing} |
|
274 \begin{lstlisting} |
|
275 In [23]: lst = [] #Empty list |
|
276 |
|
277 In [24]: lst = [1,2,3,4] #More useful list |
|
278 |
|
279 In [25]: len(lst) |
|
280 Out[26]: 4 |
|
281 |
|
282 In [27]: lst[0]+lst[1]+lst[-1] |
|
283 Out[27]: 7 |
|
284 \end{lstlisting} |
|
285 \begin{itemize} |
|
286 \item Indices start with ? |
|
287 \item Negative indices indicate ? |
|
288 \end{itemize} |
|
289 \end{frame} |
|
290 |
|
291 \begin{frame}[fragile] |
|
292 \frametitle{List: slices} |
|
293 \begin{itemize} |
|
294 \item Slicing is a basic operation |
|
295 \item \typ{list[initial:final:step]} |
|
296 \item The step is optional |
|
297 \end{itemize} |
|
298 \begin{lstlisting} |
|
299 In [28]: lst[1:3] # A slice. |
|
300 Out[28]: [2, 3] |
|
301 |
|
302 In [29]: lst[1:-1] |
|
303 Out[29]: [2, 3] |
|
304 |
|
305 In [30]: lst[1:] == lst[1:-1] |
|
306 Out[30]: False |
|
307 \end{lstlisting} |
|
308 Explain last result |
|
309 \end{frame} |
|
310 |
|
311 \begin{frame}[fragile] |
|
312 \frametitle{List: more slices} |
|
313 \begin{lstlisting} |
|
314 In [31]: lst[0:-1:2] # Notice the step! |
|
315 Out[31]: [1, 3] |
|
316 |
|
317 In [31]: lst[::2] |
|
318 Out[31]: [1, 3] |
|
319 |
|
320 In [32]: lst[-1::-1] |
|
321 \end{lstlisting} |
|
322 What do you think the last one will do? |
|
323 \end{frame} |
|
324 |
|
325 \begin{frame}[fragile] |
|
326 \frametitle{List methods} |
|
327 \begin{lstlisting} |
|
328 In [33]: lst.append(5) |
|
329 |
|
330 In [34]: lst |
|
331 Out[34]: [1, 2, 3, 4, 5] |
|
332 |
|
333 In [35]: lst.append([6,7]) |
|
334 |
|
335 In [36]: lst |
|
336 Out[36]: [1, 2, 3, 4, 5, [6, 7]] |
|
337 |
|
338 In [37]: lst.extend([8,9]) |
|
339 |
|
340 In [38]: lst |
|
341 Out[38]: [1, 2, 3, 4, 5, [6, 7], 8, 9] |
|
342 \end{lstlisting} |
|
343 \end{frame} |
|
344 |
|
345 \begin{frame}[fragile] |
|
346 \frametitle{List containership} |
|
347 \begin{lstlisting} |
|
348 In [39]: animals = ['cat', 'dog', 'rat', 'croc'] |
|
349 |
|
350 In [40]: 'dog' in animals |
|
351 Out[40]: True |
|
352 |
|
353 In [41]: 'snake' in animals |
|
354 Out[41]: False |
|
355 \end{lstlisting} |
|
356 \inctime{10} |
|
357 \end{frame} |
|
358 |
|
359 \section{Modules and import} |
|
360 \begin{frame}{Modules and \typ{import}} |
|
361 \begin{itemize} |
|
362 \item \kwrd{import} x |
|
363 \item \kwrd{from} x \kwrd{import} y |
|
364 \end{itemize} |
|
365 Whats the difference?? |
|
366 \inctime{5} |
|
367 \end{frame} |
|
368 |
|
369 \end{document} |