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 |
\frametitle{Outline}
|
|
119 |
\tableofcontents
|
|
120 |
\end{frame}
|
|
121 |
\section{Pythonicity}
|
|
122 |
\begin{frame}[fragile]
|
|
123 |
\frametitle{The Zen of Python}
|
|
124 |
|
|
125 |
Try this!
|
|
126 |
|
|
127 |
\begin{lstlisting}
|
|
128 |
>>> import this
|
|
129 |
\end{lstlisting}
|
|
130 |
|
|
131 |
\end{frame}
|
|
132 |
|
|
133 |
\begin{frame}
|
|
134 |
{Style Guide}
|
|
135 |
|
|
136 |
Read PEP8
|
|
137 |
|
|
138 |
\url{http://www.python.org/dev/peps/pep-0008/}
|
|
139 |
|
|
140 |
\inctime{10}
|
|
141 |
\end{frame}
|
|
142 |
\section{More Python Machinery}
|
|
143 |
\subsection{Objects}
|
|
144 |
\begin{frame}{Objects in Python}
|
|
145 |
\begin{itemize}
|
|
146 |
\item What is an Object? (Types and classes)
|
|
147 |
\item identity
|
|
148 |
\item type
|
|
149 |
\item method
|
|
150 |
\end{itemize}
|
|
151 |
\end{frame}
|
|
152 |
|
|
153 |
\begin{frame}[fragile]
|
|
154 |
\frametitle{Why are they useful?}
|
|
155 |
\small
|
|
156 |
\begin{lstlisting}
|
|
157 |
for element in (1, 2, 3):
|
|
158 |
print element
|
|
159 |
for key in {'one':1, 'two':2}:
|
|
160 |
print key
|
|
161 |
for char in "123":
|
|
162 |
print char
|
|
163 |
for line in open("myfile.txt"):
|
|
164 |
print line
|
|
165 |
for line in urllib2.urlopen('http://site.com'):
|
|
166 |
print line
|
|
167 |
\end{lstlisting}
|
|
168 |
\end{frame}
|
|
169 |
\begin{frame}{And the winner is \ldots OBJECTS!}
|
|
170 |
All objects providing a similar inteface can be used the same way.\\
|
|
171 |
Functions (and others) are first-class objects. Can be passed to and returned from functions.
|
|
172 |
\inctime{10}
|
|
173 |
\end{frame}
|
|
174 |
\subsection{Dictionary}
|
|
175 |
\begin{frame}{Dictionary}
|
|
176 |
\begin{itemize}
|
|
177 |
\item aka associative arrays, key-value pairs, hashmaps, hashtables \ldots
|
|
178 |
\item \typ{ d = \{ ``Hitchhiker's guide'' : 42, ``Terminator'' : ``I'll be back''\}}
|
|
179 |
\item lists and tuples index: 0 \ldots n
|
|
180 |
\item dictionaries index using strings
|
|
181 |
\item aka key-value pairs
|
|
182 |
\item what can be keys?
|
|
183 |
\end{itemize}
|
|
184 |
\end{frame}
|
|
185 |
|
|
186 |
\begin{frame}{Dict \ldots }
|
|
187 |
\begin{itemize}
|
|
188 |
\item \alert{Unordered}
|
|
189 |
\begin{block}{Standard usage}
|
|
190 |
for key in dict:\\
|
|
191 |
<use> dict[key] \# => value
|
|
192 |
\end{block}
|
|
193 |
\item \typ{d.keys()} returns a list
|
|
194 |
\item can we have duplicate keys?
|
|
195 |
\end{itemize}
|
|
196 |
\end{frame}
|
|
197 |
\begin{frame} {Problem Set 2.1}
|
|
198 |
\begin{description}
|
|
199 |
\item[2.1.1] You are given date strings of the form ``29, Jul 2009'', or ``4 January 2008''. In other words a number a string and another number, with a comma sometimes separating the items.Write a function that takes such a string and returns a tuple (yyyy, mm, dd) where all three elements are ints.
|
|
200 |
\item[2.1.2] Count word frequencies in a file.
|
|
201 |
\item[2.1.3] Find the most used Python keywords in your Python code (import keyword).
|
|
202 |
\end{description}
|
|
203 |
|
|
204 |
\inctime{20}
|
|
205 |
\end{frame}
|
|
206 |
|
|
207 |
\subsection{Set}
|
|
208 |
\begin{frame}[fragile]
|
|
209 |
\frametitle{Set}
|
|
210 |
\begin{itemize}
|
|
211 |
\item Simplest container, mutable
|
|
212 |
\item No ordering, no duplicates
|
|
213 |
\item usual suspects: union, intersection, subset \ldots
|
|
214 |
\item >, >=, <, <=, in, \ldots
|
|
215 |
\end{itemize}
|
|
216 |
\begin{lstlisting}
|
|
217 |
f10 = set([1,2,3,5,8])
|
|
218 |
p10 = set([2,3,5,7])
|
|
219 |
f10|p10, f10&p10
|
|
220 |
f10-p10, p10-f10, f10^p10
|
|
221 |
set([2,3]) < p10, set([2,3]) <= p10
|
|
222 |
2 in p10, 4 in p10
|
|
223 |
len(f10)
|
|
224 |
\end{lstlisting}
|
|
225 |
\end{frame}
|
|
226 |
|
|
227 |
\begin{frame}
|
|
228 |
\frametitle{Problem set 2.2}
|
|
229 |
\begin{description}
|
|
230 |
\item[2.2.1] Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these?
|
|
231 |
\item[2.2.2] Given a string of the form ``4-7, 9, 12, 15'' find the numbers missing in this list for a given range.
|
|
232 |
\end{description}
|
|
233 |
\inctime{15}
|
|
234 |
\end{frame}
|
|
235 |
|
|
236 |
\subsection{Functions Reloaded!}
|
|
237 |
\begin{frame}[fragile]
|
|
238 |
\frametitle{Advanced functions}
|
|
239 |
\begin{itemize}
|
|
240 |
\item default args
|
|
241 |
\item varargs
|
|
242 |
\item keyword args
|
|
243 |
\item scope
|
|
244 |
\item \typ{global}
|
|
245 |
\end{itemize}
|
|
246 |
\end{frame}
|
|
247 |
|
|
248 |
\begin{frame}[fragile]
|
|
249 |
\frametitle{Functions: default arguments}
|
|
250 |
\begin{lstlisting}
|
|
251 |
def ask_ok(prompt, retries=4, complaint='Yes or no!'):
|
|
252 |
while True:
|
|
253 |
ok = raw_input(prompt)
|
|
254 |
if ok in ('y', 'ye', 'yes'):
|
|
255 |
return True
|
|
256 |
if ok in ('n', 'no', 'nop', 'nope'):
|
|
257 |
return False
|
|
258 |
retries = retries - 1
|
|
259 |
if retries < 0:
|
|
260 |
raise IOError, 'bad user'
|
|
261 |
print complaint
|
|
262 |
\end{lstlisting}
|
|
263 |
\end{frame}
|
|
264 |
|
|
265 |
\begin{frame}[fragile]
|
|
266 |
\frametitle{Functions: keyword arguments}
|
|
267 |
\small
|
|
268 |
\begin{lstlisting}
|
|
269 |
def parrot(voltage, state='a stiff',
|
|
270 |
action='voom', type='Royal Blue'):
|
|
271 |
print "-- This parrot wouldn't", action,
|
|
272 |
print "if you supply", voltage, "Volts."
|
|
273 |
print "-- Lovely plumage, the", type
|
|
274 |
print "-- It's", state, "!"
|
|
275 |
|
|
276 |
parrot(1000)
|
|
277 |
parrot(action = 'VOOOOOM', voltage = 1000000)
|
|
278 |
parrot('a thousand', state = 'pushing up the daisies')
|
|
279 |
parrot('a million', 'bereft of life', 'jump')
|
|
280 |
\end{lstlisting}
|
|
281 |
\end{frame}
|
|
282 |
|
|
283 |
\begin{frame}[fragile]
|
|
284 |
\frametitle{Functions: arbitrary argument lists}
|
|
285 |
\begin{itemize}
|
|
286 |
\item Arbitrary number of arguments using \verb+*args+ or
|
|
287 |
\verb+*whatever+
|
|
288 |
\item Keyword arguments using \verb+**kw+
|
|
289 |
\item Given a tuple/dict how do you call a function?
|
|
290 |
\begin{itemize}
|
|
291 |
\item Using argument unpacking
|
|
292 |
\item For positional arguments: \verb+foo(*[5, 10])+
|
|
293 |
\item For keyword args: \verb+foo(**{'a':5, 'b':10})+
|
|
294 |
\end{itemize}
|
|
295 |
\end{itemize}
|
|
296 |
\end{frame}
|
|
297 |
|
|
298 |
\begin{frame}[fragile]
|
|
299 |
\begin{lstlisting}
|
|
300 |
def foo(a=10, b=100):
|
|
301 |
print a, b
|
|
302 |
def func(*args, **keyword):
|
|
303 |
print args, keyword
|
|
304 |
# Unpacking:
|
|
305 |
args = [5, 10]
|
|
306 |
foo(*args)
|
|
307 |
kw = {'a':5, 'b':10}
|
|
308 |
foo(**kw)
|
|
309 |
\end{lstlisting}
|
|
310 |
\inctime{15}
|
|
311 |
\end{frame}
|
|
312 |
|
|
313 |
\subsection{Functional programming}
|
|
314 |
\begin{frame}[fragile]
|
|
315 |
\frametitle{Functional programming}
|
|
316 |
What is the basic idea?\\
|
|
317 |
Why is it interesting?\\
|
|
318 |
\typ{map, reduce, filter}\\
|
|
319 |
list comprehension\\
|
|
320 |
generators
|
|
321 |
\inctime{10}
|
|
322 |
\end{frame}
|
|
323 |
\end{document}
|
|
324 |
|
|
325 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|