0
|
1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
2 |
% Tutorial slides on Python.
|
|
3 |
%
|
|
4 |
% Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in>
|
|
5 |
% Copyright (c) 2005-2008, 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 |
numerics and plotting}
|
|
78 |
|
|
79 |
\author[Asokan \& Prabhu] {Asokan Pichai\\Prabhu Ramachandran}
|
|
80 |
|
|
81 |
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
|
|
82 |
\date[] {26, July 2009}
|
|
83 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
84 |
|
|
85 |
%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
|
|
86 |
%\logo{\pgfuseimage{iitmlogo}}
|
|
87 |
|
|
88 |
|
|
89 |
%% Delete this, if you do not want the table of contents to pop up at
|
|
90 |
%% the beginning of each subsection:
|
|
91 |
\AtBeginSubsection[]
|
|
92 |
{
|
|
93 |
\begin{frame}<beamer>
|
|
94 |
\frametitle{Outline}
|
|
95 |
\tableofcontents[currentsection,currentsubsection]
|
|
96 |
\end{frame}
|
|
97 |
}
|
|
98 |
|
|
99 |
\AtBeginSection[]
|
|
100 |
{
|
|
101 |
\begin{frame}<beamer>
|
|
102 |
\frametitle{Outline}
|
|
103 |
\tableofcontents[currentsection,currentsubsection]
|
|
104 |
\end{frame}
|
|
105 |
}
|
|
106 |
|
|
107 |
% If you wish to uncover everything in a step-wise fashion, uncomment
|
|
108 |
% the following command:
|
|
109 |
%\beamerdefaultoverlayspecification{<+->}
|
|
110 |
|
|
111 |
%\includeonlyframes{current,current1,current2,current3,current4,current5,current6}
|
|
112 |
|
|
113 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
114 |
% DOCUMENT STARTS
|
|
115 |
\begin{document}
|
|
116 |
|
|
117 |
\begin{frame}
|
|
118 |
\titlepage
|
|
119 |
\end{frame}
|
|
120 |
\begin{frame}
|
|
121 |
{Acknowledgements}
|
|
122 |
\begin{center}
|
|
123 |
This program is conducted by\\
|
|
124 |
IIT, Bombay\\
|
|
125 |
through CDEEP\\
|
|
126 |
as part of the open source initiatives\\
|
|
127 |
under the aegis of\\
|
|
128 |
\alert{National Mission on Education through ICT,} \\
|
|
129 |
Ministry of HRD.
|
|
130 |
\end{center}
|
|
131 |
\end{frame}
|
|
132 |
|
|
133 |
\begin{frame}
|
|
134 |
\frametitle{Outline}
|
|
135 |
\tableofcontents
|
|
136 |
% You might wish to add the option [pausesections]
|
|
137 |
\end{frame}
|
|
138 |
|
|
139 |
\begin{frame}{Goal of the Workshop}
|
|
140 |
|
|
141 |
At the end of this program, successful participants will be able
|
|
142 |
to use python as their scripting and problem solving language.
|
|
143 |
Aimed at Engg. students--focus on basic numerics and plotting--
|
|
144 |
but should serve a similar purpose for others.\\
|
|
145 |
|
|
146 |
At the minimum you will be able to use Python for your plotting immediately.
|
|
147 |
|
|
148 |
\end{frame}
|
|
149 |
|
|
150 |
\begin{frame}{Checklist}
|
|
151 |
|
|
152 |
\begin{description}
|
|
153 |
\item[pylab] matplotlib interface
|
|
154 |
\item[numpy] Array computing
|
|
155 |
\item[scipy] numerical work
|
|
156 |
\item[mayavi] \typ{enthought.mayavi}: 3D viz.
|
|
157 |
\end{description}
|
|
158 |
\end{frame}
|
|
159 |
|
|
160 |
\section{30000 feet view}
|
|
161 |
\begin{frame}{Lets see what we can do!}
|
|
162 |
\huge
|
|
163 |
Hold on to your seatbelts
|
|
164 |
\end{frame}
|
|
165 |
|
|
166 |
\begin{frame}
|
|
167 |
{That was done by\ldots}
|
|
168 |
\begin{description}[CalisthenicsIsAnArt]
|
|
169 |
\item[Arrays] 2--3 lines; 5 minutes to learn
|
|
170 |
\item[2D plots] 5 lines; 10 minutes to learn
|
|
171 |
\item[Simple 3D plots] 5 lines; 10 minutes to learn; GUI
|
|
172 |
exploration!
|
|
173 |
\item[Complex plots] relatively short (10-15 lines); more time to master;
|
|
174 |
\end{description}
|
|
175 |
\inctime{15}
|
|
176 |
\end{frame}
|
|
177 |
|
|
178 |
\section{Matplotlib}
|
|
179 |
|
|
180 |
\subsection{Basic \typ{numpy} }
|
|
181 |
|
|
182 |
\newcommand{\num}{\texttt{numpy}}
|
|
183 |
|
|
184 |
\begin{frame}
|
|
185 |
\frametitle{The \num\ module}
|
|
186 |
\begin{itemize}
|
|
187 |
\item Why?
|
|
188 |
\item What:
|
|
189 |
\begin{itemize}
|
|
190 |
\item An efficient and powerful array type for various common data
|
|
191 |
types
|
|
192 |
\item Abstracts out the most commonly used standard operations on
|
|
193 |
arrays
|
|
194 |
\end{itemize}
|
|
195 |
\end{itemize}
|
|
196 |
\end{frame}
|
|
197 |
|
|
198 |
\begin{frame}[fragile]
|
|
199 |
\frametitle{Examples of \num}
|
|
200 |
\begin{lstlisting}
|
|
201 |
# Simple array math example
|
|
202 |
>>> from numpy import *
|
|
203 |
>>> a = array([1,2,3,4])
|
|
204 |
>>> b = array([2,3,4,5])
|
|
205 |
>>> a*2 + b + 1 # Basic math!
|
|
206 |
array([5, 8, 11, 14])
|
|
207 |
# Pi and e are defined.
|
|
208 |
>>> x = linspace(0.0, 10.0, 1000)
|
|
209 |
>>> x *= 2*pi/10 # inplace.
|
|
210 |
# apply functions to array.
|
|
211 |
>>> y = sin(x)
|
|
212 |
\end{lstlisting}
|
|
213 |
\end{frame}
|
|
214 |
|
|
215 |
\begin{frame}
|
|
216 |
\frametitle{Basic concepts}
|
|
217 |
\begin{itemize}
|
|
218 |
\item fixed size (\typ{arr.size});
|
|
219 |
\item Same type (\typ{arr.dtype}) of data
|
|
220 |
\item arbitrary dimensionality
|
|
221 |
\item \typ{arr.shape}: size in each dimension
|
|
222 |
\item \alert{Note:} \typ{len(arr) != arr.size} in general
|
|
223 |
\item \alert{Note:} By default array operations are performed
|
|
224 |
\alert{elementwise}
|
|
225 |
\item Indices, slicing: just like lists
|
|
226 |
\end{itemize}
|
|
227 |
\end{frame}
|
|
228 |
|
|
229 |
|
|
230 |
\begin{frame}[fragile]
|
|
231 |
\frametitle{More examples of \num}
|
|
232 |
\vspace*{-8pt}
|
|
233 |
\begin{lstlisting}
|
|
234 |
>>> x = array([1., 2, 3, 4])
|
|
235 |
>>> size(x)
|
|
236 |
4
|
|
237 |
>>> x.dtype # What is a.dtype?
|
|
238 |
dtype('float64')
|
|
239 |
>>> x.shape
|
|
240 |
(4,)
|
|
241 |
>>> print rank(x), x.itemsize
|
|
242 |
1 8
|
|
243 |
>>> x[0] = 10
|
|
244 |
>>> print x[0], x[-1]
|
|
245 |
10.0 4.0
|
|
246 |
\end{lstlisting}
|
|
247 |
|
|
248 |
\inctime{10}
|
|
249 |
\end{frame}
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
254 |
\subsection{Plotting with \typ{pylab}}
|
|
255 |
|
|
256 |
\begin{frame}
|
|
257 |
{IPython's \typ{pylab} mode}
|
|
258 |
\begin{itemize}
|
|
259 |
\item \typ{pylab}: convenient 2D plotting interface to MPL
|
|
260 |
\item Immediate use: \typ{ipython -pylab}
|
|
261 |
\item Imports all of pylab for you!
|
|
262 |
\item Allows for interactive plotting
|
|
263 |
\end{itemize}
|
|
264 |
\end{frame}
|
|
265 |
|
|
266 |
\begin{frame}[fragile]
|
|
267 |
\frametitle{Basic 2D plotting}
|
|
268 |
|
|
269 |
\begin{lstlisting}
|
|
270 |
>>> x = linspace(0, 2*pi, 1000)
|
|
271 |
>>> plot(x, sin(x))
|
|
272 |
>>> plot(x, sin(x), 'ro')
|
|
273 |
>>> xlabel(r'$\chi$', color='g')
|
|
274 |
# LaTeX markup!
|
|
275 |
>>> ylabel(r'sin($\chi$)', color='r')
|
|
276 |
>>> title('Simple figure', fontsize=20)
|
|
277 |
>>> savefig('/tmp/test.eps')
|
|
278 |
\end{lstlisting}
|
|
279 |
\begin{itemize}
|
|
280 |
\item Also: PNG, PDF, PS, EPS, SVG, PDF
|
|
281 |
\end{itemize}
|
|
282 |
\end{frame}
|
|
283 |
|
|
284 |
|
|
285 |
\begin{frame}[fragile]
|
|
286 |
\frametitle{Basic plotting \ldots}
|
|
287 |
\begin{lstlisting}
|
|
288 |
# Set properties of objects:
|
|
289 |
>>> l, = plot(x, sin(x))
|
|
290 |
# Why "l,"?
|
|
291 |
>>> setp(l, linewidth=2.0, color='r')
|
|
292 |
>>> l.set_linewidth(2.0)
|
|
293 |
>>> draw() # Redraw.
|
|
294 |
>>> setp(l) # Print properties
|
|
295 |
>>> clf() # Clear figure.
|
|
296 |
>>> close() # Close figure.
|
|
297 |
\end{lstlisting}
|
|
298 |
\end{frame}
|
|
299 |
|
|
300 |
\begin{frame}[fragile]
|
|
301 |
\frametitle{Multiple figures}
|
|
302 |
|
|
303 |
\begin{lstlisting}
|
|
304 |
>>> figure(1)
|
|
305 |
>>> plot(x, sin(x))
|
|
306 |
>>> figure(2)
|
|
307 |
>>> plot(x, tanh(x))
|
|
308 |
>>> figure(1)
|
|
309 |
>>> title('Easy as 1,2,3')
|
|
310 |
\end{lstlisting}
|
|
311 |
|
|
312 |
\end{frame}
|
|
313 |
|
|
314 |
|
|
315 |
\begin{frame}[fragile]
|
|
316 |
\frametitle{Legends and Annotation}
|
|
317 |
\begin{lstlisting}
|
|
318 |
>>> plot(x, cos(5*x), 'r--',
|
|
319 |
label='cosine')
|
|
320 |
>>> plot(x, sin(5*x), 'g--',
|
|
321 |
label='sine')
|
|
322 |
>>> legend()
|
|
323 |
# Or use:
|
|
324 |
>>> legend(['cosine', 'sine'])
|
|
325 |
# Annotation:
|
|
326 |
>>> text(1,0, '(1,0)')
|
|
327 |
\end{lstlisting}
|
|
328 |
\end{frame}
|
|
329 |
|
|
330 |
\begin{frame}[fragile]
|
|
331 |
\frametitle{More commands \ldots}
|
|
332 |
\begin{lstlisting}
|
|
333 |
# semilog, loglog
|
|
334 |
>>> x = 10.**(-arange(100)*0.1)
|
|
335 |
>>> semilogx(x, x)
|
|
336 |
>>> semilogy(x, x)
|
|
337 |
>>> loglog(x, x)
|
|
338 |
>>> loglog(x, x*x)
|
|
339 |
\end{lstlisting}
|
|
340 |
\end{frame}
|
|
341 |
|
|
342 |
\begin{frame}[fragile]
|
|
343 |
\frametitle{More plots \ldots}
|
|
344 |
\begin{lstlisting}
|
|
345 |
>>> clf()
|
|
346 |
>>> t = arange(0.1, 4, 0.1)
|
|
347 |
>>> s = exp(-t)
|
|
348 |
>>> e = 0.1*abs(randn(len(s)))
|
|
349 |
>>> errorbar(t, s, e)
|
|
350 |
# Scatter plots
|
|
351 |
>>> clf()
|
|
352 |
>>> t = randn(len(e))
|
|
353 |
>>> scatter(t, e, c=s)
|
|
354 |
\end{lstlisting}
|
|
355 |
\end{frame}
|
|
356 |
|
|
357 |
\begin{frame}[fragile]
|
|
358 |
\frametitle{Note: \typ{pylab} in Python scripts}
|
|
359 |
\begin{lstlisting}
|
|
360 |
import pylab
|
|
361 |
x = pylab.linspace(0, 20, 1000)
|
|
362 |
pylab.plot(x, pylab.sin(x))
|
|
363 |
|
|
364 |
# Can also use:
|
|
365 |
from pylab import linspace, sin, plot
|
|
366 |
\end{lstlisting}
|
|
367 |
\end{frame}
|
|
368 |
|
|
369 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
370 |
|
|
371 |
\begin{frame}[fragile]
|
|
372 |
\frametitle{X-Y plot}
|
|
373 |
\begin{columns}
|
|
374 |
\column{0.5\textwidth}
|
|
375 |
\hspace*{-0.5in}
|
|
376 |
\includegraphics[height=2in, interpolate=true]{data/xyplot}
|
|
377 |
\column{0.45\textwidth}
|
|
378 |
\begin{block}{Example code}
|
|
379 |
\tiny
|
|
380 |
\begin{lstlisting}
|
|
381 |
t1 = arange(0.0, 5.0, 0.1)
|
|
382 |
t2 = arange(0.0, 5.0, 0.02)
|
|
383 |
t3 = arange(0.0, 2.0, 0.01)
|
|
384 |
subplot(211)
|
|
385 |
plot(t1, cos(2*pi*t1)*exp(-t1), 'bo',
|
|
386 |
t2, cos(2*pi*t2)*exp(-t2), 'k')
|
|
387 |
grid(True)
|
|
388 |
title('A tale of 2 subplots')
|
|
389 |
ylabel('Damped')
|
|
390 |
subplot(212)
|
|
391 |
plot(t3, cos(2*pi*t3), 'r--')
|
|
392 |
grid(True)
|
|
393 |
xlabel('time (s)')
|
|
394 |
ylabel('Undamped')
|
|
395 |
\end{lstlisting}
|
|
396 |
\end{block}
|
|
397 |
\end{columns}
|
|
398 |
\end{frame}
|
|
399 |
|
|
400 |
\begin{frame}[fragile] \frametitle{Semi-log and log-log plots}
|
|
401 |
\begin{columns}
|
|
402 |
\column{0.5\textwidth}
|
|
403 |
\hspace*{-0.5in}
|
|
404 |
\includegraphics[height=2in, interpolate=true]{data/log}
|
|
405 |
\column{0.45\textwidth}
|
|
406 |
\begin{block}{Example code}
|
|
407 |
\tiny
|
|
408 |
\begin{lstlisting}
|
|
409 |
dt = 0.01
|
|
410 |
t = arange(dt, 20.0, dt)
|
|
411 |
subplot(311)
|
|
412 |
semilogy(t, exp(-t/5.0))
|
|
413 |
ylabel('semilogy')
|
|
414 |
grid(True)
|
|
415 |
subplot(312)
|
|
416 |
semilogx(t, sin(2*pi*t))
|
|
417 |
ylabel('semilogx')
|
|
418 |
grid(True)
|
|
419 |
# minor grid on too
|
|
420 |
gca().xaxis.grid(True, which='minor')
|
|
421 |
subplot(313)
|
|
422 |
loglog(t, 20*exp(-t/10.0), basex=4)
|
|
423 |
grid(True)
|
|
424 |
ylabel('loglog base 4 on x')
|
|
425 |
\end{lstlisting}
|
|
426 |
\end{block}
|
|
427 |
\end{columns}
|
|
428 |
\end{frame}
|
|
429 |
|
|
430 |
\begin{frame}[fragile] \frametitle{Errorbar}
|
|
431 |
\begin{columns}
|
|
432 |
\column{0.5\textwidth}
|
|
433 |
\hspace*{-0.5in}
|
|
434 |
\includegraphics[height=2in, interpolate=true]{data/errorbar}
|
|
435 |
\column{0.45\textwidth}
|
|
436 |
\begin{block}{Example code}
|
|
437 |
\tiny
|
|
438 |
\begin{lstlisting}
|
|
439 |
t = arange(0.1, 4, 0.1)
|
|
440 |
s = exp(-t)
|
|
441 |
e = 0.1*abs(randn(len(s)))
|
|
442 |
f = 0.1*abs(randn(len(s)))
|
|
443 |
g = 2*e
|
|
444 |
h = 2*f
|
|
445 |
errorbar(t, s, [e,g], f, fmt='o')
|
|
446 |
xlabel('Distance (m)')
|
|
447 |
ylabel('Height (m)')
|
|
448 |
title('Mean and standard error '\
|
|
449 |
'as a function of distance')
|
|
450 |
\end{lstlisting}
|
|
451 |
\end{block}
|
|
452 |
\end{columns}
|
|
453 |
\end{frame}
|
|
454 |
|
|
455 |
\begin{frame}[fragile] \frametitle{Histogram}
|
|
456 |
\begin{columns}
|
|
457 |
\column{0.5\textwidth}
|
|
458 |
\hspace*{-0.5in}
|
|
459 |
\includegraphics[height=2in, interpolate=true]{data/histogram}
|
|
460 |
\column{0.45\textwidth}
|
|
461 |
\begin{block}{Example code}
|
|
462 |
\tiny
|
|
463 |
\begin{lstlisting}
|
|
464 |
mu, sigma = 100, 15
|
|
465 |
x = mu + sigma*randn(10000)
|
|
466 |
# the histogram of the data
|
|
467 |
n, bins, patches = hist(x, 100, normed=1)
|
|
468 |
# add a 'best fit' line
|
|
469 |
y = normpdf( bins, mu, sigma)
|
|
470 |
l = plot(bins, y, 'r--', linewidth=2)
|
|
471 |
xlim(40, 160)
|
|
472 |
xlabel('Smarts')
|
|
473 |
ylabel('P')
|
|
474 |
title(r'$\rm{IQ:}\/ \mu=100,\/ \sigma=15$')
|
|
475 |
\end{lstlisting}
|
|
476 |
\end{block}
|
|
477 |
\end{columns}
|
|
478 |
\end{frame}
|
|
479 |
|
|
480 |
\begin{frame}[fragile] \frametitle{Bar charts}
|
|
481 |
\begin{columns}
|
|
482 |
\column{0.5\textwidth}
|
|
483 |
\hspace*{-0.5in}
|
|
484 |
\includegraphics[height=2in, interpolate=true]{data/barchart}
|
|
485 |
\column{0.45\textwidth}
|
|
486 |
\begin{block}{Example code}
|
|
487 |
\tiny
|
|
488 |
\begin{lstlisting}
|
|
489 |
N = 5
|
|
490 |
menMeans = (20, 35, 30, 35, 27)
|
|
491 |
menStd = ( 2, 3, 4, 1, 2)
|
|
492 |
# the x locations for the groups
|
|
493 |
ind = arange(N)
|
|
494 |
# the width of the bars
|
|
495 |
width = 0.35
|
|
496 |
p1 = bar(ind, menMeans, width,
|
|
497 |
color='r', yerr=menStd)
|
|
498 |
womenMeans = (25, 32, 34, 20, 25)
|
|
499 |
womenStd = ( 3, 5, 2, 3, 3)
|
|
500 |
p2 = bar(ind+width, womenMeans, width,
|
|
501 |
color='y', yerr=womenStd)
|
|
502 |
ylabel('Scores')
|
|
503 |
title('Scores by group and gender')
|
|
504 |
xticks(ind+width,
|
|
505 |
('G1', 'G2', 'G3', 'G4', 'G5'))
|
|
506 |
xlim(-width,len(ind))
|
|
507 |
yticks(arange(0,41,10))
|
|
508 |
legend((p1[0], p2[0]),
|
|
509 |
('Men', 'Women'), shadow=True)
|
|
510 |
\end{lstlisting}
|
|
511 |
\end{block}
|
|
512 |
\end{columns}
|
|
513 |
\end{frame}
|
|
514 |
|
|
515 |
\begin{frame}[fragile] \frametitle{Pie charts}
|
|
516 |
\begin{columns}
|
|
517 |
\column{0.5\textwidth}
|
|
518 |
\hspace*{-0.4in}
|
|
519 |
\includegraphics[height=2.0in, interpolate=true]{data/piechart}
|
|
520 |
\column{0.45\textwidth}
|
|
521 |
\begin{block}{Example code}
|
|
522 |
\tiny
|
|
523 |
\begin{lstlisting}
|
|
524 |
# make a square figure and axes
|
|
525 |
figure(1, figsize=(8,8))
|
|
526 |
ax = axes([0.1, 0.1, 0.8, 0.8])
|
|
527 |
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
|
|
528 |
fracs = [15,30,45, 10]
|
|
529 |
explode=(0, 0.05, 0, 0)
|
|
530 |
pie(fracs, explode=explode, labels=labels,
|
|
531 |
autopct='%1.1f%%', shadow=True)
|
|
532 |
title('Raining Hogs and Dogs',
|
|
533 |
bbox={'facecolor':'0.8', 'pad':5})
|
|
534 |
\end{lstlisting}
|
|
535 |
\end{block}
|
|
536 |
\end{columns}
|
|
537 |
\end{frame}
|
|
538 |
|
|
539 |
\begin{frame}[fragile] \frametitle{Scatter plots}
|
|
540 |
\begin{columns}
|
|
541 |
\column{0.5\textwidth}
|
|
542 |
\hspace*{-0.4in}
|
|
543 |
\includegraphics[height=2in, interpolate=true]{data/scatter}
|
|
544 |
\column{0.45\textwidth}
|
|
545 |
\begin{block}{Example code}
|
|
546 |
\tiny
|
|
547 |
\begin{lstlisting}
|
|
548 |
N = 30
|
|
549 |
x = 0.9*rand(N)
|
|
550 |
y = 0.9*rand(N)
|
|
551 |
# 0 to 10 point radiuses
|
|
552 |
area = pi*(10 * rand(N))**2
|
|
553 |
volume = 400 + rand(N)*450
|
|
554 |
scatter(x,y,s=area, marker='o', c=volume,
|
|
555 |
alpha=0.75)
|
|
556 |
xlabel(r'$\Delta_i$', size='x-large')
|
|
557 |
ylabel(r'$\Delta_{i+1}$', size='x-large')
|
|
558 |
title(r'Volume and percent change')
|
|
559 |
grid(True)
|
|
560 |
colorbar()
|
|
561 |
savefig('scatter')
|
|
562 |
\end{lstlisting}
|
|
563 |
\end{block}
|
|
564 |
\end{columns}
|
|
565 |
\end{frame}
|
|
566 |
|
|
567 |
\begin{frame}[fragile] \frametitle{Polar}
|
|
568 |
\begin{columns}
|
|
569 |
\column{0.5\textwidth}
|
|
570 |
\hspace*{-0.5in}
|
|
571 |
\includegraphics[height=2in, interpolate=true]{data/polar}
|
|
572 |
\column{0.45\textwidth}
|
|
573 |
\begin{block}{Example code}
|
|
574 |
\tiny
|
|
575 |
\begin{lstlisting}
|
|
576 |
figure(figsize=(8,8))
|
|
577 |
ax = axes([0.1, 0.1, 0.8, 0.8],
|
|
578 |
polar=True,
|
|
579 |
axisbg='#d5de9c')
|
|
580 |
r = arange(0,1,0.001)
|
|
581 |
theta = 2*2*pi*r
|
|
582 |
polar(theta, r, color='#ee8d18', lw=3)
|
|
583 |
# the radius of the grid labels
|
|
584 |
setp(ax.thetagridlabels, y=1.075)
|
|
585 |
title(r"$\theta=4\pi r", fontsize=20)
|
|
586 |
\end{lstlisting}
|
|
587 |
\end{block}
|
|
588 |
\end{columns}
|
|
589 |
\end{frame}
|
|
590 |
|
|
591 |
\begin{frame}[fragile] \frametitle{Contours}
|
|
592 |
\begin{columns}
|
|
593 |
\column{0.45\textwidth}
|
|
594 |
\hspace*{-0.5in}
|
|
595 |
\includegraphics[height=2in, interpolate=true]{data/contour}
|
|
596 |
\column{0.525\textwidth}
|
|
597 |
\begin{block}{Example code}
|
|
598 |
\tiny
|
|
599 |
\begin{lstlisting}
|
|
600 |
x = arange(-3.0, 3.0, 0.025)
|
|
601 |
y = arange(-2.0, 2.0, 0.025)
|
|
602 |
X, Y = meshgrid(x, y)
|
|
603 |
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
|
|
604 |
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
|
|
605 |
# difference of Gaussians
|
|
606 |
Z = 10.0 * (Z2 - Z1)
|
|
607 |
im = imshow(Z, interpolation='bilinear',
|
|
608 |
origin='lower',
|
|
609 |
cmap=cm.gray, extent=(-3,3,-2,2))
|
|
610 |
levels = arange(-1.2, 1.6, 0.2)
|
|
611 |
# label every second level
|
|
612 |
clabel(CS, levels[1::2], inline=1,
|
|
613 |
fmt='%1.1f', fontsize=14)
|
|
614 |
CS = contour(Z, levels,
|
|
615 |
origin='lower',
|
|
616 |
linewidths=2,
|
|
617 |
extent=(-3,3,-2,2))
|
|
618 |
# make a colorbar for the contour lines
|
|
619 |
CB = colorbar(CS, shrink=0.8, extend='both')
|
|
620 |
title('Lines with colorbar')
|
|
621 |
hot(); flag()
|
|
622 |
\end{lstlisting}
|
|
623 |
\end{block}
|
|
624 |
\end{columns}
|
|
625 |
\end{frame}
|
|
626 |
|
|
627 |
\begin{frame}[fragile] \frametitle{Velocity vectors}
|
|
628 |
\begin{columns}
|
|
629 |
\column{0.5\textwidth}
|
|
630 |
\hspace*{-0.5in}
|
|
631 |
\includegraphics[height=2in, interpolate=true]{data/quiver}
|
|
632 |
\column{0.45\textwidth}
|
|
633 |
\begin{block}{Example code}
|
|
634 |
\tiny
|
|
635 |
\begin{lstlisting}
|
|
636 |
X,Y = meshgrid(arange(0,2*pi,.2),
|
|
637 |
arange(0,2*pi,.2) )
|
|
638 |
U = cos(X)
|
|
639 |
V = sin(Y)
|
|
640 |
Q = quiver(X[::3, ::3], Y[::3, ::3],
|
|
641 |
U[::3, ::3], V[::3, ::3],
|
|
642 |
color='r', units='x',
|
|
643 |
linewidths=(2,),
|
|
644 |
edgecolors=('k'),
|
|
645 |
headaxislength=5 )
|
|
646 |
qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s',
|
|
647 |
fontproperties=
|
|
648 |
{'weight': 'bold'})
|
|
649 |
axis([-1, 7, -1, 7])
|
|
650 |
title('triangular head; scale '\
|
|
651 |
'with x view; black edges')
|
|
652 |
\end{lstlisting}
|
|
653 |
\end{block}
|
|
654 |
\end{columns}
|
|
655 |
\end{frame}
|
|
656 |
|
|
657 |
\begin{frame}[fragile] \frametitle{Maps}
|
|
658 |
\includegraphics[height=2.5in, interpolate=true]{data/plotmap}
|
|
659 |
\begin{center}
|
|
660 |
\tiny
|
|
661 |
For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py}
|
|
662 |
\end{center}
|
|
663 |
\end{frame}
|
|
664 |
|
|
665 |
|
|
666 |
\begin{frame}
|
|
667 |
\frametitle{More information}
|
|
668 |
\begin{itemize}
|
|
669 |
\item More information here: \url{http://matplotlib.sf.net}
|
|
670 |
\item \url{http://matplotlib.sf.net/tutorial.html}
|
|
671 |
\item \url{http://matplotlib.sf.net/screenshots.html}
|
|
672 |
\end{itemize}
|
|
673 |
|
|
674 |
\inctime{25}
|
|
675 |
\end{frame}
|
|
676 |
|
|
677 |
\begin{frame}
|
|
678 |
\frametitle{Problem set 1.0}
|
|
679 |
\begin{enumerate}
|
|
680 |
\item Write a function that plots any n-gon given \typ{n}.
|
|
681 |
\item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
|
|
682 |
$k=2.5, 3.5$ and $4$
|
|
683 |
\end{enumerate}
|
|
684 |
\end{frame}
|
|
685 |
|
|
686 |
\begin{frame}
|
|
687 |
\frametitle{Problem set 1.1}
|
|
688 |
\begin{enumerate}
|
|
689 |
\item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) =
|
|
690 |
kx(1-x)$. Plot the successive iterates of this process.
|
|
691 |
\item Plot this using a cobweb plot as follows:
|
|
692 |
\begin{enumerate}
|
|
693 |
\item Start at $(x_0, 0)$
|
|
694 |
\item Draw line to $(x_i, f(x_i))$;
|
|
695 |
\item Set $x_{i+1} = f(x_i)$
|
|
696 |
\item Draw line to $(x_i, x_i)$
|
|
697 |
\item Repeat from 2 for as long as you want
|
|
698 |
\end{enumerate}
|
|
699 |
\end{enumerate}
|
|
700 |
\end{frame}
|
|
701 |
|
|
702 |
\begin{frame}
|
|
703 |
\frametitle{Problem set 1.2}
|
|
704 |
\begin{enumerate}
|
|
705 |
|
|
706 |
\item Plot the Koch snowflake. Write a function to generate the
|
|
707 |
necessary points given the two points constituting a line.
|
|
708 |
\pause
|
|
709 |
\begin{enumerate}
|
|
710 |
\item Split the line into 4 segments.
|
|
711 |
\item The first and last segments are trivial.
|
|
712 |
\item To rotate the point you can use complex numbers,
|
|
713 |
recall that $z e^{j \theta}$ rotates a point $z$ in 2D
|
|
714 |
by $\theta$.
|
|
715 |
\item Do this for all line segments till everything is
|
|
716 |
done.
|
|
717 |
\end{enumerate}
|
|
718 |
\item Show rate of convergence for a first and second order finite
|
|
719 |
difference of sin(x)
|
|
720 |
\end{enumerate}
|
|
721 |
\inctime{30}
|
|
722 |
\end{frame}
|
|
723 |
|
|
724 |
|
|
725 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
726 |
|
|
727 |
\begin{frame}[fragile]
|
|
728 |
\frametitle{More IPython features}
|
|
729 |
\begin{itemize}
|
|
730 |
\item Input and output caching:
|
|
731 |
\begin{itemize}
|
|
732 |
\item \verb+In+: a list of all entered input
|
|
733 |
\item \verb+Out+: a dict of all output
|
|
734 |
\item \verb+%hist [-n]+ macro shows previous history, \verb+-n+
|
|
735 |
suppresses line number information
|
|
736 |
\end{itemize}
|
|
737 |
\item Log the session using \verb+%logstart+, \verb+%logon+ and
|
|
738 |
\verb+%logoff+
|
|
739 |
\item Use \verb+;+ to suppress printing output
|
|
740 |
\item \verb+%time statement+
|
|
741 |
\item \verb+%timeit [-n<N> -r<R> [-t|-c]] statement+
|
|
742 |
|
|
743 |
\end{itemize}
|
|
744 |
\end{frame}
|
|
745 |
|
|
746 |
\begin{frame}[fragile]
|
|
747 |
\frametitle{More IPython features}
|
|
748 |
\begin{itemize}
|
|
749 |
\item \verb+%run [options] file[.py]+ -- running Python code
|
|
750 |
\item \verb+%prun+ runs a statement/expression under the profiler
|
|
751 |
\item \verb+%debug+: Helps with debugging after a crash
|
|
752 |
\end{itemize}
|
|
753 |
\end{frame}
|
|
754 |
|
|
755 |
\begin{frame}[fragile]
|
|
756 |
\frametitle{More IPython features \ldots}
|
|
757 |
\begin{itemize}
|
|
758 |
\item \verb+%edit [options] [args]+: edit lines of code or file
|
|
759 |
specified in editor (configure editor via \verb+$EDITOR+)
|
|
760 |
\item \verb+%cd+ changes directory, see also \verb+%pushd, %popd, %dhist+
|
|
761 |
\item Shell access
|
|
762 |
\begin{itemize}
|
|
763 |
\item \verb+!command+ runs a shell command and returns its output
|
|
764 |
\item \verb+files = !ls+ sets
|
|
765 |
\verb+files+ to all result of the \verb+ls+ command
|
|
766 |
\item \verb+!ls $files+ passes the \verb+files+ variable to the
|
|
767 |
shell command
|
|
768 |
\end{itemize}
|
|
769 |
\end{itemize}
|
|
770 |
\end{frame}
|
|
771 |
|
|
772 |
\begin{frame}[fragile]
|
|
773 |
\frametitle{More IPython features \ldots}
|
|
774 |
\begin{itemize}
|
|
775 |
\item \verb+%bookmark+: store a bookmarked location, for use with \verb+%cd+
|
|
776 |
\item \verb+%save [options] filename n1-n2 n3-n4+: save lines to a
|
|
777 |
file
|
|
778 |
\item Can define and use profiles to setup IPython differently:
|
|
779 |
\verb+math, scipy, numeric, pysh+ etc.
|
|
780 |
\item \verb+%magic+: \alert{Show help on all magics}
|
|
781 |
\item Check out the \verb+%macro+ magic
|
|
782 |
\end{itemize}
|
|
783 |
\end{frame}
|
|
784 |
|
|
785 |
\begin{frame}
|
|
786 |
\frametitle{Problem set 2}
|
|
787 |
\begin{itemize}
|
|
788 |
\item Compare your linspace with that of numpy for 1 million
|
|
789 |
elements in terms of speed.
|
|
790 |
\end{itemize}
|
|
791 |
\inctime{10}
|
|
792 |
\end{frame}
|
|
793 |
|
|
794 |
|
|
795 |
\begin{frame}[fragile]
|
|
796 |
\frametitle{Debugging effectively}
|
|
797 |
|
|
798 |
\begin{itemize}
|
|
799 |
\item \kwrd{print} based strategy
|
|
800 |
\item Process: Hypothesis, test, refine, rinse-repeat
|
|
801 |
\item Using \typ{\%debug} and \typ{\%pdb} in IPython
|
|
802 |
\end{itemize}
|
|
803 |
|
|
804 |
\inctime{10}
|
|
805 |
\end{frame}
|
|
806 |
|
|
807 |
\section{Debugging and testing}
|
|
808 |
|
|
809 |
\begin{frame}[fragile]
|
|
810 |
\frametitle{Testing code with \typ{nosetests}}
|
|
811 |
|
|
812 |
\begin{itemize}
|
|
813 |
\item Writing tests is really simple!
|
|
814 |
|
|
815 |
\item Using nose
|
|
816 |
|
|
817 |
\item Example!
|
|
818 |
\end{itemize}
|
|
819 |
\end{frame}
|
|
820 |
|
|
821 |
\begin{frame}[fragile]
|
|
822 |
\frametitle{Nosetest}
|
|
823 |
\begin{lstlisting}
|
|
824 |
def gcd(a, b):
|
|
825 |
"""Returns gcd of a and b,
|
|
826 |
handles only positive numbers."""
|
|
827 |
if a%b == 0: return b
|
|
828 |
return gcd(b, a%b)
|
|
829 |
def lcm(a, b):
|
|
830 |
return a*b/gcd(a, b)
|
|
831 |
|
|
832 |
if __name__ == '__main__':
|
|
833 |
import nose
|
|
834 |
nose.main()
|
|
835 |
\end{lstlisting}
|
|
836 |
|
|
837 |
\inctime{10}
|
|
838 |
\end{frame}
|
|
839 |
|
|
840 |
\section{NumPy and SciPy}
|
|
841 |
|
|
842 |
\begin{frame}
|
|
843 |
{More Numpy}
|
|
844 |
|
|
845 |
\begin{itemize}
|
|
846 |
\item Multi-dimensional arrays
|
|
847 |
\item Random number generation
|
|
848 |
\end{itemize}
|
|
849 |
|
|
850 |
\end{frame}
|
|
851 |
|
|
852 |
\begin{frame}[fragile]
|
|
853 |
\frametitle{Multi-dimensional arrays}
|
|
854 |
\begin{lstlisting}
|
|
855 |
>>> a = array([[ 0, 1, 2, 3],
|
|
856 |
... [10,11,12,13]])
|
|
857 |
>>> a.shape # (rows, columns)
|
|
858 |
(2, 4)
|
|
859 |
# Accessing and setting values
|
|
860 |
>>> a[1,3]
|
|
861 |
13
|
|
862 |
>>> a[1,3] = -1
|
|
863 |
>>> a[1] # The second row
|
|
864 |
array([10,11,12,-1])
|
|
865 |
|
|
866 |
\end{lstlisting}
|
|
867 |
\end{frame}
|
|
868 |
|
|
869 |
\begin{frame}[fragile]
|
|
870 |
\frametitle{Slicing arrays}
|
|
871 |
\begin{lstlisting}
|
|
872 |
>>> a = array([[1,2,3], [4,5,6],
|
|
873 |
[7,8,9]])
|
|
874 |
>>> a[0,1:3]
|
|
875 |
array([2, 3])
|
|
876 |
>>> a[1:,1:]
|
|
877 |
array([[5, 6],
|
|
878 |
[8, 9]])
|
|
879 |
>>> a[:,2]
|
|
880 |
array([3, 6, 9])
|
|
881 |
\end{lstlisting}
|
|
882 |
\end{frame}
|
|
883 |
\begin{frame}[fragile]
|
|
884 |
\frametitle{Striding arrays}
|
|
885 |
\begin{lstlisting}
|
|
886 |
>>> a[0::2,0::2]
|
|
887 |
array([[1, 3],
|
|
888 |
[7, 9]])
|
|
889 |
# Slices are references to the
|
|
890 |
# same memory!
|
|
891 |
\end{lstlisting}
|
|
892 |
\end{frame}
|
|
893 |
|
|
894 |
\begin{frame}[fragile]
|
|
895 |
\frametitle{Array creation functions}
|
|
896 |
\begin{itemize}
|
|
897 |
\item \typ{array(object, dtype=None, \ldots)}
|
|
898 |
\item \typ{arange(start, stop=None, step=1 \ldots)}
|
|
899 |
\item \typ{linspace(start, stop, num=50, \ldots)}
|
|
900 |
\item \typ{ones(shape, dtype=None, \ldots)}
|
|
901 |
\item \typ{zeros(shape, dtype=float,\ldots)}
|
|
902 |
\item \typ{identity(n)}
|
|
903 |
\item \typ{empty(shape, dtype=float,\ldots)}
|
|
904 |
\item \typ{ones\_like(x)},
|
|
905 |
\item \typ{zeros\_like(x)}, \typ{empty\_like(x)}
|
|
906 |
\end{itemize}
|
|
907 |
\end{frame}
|
|
908 |
|
|
909 |
\begin{frame}[fragile]
|
|
910 |
\frametitle{Array math}
|
|
911 |
\begin{itemize}
|
|
912 |
\item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
|
|
913 |
\typ{+, -, *, /, \%}
|
|
914 |
\item Inplace operators: \typ{a += b}, or \typ{add(a, b,
|
|
915 |
a)} etc.
|
|
916 |
\item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)},
|
|
917 |
\typ{less (<)}, \typ{greater (>)} etc.
|
|
918 |
\item Trig and other functions: \typ{sin(x), arcsin(x), sinh(x),
|
|
919 |
exp(x), sqrt(x)} etc.
|
|
920 |
\item \typ{sum(x, axis=0), product(x, axis=0)}
|
|
921 |
\item \typ{dot(a, b)}
|
|
922 |
\end{itemize}
|
|
923 |
\end{frame}
|
|
924 |
|
|
925 |
\begin{frame}[fragile]
|
|
926 |
\frametitle{Advanced}
|
|
927 |
\begin{itemize}
|
|
928 |
\item Only scratched the surface of \num
|
|
929 |
\item Ufunc methods: \typ{reduce, accumulate, outer, reduceat}
|
|
930 |
\item Typecasting
|
|
931 |
\item More functions: \typ{take, choose, where, compress,
|
|
932 |
concatenate}
|
|
933 |
\item Array broadcasting and \typ{None}
|
|
934 |
\end{itemize}
|
|
935 |
\inctime{15}
|
|
936 |
\end{frame}
|
|
937 |
|
|
938 |
\begin{frame}
|
|
939 |
{Intro to SciPy}
|
|
940 |
\begin{itemize}
|
|
941 |
\item \url{http://www.scipy.org}
|
|
942 |
\item Open source scientific libraries for Python
|
|
943 |
\item Based on NumPy
|
|
944 |
\end{itemize}
|
|
945 |
|
|
946 |
\inctime{25}
|
|
947 |
\end{frame}
|
|
948 |
|
|
949 |
\begin{frame}
|
|
950 |
\frametitle{SciPy}
|
|
951 |
\begin{itemize}
|
|
952 |
\item Provides:
|
|
953 |
\begin{itemize}
|
|
954 |
\item Linear algebra
|
|
955 |
\item Numerical integration
|
|
956 |
\item Fourier transforms
|
|
957 |
\item Signal processing
|
|
958 |
\item Special functions
|
|
959 |
\item Statistics
|
|
960 |
\item Optimization
|
|
961 |
\item Image processing
|
|
962 |
\item ODE solvers
|
|
963 |
\end{itemize}
|
|
964 |
\item Uses LAPACK, QUADPACK, ODEPACK, FFTPACK etc. from netlib
|
|
965 |
\end{itemize}
|
|
966 |
\end{frame}
|
|
967 |
|
|
968 |
|
|
969 |
\section{3D Plotting}
|
|
970 |
|
|
971 |
\begin{frame}
|
|
972 |
\frametitle{Introduction to Mayavi}
|
|
973 |
\begin{itemize}
|
|
974 |
\item Most scientists not interested in details of visualization
|
|
975 |
\item Visualization of data files with a nice UI
|
|
976 |
\item Interactive visualization of data (think Matlab)
|
|
977 |
\item Embedding visualizations in applications
|
|
978 |
\item Customization
|
|
979 |
\end{itemize}
|
|
980 |
\pause
|
|
981 |
\begin{block}{The Goal}
|
|
982 |
Provide a \alert{flexible} library/app for every one of these needs!
|
|
983 |
\end{block}
|
|
984 |
\end{frame}
|
|
985 |
|
|
986 |
\begin{frame}[fragile]
|
|
987 |
\frametitle{\typ{mlab}}
|
|
988 |
\begin{columns}
|
|
989 |
\column{0.62\textwidth}
|
|
990 |
\hspace*{-0.45in}
|
|
991 |
\footnotesize
|
|
992 |
\begin{lstlisting}
|
|
993 |
from enthought.mayavi import mlab
|
|
994 |
from numpy import ogrid, sin
|
|
995 |
|
|
996 |
x, y, z = ogrid[-10:10:100j,
|
|
997 |
-10:10:100j,
|
|
998 |
-10:10:100j]
|
|
999 |
|
|
1000 |
mlab.contour3d(sin(x*y*z)/(x*y*z))
|
|
1001 |
mlab.show()
|
|
1002 |
\end{lstlisting}
|
|
1003 |
\column{0.4\textwidth}
|
|
1004 |
\hspace*{-0.1\linewidth}
|
|
1005 |
\includegraphics[width=1.18\linewidth]{data/mlab.png}
|
|
1006 |
\end{columns}
|
|
1007 |
\end{frame}
|
|
1008 |
|
|
1009 |
\begin{frame}
|
|
1010 |
{A Look at the docs }
|
|
1011 |
|
|
1012 |
\inctime{20}
|
|
1013 |
\end{frame}
|
|
1014 |
|
|
1015 |
|
|
1016 |
\section{Integration demo: Lorenz equations}
|
|
1017 |
|
|
1018 |
\begin{frame}
|
|
1019 |
\frametitle{Lorenz equation example}
|
|
1020 |
\begin{eqnarray*}
|
|
1021 |
\frac{d x}{dt} &=& s (y-x)\\
|
|
1022 |
\frac{d y}{d t} &=& rx -y -xz\\
|
|
1023 |
\frac{d z}{d t} &=& xy - bz\\
|
|
1024 |
\end{eqnarray*}
|
|
1025 |
\begin{itemize}
|
|
1026 |
\item Specifies the evolution of the system
|
|
1027 |
\item Think: Velocity of a particle in 3D
|
|
1028 |
\item Lets trace its path
|
|
1029 |
\end{itemize}
|
|
1030 |
\end{frame}
|
|
1031 |
|
|
1032 |
\begin{frame}
|
|
1033 |
{Interactive exploration}
|
|
1034 |
|
|
1035 |
\inctime{25}
|
|
1036 |
\end{frame}
|
|
1037 |
|
|
1038 |
|
|
1039 |
\end{document}
|
|
1040 |
|
|
1041 |
- Numpy arrays (30 mins)
|
|
1042 |
- Matrices
|
|
1043 |
- random number generation.
|
|
1044 |
- Image manipulation: jigsaw puzzle.
|
|
1045 |
- Monte-carlo integration.
|
|
1046 |
|
|
1047 |
|
|
1048 |
|
|
1049 |
|
|
1050 |
|
|
1051 |
\begin{frame}[fragile]
|
|
1052 |
\frametitle{More on functions}
|
|
1053 |
\begin{itemize}
|
|
1054 |
\item Support default and keyword arguments
|
|
1055 |
\item Scope of variables in the function is local
|
|
1056 |
\item Mutable items are \alert{passed by reference}
|
|
1057 |
\item First line after definition may be a documentation string
|
|
1058 |
(\alert{recommended!})
|
|
1059 |
\item Function definition and execution defines a name bound to the
|
|
1060 |
function
|
|
1061 |
\item You \emph{can} assign a variable to a function!
|
|
1062 |
\end{itemize}
|
|
1063 |
\end{frame}
|
|
1064 |
|
|
1065 |
|
|
1066 |
\begin{frame}[fragile]
|
|
1067 |
\frametitle{Functions: default arguments}
|
|
1068 |
\begin{lstlisting}
|
|
1069 |
def ask_ok(prompt, retries=4, complaint='Yes or no!'):
|
|
1070 |
while True:
|
|
1071 |
ok = raw_input(prompt)
|
|
1072 |
if ok in ('y', 'ye', 'yes'):
|
|
1073 |
return True
|
|
1074 |
if ok in ('n', 'no', 'nop', 'nope'):
|
|
1075 |
return False
|
|
1076 |
retries = retries - 1
|
|
1077 |
if retries < 0:
|
|
1078 |
raise IOError, 'bad user'
|
|
1079 |
print complaint
|
|
1080 |
\end{lstlisting}
|
|
1081 |
\end{frame}
|
|
1082 |
|
|
1083 |
\begin{frame}[fragile]
|
|
1084 |
\frametitle{Functions: keyword arguments}
|
|
1085 |
\begin{lstlisting}
|
|
1086 |
def parrot(voltage, state='a stiff',
|
|
1087 |
action='voom', type='Norwegian Blue'):
|
|
1088 |
print "-- This parrot wouldn't", action,
|
|
1089 |
print "if you put", voltage, "Volts through it."
|
|
1090 |
print "-- Lovely plumage, the", type
|
|
1091 |
print "-- It's", state, "!"
|
|
1092 |
|
|
1093 |
parrot(1000)
|
|
1094 |
parrot(action = 'VOOOOOM', voltage = 1000000)
|
|
1095 |
parrot('a thousand', state = 'pushing up the daisies')
|
|
1096 |
parrot('a million', 'bereft of life', 'jump')
|
|
1097 |
\end{lstlisting}
|
|
1098 |
\end{frame}
|
|
1099 |
|
|
1100 |
\begin{frame}[fragile]
|
|
1101 |
\frametitle{Functions: arbitrary argument lists}
|
|
1102 |
\begin{itemize}
|
|
1103 |
\item Arbitrary number of arguments using \verb+*args+ or
|
|
1104 |
\verb+*whatever+
|
|
1105 |
\item Keyword arguments using \verb+**kw+
|
|
1106 |
\item Given a tuple/dict how do you call a function?
|
|
1107 |
\begin{itemize}
|
|
1108 |
\item Using argument unpacking
|
|
1109 |
\item For positional arguments: \verb+foo(*[5, 10])+
|
|
1110 |
\item For keyword args: \verb+foo(**{'a':5, 'b':10})+
|
|
1111 |
\end{itemize}
|
|
1112 |
\end{itemize}
|
|
1113 |
\begin{lstlisting}
|
|
1114 |
def foo(a=10, b=100):
|
|
1115 |
print a, b
|
|
1116 |
def func(*args, **keyword):
|
|
1117 |
print args, keyword
|
|
1118 |
# Unpacking:
|
|
1119 |
args = [5, 10]
|
|
1120 |
foo(*args)
|
|
1121 |
kw = {'a':5, 'b':10}
|
|
1122 |
foo(**kw)
|
|
1123 |
\end{lstlisting}
|
|
1124 |
\end{frame}
|
|
1125 |
|
|
1126 |
\subsection{Modules, exceptions, classes}
|
|
1127 |
|
|
1128 |
\begin{frame}
|
|
1129 |
\frametitle{Modules}
|
|
1130 |
\begin{itemize}
|
|
1131 |
\item Define variables, functions and classes in a file with a
|
|
1132 |
\typ{.py} extension
|
|
1133 |
\item This file becomes a module!
|
|
1134 |
\item Modules are searched in the following:
|
|
1135 |
\begin{itemize}
|
|
1136 |
\item Current directory
|
|
1137 |
\item Standard: \typ{/usr/lib/python2.3/site-packages/} etc.
|
|
1138 |
\item Directories specified in PYTHONPATH
|
|
1139 |
\item \typ{sys.path}: current path settings (from the \typ{sys}
|
|
1140 |
module)
|
|
1141 |
\end{itemize}
|
|
1142 |
\item The \typ{import} keyword ``loads'' a module
|
|
1143 |
\item One can also use:
|
|
1144 |
\mbox{\typ{from module import name1, name2, name2}}\\
|
|
1145 |
where \typ{name1} etc. are names in the module, ``module''
|
|
1146 |
\item \typ{from module import *} \ --- imports everything from module,
|
|
1147 |
\alert{use only in interactive mode}
|
|
1148 |
\end{itemize}
|
|
1149 |
\end{frame}
|
|
1150 |
|
|
1151 |
\begin{frame}[fragile]
|
|
1152 |
\frametitle{Modules: example}
|
|
1153 |
\begin{lstlisting}
|
|
1154 |
# --- foo.py ---
|
|
1155 |
some_var = 1
|
|
1156 |
def fib(n): # write Fibonacci series up to n
|
|
1157 |
"""Print a Fibonacci series up to n."""
|
|
1158 |
a, b = 0, 1
|
|
1159 |
while b < n:
|
|
1160 |
print b,
|
|
1161 |
a, b = b, a+b
|
|
1162 |
# EOF
|
|
1163 |
|
|
1164 |
>>> import foo
|
|
1165 |
>>> foo.fib(10)
|
|
1166 |
1 1 2 3 5 8
|
|
1167 |
>>> foo.some_var
|
|
1168 |
1
|
|
1169 |
\end{lstlisting}
|
|
1170 |
\end{frame}
|
|
1171 |
|
|
1172 |
\begin{frame}[fragile]
|
|
1173 |
\frametitle{Namespaces}
|
|
1174 |
\begin{itemize}
|
|
1175 |
\item A mapping from names to objects
|
|
1176 |
\item Modules introduce a namespace
|
|
1177 |
\item So do classes
|
|
1178 |
\item The running script's namespace is \verb+__main__+
|
|
1179 |
\item A modules namespace is identified by its name
|
|
1180 |
\item The standard functions (like \typ{len}) are in the
|
|
1181 |
\verb+__builtin__+ namespace
|
|
1182 |
\item Namespaces help organize different names and their bindings to
|
|
1183 |
different objects
|
|
1184 |
\end{itemize}
|
|
1185 |
\end{frame}
|
|
1186 |
|
|
1187 |
\begin{frame}
|
|
1188 |
\frametitle{Exceptions}
|
|
1189 |
\begin{itemize}
|
|
1190 |
\item Python's way of notifying you of errors
|
|
1191 |
\item Several standard exceptions: \typ{SyntaxError}, \typ{IOError}
|
|
1192 |
etc.
|
|
1193 |
\item Users can also \typ{raise} errors
|
|
1194 |
\item Users can create their own exceptions
|
|
1195 |
\item Exceptions can be ``caught'' via \typ{try/except} blocks
|
|
1196 |
\end{itemize}
|
|
1197 |
\end{frame}
|
|
1198 |
|
|
1199 |
\begin{frame}[fragile]
|
|
1200 |
\frametitle{Exception: examples}
|
|
1201 |
\begin{lstlisting}
|
|
1202 |
>>> 10 * (1/0)
|
|
1203 |
Traceback (most recent call last):
|
|
1204 |
File "<stdin>", line 1, in ?
|
|
1205 |
ZeroDivisionError: integer division or modulo by zero
|
|
1206 |
>>> 4 + spam*3
|
|
1207 |
Traceback (most recent call last):
|
|
1208 |
File "<stdin>", line 1, in ?
|
|
1209 |
NameError: name 'spam' is not defined
|
|
1210 |
>>> '2' + 2
|
|
1211 |
Traceback (most recent call last):
|
|
1212 |
File "<stdin>", line 1, in ?
|
|
1213 |
TypeError: cannot concatenate 'str' and 'int' objects
|
|
1214 |
\end{lstlisting}
|
|
1215 |
\end{frame}
|
|
1216 |
|
|
1217 |
\begin{frame}[fragile]
|
|
1218 |
\frametitle{Exception: examples}
|
|
1219 |
\begin{lstlisting}
|
|
1220 |
>>> while True:
|
|
1221 |
... try:
|
|
1222 |
... x = int(raw_input("Enter a number: "))
|
|
1223 |
... break
|
|
1224 |
... except ValueError:
|
|
1225 |
... print "Invalid number, try again..."
|
|
1226 |
...
|
|
1227 |
>>> # To raise exceptions
|
|
1228 |
... raise ValueError, "your error message"
|
|
1229 |
Traceback (most recent call last):
|
|
1230 |
File "<stdin>", line 2, in ?
|
|
1231 |
ValueError: your error message
|
|
1232 |
\end{lstlisting}
|
|
1233 |
\end{frame}
|
|
1234 |
|
|
1235 |
\begin{frame}[fragile]
|
|
1236 |
\frametitle{Classes: the big picture}
|
|
1237 |
\begin{itemize}
|
|
1238 |
\item Lets you create new data types
|
|
1239 |
\item Class is a template for an object belonging to that class
|
|
1240 |
\item Note: in Python a class is also an object
|
|
1241 |
\item Instantiating a class creates an instance (an object)
|
|
1242 |
\item An instance encapsulates the state (data) and behavior
|
|
1243 |
(methods)
|
|
1244 |
\item Allows you to define an inheritance hierarchy
|
|
1245 |
\begin{itemize}
|
|
1246 |
\item ``A Honda car \alert{is a} car.''
|
|
1247 |
\item ``A car \alert{is an} automobile.''
|
|
1248 |
\item ``A Python \alert{is a} reptile.''
|
|
1249 |
\end{itemize}
|
|
1250 |
\item Programmers need to think OO
|
|
1251 |
\end{itemize}
|
|
1252 |
\end{frame}
|
|
1253 |
|
|
1254 |
\begin{frame}[fragile]
|
|
1255 |
\frametitle{Classes: what's the big deal?}
|
|
1256 |
\begin{itemize}
|
|
1257 |
\item Lets you create objects that mimic a real problem being
|
|
1258 |
simulated
|
|
1259 |
\item Makes problem solving more natural and elegant
|
|
1260 |
\item Easier to create code
|
|
1261 |
\item Allows for code-reuse
|
|
1262 |
\item Polymorphism
|
|
1263 |
\end{itemize}
|
|
1264 |
\end{frame}
|
|
1265 |
|
|
1266 |
\begin{frame}[fragile]
|
|
1267 |
\frametitle{Class definition and instantiation}
|
|
1268 |
\begin{itemize}
|
|
1269 |
\item Class definitions when executed create class objects
|
|
1270 |
\item Instantiating the class object creates an instance of the
|
|
1271 |
class
|
|
1272 |
\end{itemize}
|
|
1273 |
\footnotesize
|
|
1274 |
\begin{lstlisting}
|
|
1275 |
class Foo(object):
|
|
1276 |
pass
|
|
1277 |
# class object created.
|
|
1278 |
# Create an instance of Foo.
|
|
1279 |
f = Foo()
|
|
1280 |
# Can assign an attribute to the instance
|
|
1281 |
f.a = 100
|
|
1282 |
print f.a
|
|
1283 |
100
|
|
1284 |
\end{lstlisting}
|
|
1285 |
\end{frame}
|
|
1286 |
|
|
1287 |
\begin{frame}[fragile]
|
|
1288 |
\frametitle{Classes \ldots}
|
|
1289 |
\begin{itemize}
|
|
1290 |
\item All attributes are accessed via the \typ{object.attribute}
|
|
1291 |
syntax
|
|
1292 |
\item Both class and instance attributes are supported
|
|
1293 |
\item \emph{Methods} represent the behavior of an object: crudely
|
|
1294 |
think of them as functions ``belonging'' to the object
|
|
1295 |
\item All methods in Python are ``virtual''
|
|
1296 |
\item Inheritance through subclassing
|
|
1297 |
\item Multiple inheritance is supported
|
|
1298 |
\item No special public and private attributes: only good
|
|
1299 |
conventions
|
|
1300 |
\begin{itemize}
|
|
1301 |
\item \verb+object.public()+: public
|
|
1302 |
\item \verb+object._private()+ \& \verb+object.__priv()+:
|
|
1303 |
non-public
|
|
1304 |
\end{itemize}
|
|
1305 |
\end{itemize}
|
|
1306 |
\end{frame}
|
|
1307 |
|
|
1308 |
\begin{frame}[fragile]
|
|
1309 |
\frametitle{Classes: examples}
|
|
1310 |
\begin{lstlisting}
|
|
1311 |
class MyClass(object):
|
|
1312 |
"""Example class (this is the class docstring)."""
|
|
1313 |
i = 12345 # A class attribute
|
|
1314 |
def f(self):
|
|
1315 |
"""This is the method docstring"""
|
|
1316 |
return 'hello world'
|
|
1317 |
|
|
1318 |
>>> a = MyClass() # creates an instance
|
|
1319 |
>>> a.f()
|
|
1320 |
'hello world'
|
|
1321 |
>>> # a.f() is equivalent to MyClass.f(a)
|
|
1322 |
... # This also explains why f has a 'self' argument.
|
|
1323 |
... MyClass.f(a)
|
|
1324 |
'hello world'
|
|
1325 |
\end{lstlisting}
|
|
1326 |
\end{frame}
|
|
1327 |
|
|
1328 |
\begin{frame}[fragile]
|
|
1329 |
\frametitle{Classes (continued)}
|
|
1330 |
\begin{itemize}
|
|
1331 |
\item \typ{self} is \alert{conventionally} the first argument for a
|
|
1332 |
method
|
|
1333 |
\item In previous example, \typ{a.f} is a method object
|
|
1334 |
\item When \typ{a.f} is called, it is passed the instance \typ{a} as
|
|
1335 |
the first argument
|
|
1336 |
\item If a method called \verb+__init__+ exists, it is called when
|
|
1337 |
the object is created
|
|
1338 |
\item If a method called \verb+__del__+ exists, it is called before
|
|
1339 |
the object is garbage collected
|
|
1340 |
\item Instance attributes are set by simply ``setting'' them in
|
|
1341 |
\typ{self}
|
|
1342 |
\item Other special methods (by convention) like \verb+__add__+ let
|
|
1343 |
you define numeric types:
|
|
1344 |
{\footnotesize \url{http://docs.python.org/ref/specialnames.html}
|
|
1345 |
\\ \url{http://docs.python.org/ref/numeric-types.html}
|
|
1346 |
}
|
|
1347 |
\end{itemize}
|
|
1348 |
\end{frame}
|
|
1349 |
|
|
1350 |
\begin{frame}[fragile]
|
|
1351 |
\frametitle{Classes: examples}
|
|
1352 |
\begin{lstlisting}
|
|
1353 |
class Bag(MyClass): # Shows how to derive classes
|
|
1354 |
def __init__(self): # called on object creation.
|
|
1355 |
self.data = [] # an instance attribute
|
|
1356 |
def add(self, x):
|
|
1357 |
self.data.append(x)
|
|
1358 |
def addtwice(self, x):
|
|
1359 |
self.add(x)
|
|
1360 |
self.add(x)
|
|
1361 |
>>> a = Bag()
|
|
1362 |
>>> a.f() # Inherited method
|
|
1363 |
'hello world'
|
|
1364 |
>>> a.add(1); a.addtwice(2)
|
|
1365 |
>>> a.data
|
|
1366 |
[1, 2, 2]
|
|
1367 |
\end{lstlisting}
|
|
1368 |
\end{frame}
|
|
1369 |
|
|
1370 |
\begin{frame}[fragile]
|
|
1371 |
\frametitle{Derived classes}
|
|
1372 |
\begin{itemize}
|
|
1373 |
\item Call the parent's \verb+__init__+ if needed
|
|
1374 |
\item If you don't need a new constructor, no need to define it in subclass
|
|
1375 |
\item Can also use the \verb+super+ built-in function
|
|
1376 |
\end{itemize}
|
|
1377 |
\begin{lstlisting}
|
|
1378 |
class AnotherBag(Bag):
|
|
1379 |
def __init__(self):
|
|
1380 |
# Must call parent's __init__ explicitly
|
|
1381 |
Bag.__init__(self)
|
|
1382 |
# Alternatively use this:
|
|
1383 |
super(AnotherBag, self).__init__()
|
|
1384 |
# Now setup any more data.
|
|
1385 |
self.more_data = []
|
|
1386 |
\end{lstlisting}
|
|
1387 |
\end{frame}
|
|
1388 |
|
|
1389 |
\begin{frame}[fragile]
|
|
1390 |
\frametitle{Classes: polymorphism}
|
|
1391 |
\begin{lstlisting}
|
|
1392 |
class Drawable(object):
|
|
1393 |
def draw(self):
|
|
1394 |
# Just a specification.
|
|
1395 |
pass
|
|
1396 |
\end{lstlisting}
|
|
1397 |
\mode<presentation>{\pause}
|
|
1398 |
\begin{lstlisting}
|
|
1399 |
class Square(Drawable):
|
|
1400 |
def draw(self):
|
|
1401 |
# draw a square.
|
|
1402 |
class Circle(Drawable):
|
|
1403 |
def draw(self):
|
|
1404 |
# draw a circle.
|
|
1405 |
\end{lstlisting}
|
|
1406 |
\mode<presentation>{\pause}
|
|
1407 |
\begin{lstlisting}
|
|
1408 |
class Artist(Drawable):
|
|
1409 |
def draw(self):
|
|
1410 |
for obj in self.drawables:
|
|
1411 |
obj.draw()
|
|
1412 |
\end{lstlisting}
|
|
1413 |
\end{frame}
|
|
1414 |
|
|
1415 |
\subsection{Miscellaneous}
|
|
1416 |
|
|
1417 |
\begin{frame}[fragile]
|
|
1418 |
\frametitle{Stand-alone scripts}
|
|
1419 |
Consider a file \typ{f.py}:
|
|
1420 |
\begin{lstlisting}
|
|
1421 |
#!/usr/bin/env python
|
|
1422 |
"""Module level documentation."""
|
|
1423 |
# First line tells the shell that it should use Python
|
|
1424 |
# to interpret the code in the file.
|
|
1425 |
def f():
|
|
1426 |
print "f"
|
|
1427 |
|
|
1428 |
# Check if we are running standalone or as module.
|
|
1429 |
# When imported, __name__ will not be '__main__'
|
|
1430 |
if __name__ == '__main__':
|
|
1431 |
# This is not executed when f.py is imported.
|
|
1432 |
f()
|
|
1433 |
\end{lstlisting}
|
|
1434 |
\end{frame}
|
|
1435 |
|
|
1436 |
\begin{frame}[fragile]
|
|
1437 |
\frametitle{List comprehensions}
|
|
1438 |
\begin{lstlisting}
|
|
1439 |
>>> veg = ['tomato', 'cabbage', 'carrot', 'potato']
|
|
1440 |
>>> [x.upper() for x in veg]
|
|
1441 |
['TOMATO', 'CABBAGE', 'CARROT', 'POTATO']
|
|
1442 |
>>> vec = range(0, 8)
|
|
1443 |
>>> even = [x for x in vec if x%2 == 0]
|
|
1444 |
>>> even
|
|
1445 |
[0, 2, 4, 6]
|
|
1446 |
>>> [x*x for x in even]
|
|
1447 |
[0, 4, 16, 36]
|
|
1448 |
>>> odd = [x for x in vec if x%2 == 1]
|
|
1449 |
>>> odd
|
|
1450 |
[1, 3, 5, 7]
|
|
1451 |
>>> [x*y for x in even for y in odd]
|
|
1452 |
[0, 0, 0, 0, 2, 6, 10, 14, 4, 12, 20, 28, 6, 18,30,42]
|
|
1453 |
\end{lstlisting}
|
|
1454 |
\end{frame}
|
|
1455 |
|
|
1456 |
\begin{frame}[fragile]
|
|
1457 |
\frametitle{File handling}
|
|
1458 |
\begin{lstlisting}
|
|
1459 |
>>> # Reading files:
|
|
1460 |
... f = open('/path/to/file_name')
|
|
1461 |
>>> data = f.read() # Read entire file.
|
|
1462 |
>>> line = f.readline() # Read one line.
|
|
1463 |
>>> # Read entire file appending each line into a list
|
|
1464 |
... lines = f.readlines()
|
|
1465 |
>>> f.close() # close the file.
|
|
1466 |
>>> # Writing files:
|
|
1467 |
... f = open('/path/to/file_name', 'w')
|
|
1468 |
>>> f.write('hello world\n')
|
|
1469 |
\end{lstlisting}
|
|
1470 |
\begin{itemize}
|
|
1471 |
\item \typ{tell()}: returns int of current position
|
|
1472 |
\item \typ{seek(pos)}: moves current position to specified byte
|
|
1473 |
\item Call \typ{close()} when done using a file
|
|
1474 |
\end{itemize}
|
|
1475 |
\end{frame}
|
|
1476 |
|
|
1477 |
\begin{frame}[fragile]
|
|
1478 |
\frametitle{Math}
|
|
1479 |
\begin{itemize}
|
|
1480 |
\item \typ{math} module provides basic math routines for
|
|
1481 |
floats
|
|
1482 |
\item \typ{cmath} module provides math routies for complex
|
|
1483 |
numbers
|
|
1484 |
\item \typ{random}: provides pseudo-random number generators
|
|
1485 |
for various distributions
|
|
1486 |
\item These are always available and part of the standard library
|
|
1487 |
\item More serious math is provided by the NumPy/SciPy modules --
|
|
1488 |
these are not standard and need to be installed separately
|
|
1489 |
\end{itemize}
|
|
1490 |
\end{frame}
|
|
1491 |
|
|
1492 |
\begin{frame}[fragile]
|
|
1493 |
\frametitle{Timing and profiling}
|
|
1494 |
\begin{itemize}
|
|
1495 |
\item Timing code: use the \typ{time} module
|
|
1496 |
\item Read up on \typ{time.time()} and \typ{time.clock()}
|
|
1497 |
\item \typ{timeit}: is a better way of doing timing
|
|
1498 |
\item IPython has handy \typ{time} and \typ{timeit} macros (type
|
|
1499 |
\typ{timeit?} for help)
|
|
1500 |
\item IPython lets you debug and profile code via the \typ{run}
|
|
1501 |
macro (type \typ{run?} on the prompt to learn more)
|
|
1502 |
\end{itemize}
|
|
1503 |
\end{frame}
|
|
1504 |
|
|
1505 |
\begin{frame}[fragile]
|
|
1506 |
\frametitle{Odds and ends}
|
|
1507 |
\begin{itemize}
|
|
1508 |
\item \typ{dir([object])} function: attributes of given object
|
|
1509 |
\item \typ{type(object)}: returns type information
|
|
1510 |
\item \typ{str(), repr()}: convert object to string representation
|
|
1511 |
\item \typ{isinstance, issubclass}
|
|
1512 |
\item \typ{assert} statements let you do debugging assertions in
|
|
1513 |
code
|
|
1514 |
\item \typ{csv} module: reading and writing CSV files
|
|
1515 |
\item \typ{pickle}: lets you save and load Python objects
|
|
1516 |
(\alert{serialization})
|
|
1517 |
\item \typ{sys.argv}: command line arguments
|
|
1518 |
\item \typ{os.path}: common path manipulations
|
|
1519 |
\item Check out the Python Library reference:
|
|
1520 |
\url{http://docs.python.org/lib/lib.html}
|
|
1521 |
\end{itemize}
|
|
1522 |
\end{frame}
|
|
1523 |
|
|
1524 |
\begin{frame}[fragile]
|
|
1525 |
\frametitle{Test driven development (TDD)}
|
|
1526 |
\begin{itemize}
|
|
1527 |
\item Why?
|
|
1528 |
\begin{itemize}
|
|
1529 |
|
|
1530 |
\item Forces you to write reusable code!
|
|
1531 |
|
|
1532 |
\item Think about the API
|
|
1533 |
|
|
1534 |
\item More robust
|
|
1535 |
|
|
1536 |
\item Makes refactoring very easy
|
|
1537 |
|
|
1538 |
\end{itemize}
|
|
1539 |
\item How? Python offers three major ways of doing this
|
|
1540 |
\begin{itemize}
|
|
1541 |
\item doctest
|
|
1542 |
\item unittest
|
|
1543 |
\item nosetest (and similar like py.test)
|
|
1544 |
\end{itemize}
|
|
1545 |
|
|
1546 |
\item Test every piece of functionality you offer
|
|
1547 |
|
|
1548 |
\item This isn't a formal introduction but more a practical one
|
|
1549 |
|
|
1550 |
\end{itemize}
|
|
1551 |
\end{frame}
|
|
1552 |
|
|
1553 |
\begin{frame}[fragile]
|
|
1554 |
\frametitle{Unit test}
|
|
1555 |
\begin{lstlisting}
|
|
1556 |
import unittest
|
|
1557 |
|
|
1558 |
class MyTestCase(unittest.TestCase):
|
|
1559 |
def setUp(self):
|
|
1560 |
# Called *before* each test_*
|
|
1561 |
def tearDown(self):
|
|
1562 |
# Called *after* each test_*
|
|
1563 |
def test_something(self):
|
|
1564 |
"docstring"
|
|
1565 |
# Test code.
|
|
1566 |
self.assertEqual(x, y)
|
|
1567 |
self.assertRaises(ValueError, func, arg1, arg2 ...)
|
|
1568 |
|
|
1569 |
if __name__ == '__main__':
|
|
1570 |
unittest.main()
|
|
1571 |
\end{lstlisting}
|
|
1572 |
\end{frame}
|
|
1573 |
|
|
1574 |
\begin{frame}[fragile]
|
|
1575 |
\frametitle{Nosetest}
|
|
1576 |
\begin{lstlisting}
|
|
1577 |
import particle
|
|
1578 |
def test_particle():
|
|
1579 |
# Use asserts here.
|
|
1580 |
p = particle.Particle(1.0)
|
|
1581 |
assert p.property[0] == 1.0
|
|
1582 |
assert p.property[2] == 0.0
|
|
1583 |
|
|
1584 |
if __name__ == '__main__':
|
|
1585 |
import nose
|
|
1586 |
nose.main()
|
|
1587 |
\end{lstlisting}
|
|
1588 |
\end{frame}
|
|
1589 |
|
|
1590 |
\begin{frame}[fragile]
|
|
1591 |
\frametitle{Testing}
|
|
1592 |
\begin{itemize}
|
|
1593 |
\item More details: see library reference and search for nosetest
|
|
1594 |
\end{itemize}
|
|
1595 |
\end{frame}
|
|
1596 |
|
|
1597 |
\section{Numerics \& Plotting}
|
|
1598 |
|
|
1599 |
\subsection{NumPy Arrays}
|
|
1600 |
|
|
1601 |
\subsection{Plotting: Matplotlib}
|
|
1602 |
|
|
1603 |
\begin{frame}
|
|
1604 |
\frametitle{About \texttt{matplotlib}}
|
|
1605 |
\begin{itemize}
|
|
1606 |
\item Easy to use, scriptable, ``Matlab-like'' 2D plotting
|
|
1607 |
\item Publication quality figures and interactive capabilities
|
|
1608 |
\item Plots, histograms, power spectra, bar charts, errorcharts,
|
|
1609 |
scatterplots, etc.
|
|
1610 |
\item Also does polar plots, maps, contours
|
|
1611 |
\item Support for simple \TeX\ markup
|
|
1612 |
\item Multiple output backends (images, EPS, SVG, wx, Agg, Tk, GTK)
|
|
1613 |
\item Cross-platform: Linux, Win32, Mac OS X
|
|
1614 |
\item Good idea to use via IPython: \typ{ipython -pylab}
|
|
1615 |
\item From scripts use: \typ{import pylab}
|
|
1616 |
\end{itemize}
|
|
1617 |
\end{frame}
|
|
1618 |
|
|
1619 |
\begin{frame}
|
|
1620 |
\frametitle{More information}
|
|
1621 |
\begin{itemize}
|
|
1622 |
\item More information here: \url{http://matplotlib.sf.net}
|
|
1623 |
\item \url{http://matplotlib.sf.net/tutorial.html}
|
|
1624 |
\item \url{http://matplotlib.sf.net/screenshots.html}
|
|
1625 |
\end{itemize}
|
|
1626 |
\end{frame}
|
|
1627 |
|
|
1628 |
\begin{frame}[fragile]
|
|
1629 |
\frametitle{Basic plotting with \texttt{matplotlib}}
|
|
1630 |
\begin{lstlisting}
|
|
1631 |
>>> x = arange(0, 2*pi, 0.05)
|
|
1632 |
>>> plot(x, sin(x)) # Same as plot(x, sin(x), 'b-')
|
|
1633 |
>>> plot(x, sin(x), 'ro')
|
|
1634 |
>>> axis([0,2*pi, -1,1])
|
|
1635 |
>>> xlabel(r'$\chi$', color='g')
|
|
1636 |
>>> ylabel(r'sin($\chi$)', color='r')
|
|
1637 |
>>> title('A simple figure', fontsize=20)
|
|
1638 |
>>> savefig('/tmp/test.eps')
|
|
1639 |
# Multiple plots in one figure
|
|
1640 |
>>> t = arange(0.0, 5.2, 0.2)
|
|
1641 |
# red dashes, blue squares and green triangles
|
|
1642 |
>>> plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
|
|
1643 |
\end{lstlisting}
|
|
1644 |
\end{frame}
|
|
1645 |
|
|
1646 |
\begin{frame}[fragile]
|
|
1647 |
\frametitle{Basic plotting \ldots}
|
|
1648 |
\begin{lstlisting}
|
|
1649 |
# Set properties of objects:
|
|
1650 |
>>> plot(x, sin(x), linewidth=2.0, color='r')
|
|
1651 |
>>> l, = plot(x, sin(x))
|
|
1652 |
>>> setp(l, linewidth=2.0, color='r')
|
|
1653 |
>>> l.set_linewidth(2.0); l.set_color('r')
|
|
1654 |
>>> draw() # Redraws current figure.
|
|
1655 |
>>> setp(l) # Prints available properties
|
|
1656 |
>>> close() # Closes the figure.
|
|
1657 |
# Multiple figures:
|
|
1658 |
>>> figure(1); plot(x, sin(x))
|
|
1659 |
>>> figure(2); plot(x, tanh(x))
|
|
1660 |
>>> figure(1); title('Easy as 1,2,3')
|
|
1661 |
\end{lstlisting}
|
|
1662 |
\end{frame}
|
|
1663 |
|
|
1664 |
\begin{frame}[fragile]
|
|
1665 |
\frametitle{Basic plotting \ldots}
|
|
1666 |
\begin{lstlisting}
|
|
1667 |
>>> figure(1)
|
|
1668 |
>>> subplot(211) # Same as subplot(2, 1, 1)
|
|
1669 |
>>> plot(x, cos(5*x)*exp(-x))
|
|
1670 |
>>> subplot(2, 1, 2)
|
|
1671 |
>>> plot(x, cos(5*x), 'r--', label='cosine')
|
|
1672 |
>>> plot(x, sin(5*x), 'g--', label='sine')
|
|
1673 |
>>> legend() # Or legend(['cosine', 'sine'])
|
|
1674 |
>>> text(1,0, '(1,0)')
|
|
1675 |
>>> axes = gca() # Current axis
|
|
1676 |
>>> fig = gcf() # Current figure
|
|
1677 |
\end{lstlisting}
|
|
1678 |
\end{frame}
|
|
1679 |
|
|
1680 |
|
|
1681 |
\subsection{SciPy}
|
|
1682 |
|
|
1683 |
\begin{frame}
|
|
1684 |
\frametitle{Using \texttt{SciPy}}
|
|
1685 |
\begin{itemize}
|
|
1686 |
\item SciPy is Open Source software for mathematics, science, and
|
|
1687 |
engineering
|
|
1688 |
\item \typ{import scipy}
|
|
1689 |
\item Built on NumPy
|
|
1690 |
\item Provides modules for statistics, optimization, integration,
|
|
1691 |
linear algebra, Fourier transforms, signal and image processing,
|
|
1692 |
genetic algorithms, ODE solvers, special functions, and more
|
|
1693 |
\item Used widely by scientists world over
|
|
1694 |
\item Details are beyond the scope of this tutorial
|
|
1695 |
\end{itemize}
|
|
1696 |
\end{frame}
|
|
1697 |
|
|
1698 |
\section{Standard library}
|
|
1699 |
|
|
1700 |
\subsection{Quick Tour}
|
|
1701 |
|
|
1702 |
\begin{frame}
|
|
1703 |
\frametitle{Standard library}
|
|
1704 |
\begin{itemize}
|
|
1705 |
\item Very powerful
|
|
1706 |
\item ``Batteries included''
|
|
1707 |
\item Example standard modules taken from the tutorial
|
|
1708 |
\begin{itemize}
|
|
1709 |
\item Operating system interface: \typ{os}
|
|
1710 |
\item System, Command line arguments: \typ{sys}
|
|
1711 |
\item Regular expressions: \typ{re}
|
|
1712 |
\item Math: \typ{math}, \typ{random}
|
|
1713 |
\item Internet access: \typ{urllib2}, \typ{smtplib}
|
|
1714 |
\item Data compression: \typ{zlib}, \typ{gzip}, \typ{bz2},
|
|
1715 |
\typ{zipfile}, and \typ{tarfile}
|
|
1716 |
\item Unit testing: \typ{doctest} and \typ{unittest}
|
|
1717 |
\item And a whole lot more!
|
|
1718 |
\end{itemize}
|
|
1719 |
\item Check out the Python Library reference:
|
|
1720 |
\url{http://docs.python.org/lib/lib.html}
|
|
1721 |
\end{itemize}
|
|
1722 |
\end{frame}
|
|
1723 |
|
|
1724 |
\begin{frame}[fragile]
|
|
1725 |
\frametitle{Stdlib: examples}
|
|
1726 |
\begin{lstlisting}
|
|
1727 |
>>> import os
|
|
1728 |
>>> os.system('date')
|
|
1729 |
Fri Jun 10 22:13:09 IST 2005
|
|
1730 |
0
|
|
1731 |
>>> os.getcwd()
|
|
1732 |
'/home/prabhu'
|
|
1733 |
>>> os.chdir('/tmp')
|
|
1734 |
>>> import os
|
|
1735 |
>>> dir(os)
|
|
1736 |
<returns a list of all module functions>
|
|
1737 |
>>> help(os)
|
|
1738 |
<extensive manual page from module's docstrings>
|
|
1739 |
\end{lstlisting}
|
|
1740 |
\end{frame}
|
|
1741 |
|
|
1742 |
\begin{frame}[fragile]
|
|
1743 |
\frametitle{Stdlib: examples}
|
|
1744 |
\begin{lstlisting}
|
|
1745 |
>>> import sys
|
|
1746 |
>>> # Print the list of command line args to Python
|
|
1747 |
... print sys.argv
|
|
1748 |
['']
|
|
1749 |
>>> import re # Regular expressions
|
|
1750 |
>>> re.findall(r'\bf[a-z]*',
|
|
1751 |
... 'which foot or hand fell fastest')
|
|
1752 |
['foot', 'fell', 'fastest']
|
|
1753 |
>>> re.sub(r'(\b[a-z]+) \1', r'\1',
|
|
1754 |
... 'cat in the the hat')
|
|
1755 |
'cat in the hat'
|
|
1756 |
\end{lstlisting}
|
|
1757 |
\end{frame}
|
|
1758 |
|
|
1759 |
\begin{frame}[fragile]
|
|
1760 |
\frametitle{Stdlib: examples}
|
|
1761 |
\begin{lstlisting}
|
|
1762 |
>>> import math
|
|
1763 |
>>> math.cos(math.pi / 4.0)
|
|
1764 |
0.70710678118654757
|
|
1765 |
>>> math.log(1024, 2)
|
|
1766 |
10.0
|
|
1767 |
>>> import random
|
|
1768 |
>>> random.choice(['apple', 'pear', 'banana'])
|
|
1769 |
'pear'
|
|
1770 |
\end{lstlisting}
|
|
1771 |
\end{frame}
|
|
1772 |
|
|
1773 |
\begin{frame}[fragile]
|
|
1774 |
\frametitle{Stdlib: examples}
|
|
1775 |
\begin{lstlisting}
|
|
1776 |
>>> import urllib2
|
|
1777 |
>>> f = urllib2.urlopen('http://www.python.org/')
|
|
1778 |
>>> print f.read(100)
|
|
1779 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
1780 |
<?xml-stylesheet href="./css/ht2html
|
|
1781 |
\end{lstlisting}
|
|
1782 |
\end{frame}
|
|
1783 |
|
|
1784 |
\begin{frame}[fragile]
|
|
1785 |
\frametitle{Stdlib: examples}
|
|
1786 |
\begin{lstlisting}
|
|
1787 |
>>> import zlib
|
|
1788 |
>>> s = 'witch which has which witches wrist watch'
|
|
1789 |
>>> len(s)
|
|
1790 |
41
|
|
1791 |
>>> t = zlib.compress(s)
|
|
1792 |
>>> len(t)
|
|
1793 |
37
|
|
1794 |
>>> zlib.decompress(t)
|
|
1795 |
'witch which has which witches wrist watch'
|
|
1796 |
>>> zlib.crc32(t)
|
|
1797 |
-1438085031
|
|
1798 |
\end{lstlisting}
|
|
1799 |
\end{frame}
|
|
1800 |
|
|
1801 |
\begin{frame}
|
|
1802 |
\frametitle{Summary}
|
|
1803 |
\begin{itemize}
|
|
1804 |
\item Introduced Python
|
|
1805 |
\item Basic syntax
|
|
1806 |
\item Basic types and data structures
|
|
1807 |
\item Control flow
|
|
1808 |
\item Functions
|
|
1809 |
\item Modules
|
|
1810 |
\item Exceptions
|
|
1811 |
\item Classes
|
|
1812 |
\item Standard library
|
|
1813 |
\end{itemize}
|
|
1814 |
\end{frame}
|
|
1815 |
|
|
1816 |
\end{document}
|
|
1817 |
|
|
1818 |
\subsection{Basic data structures}
|
|
1819 |
\begin{frame}{Lists}
|
|
1820 |
\begin{itemize}
|
|
1821 |
\item \texttt{species = [ 'humans', 'orcs', 'elves', 'dwarves' ]}
|
|
1822 |
\item \texttt{ ids = [ 107, 109, 124, 141, 142, 144 ]}
|
|
1823 |
\item \texttt{ oneliners = [ 'I will be back', 'Do or do not! No try!!', 42 ] }
|
|
1824 |
\end{itemize}
|
|
1825 |
|
|
1826 |
\begin{block}{List operations}
|
|
1827 |
ids + [ 100, 102 ]\\
|
|
1828 |
species.append( 'unicorns')\\
|
|
1829 |
print oneliners[ 1 ]\\
|
|
1830 |
look up \alert{docs.python.org/tutorial/datastructures.html}
|
|
1831 |
\end{block}
|
|
1832 |
\end{frame}
|
|
1833 |
\end{document}
|
|
1834 |
\section{Python Tutorial}
|
|
1835 |
\subsection{Preliminaries}
|
|
1836 |
\begin{frame}
|
|
1837 |
\frametitle{Using the interpreter}
|
|
1838 |
\begin{itemize}
|
|
1839 |
\item Starting up: \typ{python} or \typ{ipython}
|
|
1840 |
\item Quitting: \typ{Control-D} or \typ{Control-Z} (on Win32)
|
|
1841 |
\item Can use it like a calculator
|
|
1842 |
\item Can execute one-liners via the \typ{-c} option:
|
|
1843 |
\typ{python -c "print 'hello world'"}
|
|
1844 |
\item Other options via \typ{python -h}
|
|
1845 |
\end{itemize}
|
|
1846 |
\end{frame}
|
|
1847 |
|
|
1848 |
\begin{frame}
|
|
1849 |
\frametitle{IPython}
|
|
1850 |
\begin{itemize}
|
|
1851 |
\item Recommended interpreter, IPython:
|
|
1852 |
\url{http://ipython.scipy.org}
|
|
1853 |
\item Better than the default Python shell
|
|
1854 |
\item Supports tab completion by default
|
|
1855 |
\item Easier object introspection
|
|
1856 |
\item Shell access!
|
|
1857 |
\item Command system to allow extending its own behavior
|
|
1858 |
\item Supports history (across sessions) and logging
|
|
1859 |
\item Can be embedded in your own Python code
|
|
1860 |
\item Support for macros
|
|
1861 |
\item A flexible framework for your own custom interpreter
|
|
1862 |
\item Other miscellaneous conveniences
|
|
1863 |
\item We'll get back to this later
|
|
1864 |
\end{itemize}
|
|
1865 |
\end{frame}
|
|
1866 |
|
|
1867 |
\begin{frame}[fragile]
|
|
1868 |
\frametitle{Basic IPython features}
|
|
1869 |
\begin{itemize}
|
|
1870 |
\item Startup: \verb+ipython [options] files+
|
|
1871 |
\begin{itemize}
|
|
1872 |
\item \verb+ipython [-wthread|-gthread|-qthread]+:
|
|
1873 |
Threading modes to support wxPython, pyGTK and Qt
|
|
1874 |
\item \verb+ipython -pylab+: Support for matplotlib
|
|
1875 |
\end{itemize}
|
|
1876 |
\item TAB completion:
|
|
1877 |
\begin{itemize}
|
|
1878 |
\item Type \verb+object_name.<TAB>+ to see list of options
|
|
1879 |
\item Also completes on file and directory names
|
|
1880 |
\end{itemize}
|
|
1881 |
\item \verb+object?+ shows docstring/help for any Python object
|
|
1882 |
\item \verb+object??+ presents more docs (and source if possible)
|
|
1883 |
\item Debugging with \verb+%pdb+ magic: pops up pdb on errors
|
|
1884 |
\item Access history (saved over earlier sessions also)
|
|
1885 |
\begin{itemize}
|
|
1886 |
\item Use \texttt{<UpArrow>}: move up history
|
|
1887 |
\item Use \texttt{<Ctrl-r> string}: search history backwards
|
|
1888 |
\item Use \texttt{Esc >}: get back to end of history
|
|
1889 |
\end{itemize}
|
|
1890 |
\item \verb+%run [options] file[.py]+ lets you run Python code
|
|
1891 |
\end{itemize}
|
|
1892 |
\end{frame}
|
|
1893 |
% LocalWords: BDFL Guido Rossum PSF Nokia OO Zope CMS RedHat SciPy MayaVi spam
|
|
1894 |
% LocalWords: IPython ipython stdin TypeError dict int elif PYTHONPATH IOError
|
|
1895 |
% LocalWords: namespace Namespaces SyntaxError ZeroDivisionError NameError str
|
|
1896 |
% LocalWords: ValueError subclassed def
|
|
1897 |
|
|
1898 |
|
|
1899 |
\item Types are of two kinds: \alert{mutable} and \alert{immutable}
|
|
1900 |
\item Immutable types: numbers, strings, \typ{None} and tuples
|
|
1901 |
\item Immutables cannot be changed ``in-place''
|
|
1902 |
\item Mutable types: lists, dictionaries, instances, etc.
|
|
1903 |
\item Mutable objects can be ``changed''
|
|
1904 |
\end{itemize}
|
|
1905 |
|
|
1906 |
|
|
1907 |
\begin{frame}
|
|
1908 |
\frametitle{Important!}
|
|
1909 |
\begin{itemize}
|
|
1910 |
\item Assignment to an object is by reference
|
|
1911 |
\item Essentially, \alert{names are bound to objects}
|
|
1912 |
\end{itemize}
|
|
1913 |
\end{frame}
|
|
1914 |
|
|
1915 |
|
|
1916 |
\end{document}
|
|
1917 |
\begin{frame}[fragile]
|
|
1918 |
\frametitle{Dictionaries}
|
|
1919 |
\begin{itemize}
|
|
1920 |
\item Associative arrays/mappings
|
|
1921 |
\item Indexed by ``keys'' (keys must be immutable)
|
|
1922 |
\item \typ{dict[key] = value}
|
|
1923 |
\item \typ{keys()} returns all keys of the dict
|
|
1924 |
\item \typ{values()} returns the values of the dict
|
|
1925 |
\item \verb+has_key(key)+ returns if \typ{key} is in the dict
|
|
1926 |
\end{itemize}
|
|
1927 |
\end{frame}
|
|
1928 |
|
|
1929 |
\begin{frame}[fragile]
|
|
1930 |
\frametitle{Dictionaries: example}
|
|
1931 |
\begin{lstlisting}
|
|
1932 |
>>> tel = {'jack': 4098, 'sape': 4139}
|
|
1933 |
>>> tel['guido'] = 4127
|
|
1934 |
>>> tel
|
|
1935 |
{'sape': 4139, 'guido': 4127, 'jack': 4098}
|
|
1936 |
>>> tel['jack']
|
|
1937 |
4098
|
|
1938 |
>>> del tel['sape']
|
|
1939 |
>>> tel['irv'] = 4127
|
|
1940 |
>>> tel
|
|
1941 |
{'guido': 4127, 'irv': 4127, 'jack': 4098}
|
|
1942 |
>>> tel.keys()
|
|
1943 |
['guido', 'irv', 'jack']
|
|
1944 |
>>> tel.has_key('guido')
|
|
1945 |
True
|
|
1946 |
\end{lstlisting}
|
|
1947 |
\end{frame}
|
|
1948 |
|
|
1949 |
\subsection{Control flow, functions}
|
|
1950 |
|
|
1951 |
|
|
1952 |
|
|
1953 |
\begin{frame}[fragile]
|
|
1954 |
\frametitle{\typ{If} example}
|
|
1955 |
\begin{lstlisting}
|
|
1956 |
>>> a = ['cat', 'window', 'defenestrate']
|
|
1957 |
>>> if 'cat' in a:
|
|
1958 |
... print "meaw"
|
|
1959 |
...
|
|
1960 |
meaw
|
|
1961 |
>>> pets = {'cat': 1, 'dog':2, 'croc': 10}
|
|
1962 |
>>> if 'croc' in pets:
|
|
1963 |
... print pets['croc']
|
|
1964 |
...
|
|
1965 |
10
|
|
1966 |
\end{lstlisting}
|
|
1967 |
\end{frame}
|
|
1968 |
|
|
1969 |
\begin{frame}[fragile]
|
|
1970 |
\frametitle{\typ{for} example}
|
|
1971 |
\begin{lstlisting}
|
|
1972 |
>>> a = ['cat', 'window', 'defenestrate']
|
|
1973 |
>>> for x in a:
|
|
1974 |
... print x, len(x)
|
|
1975 |
...
|
|
1976 |
cat 3
|
|
1977 |
window 6
|
|
1978 |
defenestrate 12
|
|
1979 |
>>> knights = {'gallahad': 'the pure',
|
|
1980 |
... 'robin': 'the brave'}
|
|
1981 |
>>> for k, v in knights.iteritems():
|
|
1982 |
... print k, v
|
|
1983 |
...
|
|
1984 |
gallahad the pure
|
|
1985 |
robin the brave
|
|
1986 |
\end{lstlisting}
|
|
1987 |
\end{frame}
|