141 \end{lstlisting} |
141 \end{lstlisting} |
142 \end{frame} |
142 \end{frame} |
143 |
143 |
144 \begin{frame}[fragile] |
144 \begin{frame}[fragile] |
145 \frametitle{Functions: default arguments \ldots} |
145 \frametitle{Functions: default arguments \ldots} |
146 \small |
146 \begin{lstlisting} |
147 \begin{lstlisting} |
147 In []: def welcome(greet, name="World"): |
148 def ask_ok(prompt, complaint='Yes or no!'): |
148 .... print greet, name |
149 while True: |
149 |
150 ok = raw_input(prompt) |
150 In []: welcome("Hello") |
151 if ok in ('y', 'ye', 'yes'): |
151 Hello World |
152 return True |
152 |
153 if ok in ('n', 'no', 'nop', |
153 In []: welcome("Hi", "Guido") |
154 'nope'): |
154 Hi Guido |
155 return False |
|
156 print complaint |
|
157 |
|
158 ask_ok('?') |
|
159 ask_ok('?', '[Y/N]') |
|
160 \end{lstlisting} |
155 \end{lstlisting} |
161 \end{frame} |
156 \end{frame} |
162 |
157 |
163 \subsection{Keyword arguments} |
158 \subsection{Keyword arguments} |
164 \begin{frame}[fragile] |
159 \begin{frame}[fragile] |
165 \frametitle{Functions: Keyword arguments} |
160 \frametitle{Functions: Keyword arguments} |
166 We have seen the following |
161 We have seen the following |
167 \begin{lstlisting} |
162 \begin{lstlisting} |
168 In []: legend(['sin(2y)'], |
163 In []: legend(['sin(2y)'], |
169 loc='center') |
164 loc = 'center') |
|
165 |
170 In []: plot(y, sin(y), 'g', |
166 In []: plot(y, sin(y), 'g', |
171 linewidth=2) |
167 linewidth = 2) |
|
168 |
172 In []: annotate('local max', |
169 In []: annotate('local max', |
173 xy=(1.5, 1)) |
170 xy = (1.5, 1)) |
|
171 |
174 In []: pie(science.values(), |
172 In []: pie(science.values(), |
175 labels=science.keys()) |
173 labels = science.keys()) |
176 \end{lstlisting} |
174 \end{lstlisting} |
177 \end{frame} |
175 \end{frame} |
178 |
176 |
179 \begin{frame}[fragile] |
177 \begin{frame}[fragile] |
180 \frametitle{Functions: keyword arguments \ldots} |
178 \frametitle{Functions: keyword arguments \ldots} |
181 \small |
179 \begin{lstlisting} |
182 \begin{lstlisting} |
180 In []: def welcome(greet, name="World"): |
183 def ask_ok(prompt, complaint='Yes or no!'): |
181 .... print greet, name |
184 while True: |
182 |
185 ok = raw_input(prompt) |
183 In []: welcome("Hello", "James") |
186 if ok in ('y', 'ye', 'yes'): |
184 Hello James |
187 return True |
185 |
188 if ok in ('n', 'no', 'nop', |
186 In []: welcome("Hi", name="Guido") |
189 'nope'): |
187 Hi Guido |
190 return False |
188 |
191 print complaint |
189 In []: welcome(name="Guido", greet="Hey") |
192 |
190 Hey Guido |
193 ask_ok(prompt='?') |
191 \end{lstlisting} |
194 ask_ok(prompt='?', complaint='[y/n]') |
|
195 ask_ok(complaint='[y/n]', prompt='?') |
|
196 \end{lstlisting} |
|
197 \end{frame} |
192 \end{frame} |
198 |
193 |
199 \subsection{Built-in functions} |
194 \subsection{Built-in functions} |
200 \begin{frame} |
195 \begin{frame} |
201 {Before writing a function} |
196 {Before writing a function} |
202 \begin{itemize} |
197 \begin{itemize} |
203 \item Variety of builtin functions are available |
198 \item Variety of built-in functions are available |
204 \item \typ{abs, any, all, len, max, min} |
199 \item \typ{abs, any, all, len, max, min} |
205 \item \typ{pow, range, sum, type} |
200 \item \typ{pow, range, sum, type} |
206 \item Refer here: |
201 \item Refer here: |
207 \url{http://docs.python.org/library/functions.html} |
202 \url{http://docs.python.org/library/functions.html} |
208 \end{itemize} |
203 \end{itemize} |
219 A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and is also not primitive. |
214 A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and is also not primitive. |
220 \end{frame} |
215 \end{frame} |
221 |
216 |
222 \begin{frame}{Problem 3.3} |
217 \begin{frame}{Problem 3.3} |
223 Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).} |
218 Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).} |
|
219 |
224 \inctime{15} |
220 \inctime{15} |
225 \end{frame} |
221 \end{frame} |
226 |
222 |
227 \section{Modules} |
223 \section{Modules} |
228 \begin{frame}[fragile] |
224 \begin{frame}[fragile] |
229 \frametitle{\typ{from} \ldots \typ{import} magic} |
225 \frametitle{\texttt{from} \ldots \texttt{import} magic} |
230 \begin{lstlisting} |
226 \begin{lstlisting} |
231 from scipy.interpolate import splrep |
|
232 from scipy.interpolate import splev |
|
233 |
|
234 from scipy.integrate import quad |
|
235 from scipy.integrate import odeint |
227 from scipy.integrate import odeint |
236 |
228 |
237 from scipy.optimize import fsolve |
229 from scipy.optimize import fsolve |
238 \end{lstlisting} |
230 \end{lstlisting} |
239 \emphbar{All the above statements import one function into your namespace} |
231 \emphbar{Above statements import a function to our namespace} |
240 \end{frame} |
232 \end{frame} |
241 |
233 |
242 \begin{frame}[fragile] |
234 \begin{frame}[fragile] |
243 \frametitle{Running scripts from command line} |
235 \frametitle{Running scripts from command line} |
244 \small |
236 \small |
245 \begin{itemize} |
237 \begin{itemize} |
246 \item Start cmd |
238 \item Fire up a terminal |
247 \item cd to Desktop |
239 \item python four\_plot.py |
248 \item python sine\_plot.py |
|
249 \end{itemize} |
240 \end{itemize} |
250 \pause |
241 \pause |
251 \begin{lstlisting} |
242 \begin{lstlisting} |
252 Traceback (most recent call last): |
243 Traceback (most recent call last): |
253 File "sine_plot.py", line 1, in <module> |
244 File "four_plot.py", line 1, in <module> |
254 x = linspace(-5*pi, 5*pi, 500) |
245 x = linspace(-5*pi, 5*pi, 500) |
255 NameError: name 'linspace' is not defined |
246 NameError: name 'linspace' is not defined |
256 \end{lstlisting} |
247 \end{lstlisting} |
257 \end{frame} |
248 \end{frame} |
258 |
249 |
259 \begin{frame}[fragile] |
250 \begin{frame}[fragile] |
260 \frametitle{Remedy} |
251 \frametitle{Remedy} |
261 \emphbar{Adding what lines to sine\_plot.py makes this program work?} |
|
262 \begin{lstlisting} |
252 \begin{lstlisting} |
263 from scipy import * |
253 from scipy import * |
264 \end{lstlisting} |
254 \end{lstlisting} |
265 \alert{Now run python sine\_plot.py again!} |
255 \alert{Now run python four\_plot.py again!} |
266 \pause |
256 \pause |
267 \begin{lstlisting} |
257 \begin{lstlisting} |
268 Traceback (most recent call last): |
258 Traceback (most recent call last): |
269 File "sine_plot.py", line 4, in <module> |
259 File "four_plot.py", line 4, in <module> |
270 plot(x, x, 'b') |
260 plot(x, x, 'b') |
271 NameError: name 'plot' is not defined |
261 NameError: name 'plot' is not defined |
272 \end{lstlisting} |
262 \end{lstlisting} |
273 \end{frame} |
263 \end{frame} |
274 |
264 |
275 \begin{frame}[fragile] |
265 \begin{frame}[fragile] |
276 \frametitle{Remedy \ldots} |
266 \frametitle{Remedy \ldots} |
277 \emphbar{What should we add now?} |
|
278 \begin{lstlisting} |
267 \begin{lstlisting} |
279 from pylab import * |
268 from pylab import * |
280 \end{lstlisting} |
269 \end{lstlisting} |
281 \alert{Now run python sine\_plot.py again!!} |
270 \alert{Now run python four\_plot.py again!!} |
282 \end{frame} |
271 \end{frame} |
283 |
272 |
284 \begin{frame}[fragile] |
273 \begin{frame}[fragile] |
285 \frametitle{Modules} |
274 \frametitle{Modules} |
286 \begin{itemize} |
275 \begin{itemize} |
296 \end{frame} |
285 \end{frame} |
297 |
286 |
298 \begin{frame}[fragile] |
287 \begin{frame}[fragile] |
299 \frametitle{Package hierarchies} |
288 \frametitle{Package hierarchies} |
300 \begin{lstlisting} |
289 \begin{lstlisting} |
301 from scipy.interpolate import splev |
290 from scipy.integrate import odeint |
302 |
|
303 from scipy.integrate import quad |
|
304 |
291 |
305 from scipy.optimize import fsolve |
292 from scipy.optimize import fsolve |
306 \end{lstlisting} |
293 \end{lstlisting} |
307 \end{frame} |
294 \end{frame} |
308 |
295 |
309 \begin{frame}[fragile] |
296 \begin{frame}[fragile] |
310 \frametitle{from \ldots import in a conventional way!} |
297 \frametitle{\texttt{from} \ldots \texttt{import} - conventional way!} |
311 \small |
298 \small |
312 \begin{lstlisting} |
299 \begin{lstlisting} |
313 from scipy import linspace, pi, sin |
300 from scipy import linspace, pi, sin |
314 from pylab import plot, legend, annotate |
301 from pylab import plot, legend, annotate |
315 from pylab import xlim, ylim |
302 from pylab import xlim, ylim |
366 \end{frame} |
353 \end{frame} |
367 |
354 |
368 \begin{frame}[fragile] |
355 \begin{frame}[fragile] |
369 \frametitle{Modules of special interest} |
356 \frametitle{Modules of special interest} |
370 \begin{description}[matplotlibfor2d] |
357 \begin{description}[matplotlibfor2d] |
371 \item[\typ{pylab}] Easy, interactive, 2D plotting |
358 \item[\texttt{pylab}] Easy, interactive, 2D plotting |
372 |
359 |
373 \item[\typ{scipy}] arrays, statistics, optimization, integration, linear |
360 \item[\texttt{scipy}] arrays, statistics, optimization, integration, linear |
374 algebra, Fourier transforms, signal and image processing, |
361 algebra, Fourier transforms, signal and image processing, |
375 genetic algorithms, ODE solvers, special functions, and more |
362 genetic algorithms, ODE solvers, special functions, and more |
376 |
363 |
377 \item[\typ{Mayavi}] Easy, interactive, 3D plotting |
364 \item[\texttt{Mayavi}] Easy, interactive, 3D plotting |
378 \end{description} |
365 \end{description} |
379 \end{frame} |
366 \end{frame} |
380 |
367 |
381 \section{Objects} |
368 \section{Objects} |
382 \begin{frame}{Everything is an Object!} |
369 \begin{frame}{Everything is an Object!} |