day1/Session-1.tex
changeset 5 838cedef5e9d
child 24 95fdc10975d5
equal deleted inserted replaced
4:e5047bcbb608 5:838cedef5e9d
       
     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,
       
    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[Asokan \& Prabhu] {Asokan Pichai\\Prabhu Ramachandran}
       
    79 
       
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
       
    81 \date[] {10, August 2009}
       
    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 
       
    99 % If you wish to uncover everything in a step-wise fashion, uncomment
       
   100 % the following command: 
       
   101 %\beamerdefaultoverlayspecification{<+->}
       
   102 
       
   103 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6}
       
   104 
       
   105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   106 % DOCUMENT STARTS
       
   107 \begin{document}
       
   108 
       
   109 \begin{frame}
       
   110   \titlepage
       
   111 \end{frame}
       
   112 \begin{frame}
       
   113   {Acknowledgements}
       
   114   \begin{center}
       
   115   This program is conducted by\\
       
   116   IIT, Bombay\\
       
   117   through CDEEP\\as part of  the open source initiatives\\
       
   118   under the aegis of\\
       
   119   \alert{National Mission on Education through ICT,} \\
       
   120   Ministry of HRD.
       
   121   \end{center}
       
   122 \end{frame}
       
   123 
       
   124 \begin{frame}
       
   125   \frametitle{Outline}
       
   126   \tableofcontents
       
   127   % You might wish to add the option [pausesections]
       
   128 \end{frame}
       
   129 
       
   130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   131 % TODO
       
   132 %
       
   133 %  * Add slide on Python packages (modules)
       
   134 %  * Add slides on reference counting.
       
   135 
       
   136 \section{Agenda}
       
   137 \begin{frame}{About the Workshop}
       
   138   \begin{description}
       
   139 	\item[Day 1, Session 1] Sat 09:30--11:00
       
   140 	\item[Day 1, Session 2] Sat 11:15--12:45
       
   141 	\item[Day 1, Session 3] Sat 13:45--15:15
       
   142 	\item[Day 1, Session 4] Sat 15:30--17:00
       
   143         \item[Day 2, Session 1] Sun 09:30--11:00
       
   144 	\item[Day 2, Session 2] Sun 11:15--12:45
       
   145 	\item[Day 2, Session 3] Sun 13:45--15:15
       
   146 	\item[Day 2, Session 4] Sun 15:30--17:00
       
   147   \end{description}
       
   148 \end{frame}
       
   149 
       
   150 \section{Agenda}
       
   151 \begin{frame}{About the Workshop}
       
   152   \begin{block}{Goal of the workshop}
       
   153 	At the end of this program, successful participants will be able to use python as their scripting and problem solving language. Aimed at Engg. students--focus on basic numerics and plotting-- but should serve a similar purpose for others. 
       
   154   \end{block}
       
   155 \end{frame}
       
   156 
       
   157 \begin{frame}{Checklist}
       
   158   Let us verify that all of us are having the same (similar) tools and environment
       
   159   \begin{description}
       
   160 	\item[python] Type python at the command line. Do you see version 2.5 or later?
       
   161 	\item[IPython] Is IPython available?
       
   162 	\item[Editor] Which editor? scite, vim, emacs, \ldots
       
   163   \end{description}
       
   164 \end{frame}
       
   165 
       
   166 \section{Overview}
       
   167 \begin{frame}{Session 1}
       
   168   \begin{itemize}
       
   169 	\item Introduction and motivation
       
   170 	\item Using the interpreter(s)
       
   171 	\item Basic data types: int, float, string
       
   172 	\item Basic data structures: list
       
   173 	\item Basic console IO: \texttt{raw\_input(), print}
       
   174     \item Basic control flow: \texttt{if, while}
       
   175 	\item Problem set 1
       
   176     \item Functions $\rightarrow$ Problem set 2
       
   177     \item lists, \texttt{for}  $\rightarrow$ Problem set 3
       
   178     \item IO, Modules $\rightarrow$ Problem sets 4,5, \ldots
       
   179   \end{itemize}
       
   180 \end{frame}
       
   181 
       
   182 \begin{frame}
       
   183   \frametitle{Introduction}
       
   184   \begin{itemize}
       
   185   \item Creator and BDFL: Guido van Rossum
       
   186   \item Conceived in December 1989
       
   187   \item ``Python'' as in Monty Python's Flying Circus
       
   188   \item Current stable version of Python is 2.6.x
       
   189   \item PSF license (like BSD: no strings attached)
       
   190   \item Highly cross platform
       
   191   \item Runs on the Nokia series 60!
       
   192   \item \alert{Philosophy:} Simple and complete by design
       
   193   \end{itemize}
       
   194 \end{frame}
       
   195 
       
   196 \begin{frame}
       
   197   \frametitle{Resources}
       
   198   \begin{itemize}
       
   199   \item Part of many GNU/Linux distributions
       
   200   \item Web: \url{http://www.python.org}
       
   201   \item Doc: \url{http://www.python.org/doc}
       
   202   \item Free Tutorials:
       
   203     \begin{itemize}
       
   204     \item Official Python tutorial: \url{http://docs.python.org/tut/tut.html}
       
   205     \item Byte of Python: \url{http://www.byteofpython.info/}
       
   206     \item Dive into Python: \url{http://diveintopython.org/}
       
   207     \end{itemize}
       
   208   \end{itemize}
       
   209 \end{frame}
       
   210 
       
   211 \begin{frame}
       
   212   \frametitle{Why Python?}
       
   213   \begin{itemize}
       
   214   \item Designed to be readable and easy to use
       
   215   \item High level, interpreted, modular, OO
       
   216   \item Much faster development cycle
       
   217   \item Powerful interactive environment
       
   218   \item Rapid application development
       
   219   \item Rich standard library and modules
       
   220   \item Interfaces well with C++, C and FORTRAN
       
   221   \item \alert{More than a math package $\Rightarrow$ some extra work compared to math packages}
       
   222   \end{itemize}
       
   223 \end{frame}
       
   224 
       
   225 \begin{frame}
       
   226   \frametitle{Use cases}
       
   227   \begin{itemize}
       
   228   \item NASA: Space Shuttle Mission Design
       
   229   \item AstraZeneca: Collaborative Drug Discovery
       
   230   \item ForecastWatch.com: Helps Meteorologists
       
   231   \item Industrial Light \& Magic: Runs on Python
       
   232   \item Zope: Commercial grade Toolkit
       
   233   \item Plone: Professional high feature CMS
       
   234   \item RedHat: install scripts, sys-admin tools
       
   235   \item Django: A great web application framework
       
   236   \item Google: A strong python shop
       
   237   \end{itemize}
       
   238 \end{frame}
       
   239 
       
   240 \begin{frame}
       
   241   \frametitle{To sum up, python is\ldots}
       
   242   \begin{itemize}
       
   243   \item dynamically typed, interpreted $\rightarrow$ rapid testing/prototyping
       
   244   \item powerful, very high level
       
   245   \item has full introspection 
       
   246   \item Did we mention powerful?
       
   247   \end{itemize}
       
   248   \begin{block}{But \ldots}
       
   249     may be wanting in performance. specialised resources such as SWIG, \alert{Cython} are available 
       
   250   \end{block}
       
   251   \inctime{15}
       
   252 \end{frame}
       
   253 
       
   254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   255 % TIME: 15 m, running 15m
       
   256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   257 
       
   258 \section{Python}
       
   259 
       
   260 \subsection{Getting Started}
       
   261 
       
   262 \begin{frame}[fragile]{At the prompt, type the following}
       
   263    \begin{lstlisting}
       
   264 >>> print 'Hello Python' 
       
   265 >>> print 3124 * 126789
       
   266 >>> 1786 % 12
       
   267 >>> 3124 * 126789
       
   268 >>> a = 3124 * 126789
       
   269 >>> big = 12345678901234567890 ** 3
       
   270 >>> verybig = big * big * big * big 
       
   271 >>> 12345**6, 12345**67, 12345**678
       
   272   \end{lstlisting}
       
   273 \end{frame}
       
   274 
       
   275 \begin{frame}[fragile]{At the prompt, type the following}
       
   276    \begin{lstlisting}
       
   277 >>> s = 'Hello '
       
   278 >>> p = 'World'
       
   279 >>> s + p 
       
   280 >>> s * 12 
       
   281 >>> s * s
       
   282 >>> s + p * 12, (s + p)* 12
       
   283 >>> s * 12 + p * 12
       
   284 >>> 12 * s 
       
   285     \end{lstlisting}
       
   286 \end{frame}
       
   287 
       
   288 \begin{frame}[fragile]{At the prompt, type the following}
       
   289   \begin{lstlisting}
       
   290 >>> 17/2
       
   291 >>> 17/2.0
       
   292 >>> 17.0/2
       
   293 >>> 17.0/8.5
       
   294 >>> int(17/2.0)
       
   295 >>> float(17/2)
       
   296 >>> str(17/2.0)
       
   297 >>> round( 7.5 )
       
   298   \end{lstlisting}
       
   299   \begin{block}{Mini exercise}
       
   300 	Round a float to the nearest integer, using \texttt{int()}?
       
   301   \end{block}
       
   302 \end{frame}
       
   303 
       
   304 \begin{frame}{Midi exercises}
       
   305   \begin{center}
       
   306     \begin{itemize}
       
   307       \item What does this do?
       
   308       \item \texttt{round(amount * 10) /10.0 }
       
   309     \end{itemize}
       
   310   \end{center}
       
   311 \end{frame}
       
   312 
       
   313 \begin{frame}{More exercises?}
       
   314   \begin{center}
       
   315     \begin{block}{Round sums}
       
   316       How to round a number to the nearest  5 paise?\\
       
   317       \begin{description}
       
   318         \item[Remember] 17.23 $\rightarrow$ 17.25,\\ while 17.22 $\rightarrow$ 17.20\\
       
   319       \end{description}
       
   320       How to round a number to the nearest 20 paise?
       
   321     \end{block}
       
   322   \end{center}
       
   323 \end{frame}
       
   324 
       
   325 \begin{frame}[fragile] {A question of good style}
       
   326   \begin{lstlisting}
       
   327     amount = 12.68
       
   328     denom = 0.05
       
   329     nCoins = round(amount/denom)
       
   330     rAmount = nCoins * denom
       
   331   \end{lstlisting}
       
   332   \pause
       
   333   \begin{block}{Style Rule \#1}
       
   334     Naming is 80\% of programming
       
   335   \end{block}
       
   336 \end{frame}
       
   337 
       
   338 
       
   339 \begin{frame}[fragile]
       
   340   \frametitle{Odds and ends}
       
   341   \begin{itemize}
       
   342     \item Case sensitive
       
   343     \item Dynamically typed $\Rightarrow$ need not specify a type
       
   344       \begin{lstlisting}
       
   345 a = 1
       
   346 a = 1.1
       
   347 a = "Now I am a string!"
       
   348       \end{lstlisting}
       
   349     \item Comments:
       
   350       \begin{lstlisting}
       
   351 a = 1  # In-line comments
       
   352 # Comment in a line to itself.
       
   353 a = "# This is not a comment!"
       
   354       \end{lstlisting}
       
   355   \end{itemize}
       
   356   \inctime{15}
       
   357 \end{frame}
       
   358 
       
   359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   360 % TIME: 15 m, running 30m
       
   361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   362 
       
   363 \subsection{Data types}
       
   364 \begin{frame}
       
   365   \frametitle{Basic types}
       
   366   \begin{itemize}
       
   367     \item numbers: float, int, long, complex
       
   368     \item strings
       
   369     \item boolean
       
   370   \end{itemize}
       
   371   \begin{block}{Also to be discussed later}
       
   372     tuples, lists, dictionaries, functions, objects\ldots
       
   373   \end{block}
       
   374 \end{frame}
       
   375 
       
   376 \begin{frame}[fragile]
       
   377   \frametitle{Numbers}
       
   378   \vspace*{-0.25in}
       
   379   \begin{lstlisting}
       
   380 >>> a = 1 # Int.
       
   381 >>> l = 1000000L # Long
       
   382 >>> e = 1.01325e5 # float
       
   383 >>> f = 3.14159 # float
       
   384 >>> c = 1+1j # Complex!
       
   385 >>> print f*c/a
       
   386 (3.14159+3.14159j)
       
   387 >>> print c.real, c.imag
       
   388 1.0 1.0
       
   389 >>> abs(c)
       
   390 1.4142135623730951
       
   391 >>> abs( 8 - 9.5 )
       
   392 1.5
       
   393   \end{lstlisting}
       
   394 \end{frame}
       
   395 
       
   396 \begin{frame}[fragile]
       
   397   \frametitle{Boolean}
       
   398   \begin{lstlisting}
       
   399 >>> t = True
       
   400 >>> f = not t
       
   401 False
       
   402 >>> f or t
       
   403 True
       
   404 >>> f and t
       
   405 False
       
   406   \end{lstlisting}
       
   407   \begin{block}{Try:}
       
   408   NOT True\\
       
   409   not TRUE
       
   410   \end{block}
       
   411 \end{frame}
       
   412 
       
   413 \begin{frame}[fragile]
       
   414   \frametitle{Relational and logical operators}
       
   415   \begin{lstlisting}
       
   416 >>> a, b, c = -1, 0, 1
       
   417 >>> a == b
       
   418 False
       
   419 >>> a <= b 
       
   420 True
       
   421 >>> a + b != c
       
   422 True
       
   423 >>> a < b < c
       
   424 True
       
   425 >>> c >= a + b
       
   426 True
       
   427   \end{lstlisting}
       
   428 \end{frame}
       
   429 
       
   430 \begin{frame}[fragile]
       
   431   \frametitle{Strings}
       
   432   \begin{lstlisting}
       
   433 s = 'this is a string'
       
   434 s = 'This one has "quotes" inside!'
       
   435 s = "I have 'single-quotes' inside!"
       
   436 l = "A string spanning many lines\
       
   437 one more line\
       
   438 yet another"
       
   439 t = """A triple quoted string does
       
   440 not need to be escaped at the end and 
       
   441 "can have nested quotes" etc."""
       
   442   \end{lstlisting}
       
   443 \end{frame}
       
   444 
       
   445 \begin{frame}[fragile]
       
   446   \frametitle{More Strings}
       
   447   \vspace*{-0.2in}
       
   448   \begin{lstlisting}
       
   449 >>> w = "hello"    
       
   450 >>> print w[0] + w[2] + w[-1]
       
   451 hlo
       
   452 >>> len(w) # guess what
       
   453 5
       
   454 >>> s = u'Unicode strings!'
       
   455 >>> # Raw strings (note the leading 'r')
       
   456 ... r_s = r'A string $\alpha \nu$'
       
   457   \end{lstlisting}
       
   458 \pause
       
   459   \begin{lstlisting}
       
   460 >>> w[0] = 'H' # Can't do that!
       
   461 Traceback (most recent call last):
       
   462   File "<stdin>", line 1, in ?
       
   463 TypeError: object does not support item assignment
       
   464   \end{lstlisting}
       
   465 \end{frame}
       
   466 
       
   467 \begin{frame}
       
   468   \frametitle{Let us switch to IPython}
       
   469   Why?
       
   470   \begin{block}
       
   471     {Better help (and a lot more)}
       
   472     Tab completion\\
       
   473     ?\\
       
   474     .?\\
       
   475     object.function?
       
   476   \end{block}
       
   477 \end{frame}
       
   478 
       
   479 \begin{frame}[fragile]
       
   480   \frametitle{More on strings}
       
   481   \begin{lstlisting}
       
   482 In [1]: a = 'hello world'
       
   483 In [2]: a.startswith('hell')
       
   484 Out[2]: True
       
   485 In [3]: a.endswith('ld')
       
   486 Out[3]: True
       
   487 In [4]: a.upper()
       
   488 Out[4]: 'HELLO WORLD'
       
   489 In [5]: a.upper().lower()
       
   490 Out[5]: 'hello world'
       
   491   \end{lstlisting}
       
   492 \end{frame}
       
   493 
       
   494 \begin{frame}[fragile]{Still with strings}
       
   495   \begin{lstlisting}
       
   496 In [6]: a.split()
       
   497 Out[6]: ['hello', 'world']
       
   498 In [7]: ''.join(['a', 'b', 'c'])
       
   499 Out[7]: 'abc'
       
   500 In [8] 'd' in ''.join( 'a', 'b', 'c')
       
   501 Out[8]: False
       
   502   \end{lstlisting}
       
   503   \begin{block}{Try:}
       
   504     \texttt{a.split( 'o' )}\\
       
   505     \texttt{'x'.join( a.split( 'o' ) )}
       
   506   \end{block}
       
   507 \end{frame}
       
   508 
       
   509 \begin{frame}[fragile]{Surprise! strings!!}
       
   510   \begin{lstlisting}
       
   511 In [11]: x, y = 1, 1.2
       
   512 In [12]: 'x is %s, y is %s' %(x, y)
       
   513 Out[12]: 'x is 1, y is 1.234'
       
   514   \end{lstlisting}
       
   515   \begin{block}{Try:}
       
   516     \texttt{'x is \%d, y is \%f' \%(x, y) }\\
       
   517     \texttt{'x is \%3d, y is \%4.2f' \%(x, y) }
       
   518   \end{block}
       
   519   \small
       
   520 \url{docs.python.org/lib/typesseq-strings.html}\\
       
   521 \end{frame}
       
   522 
       
   523 \begin{frame}
       
   524   {Interlude}
       
   525   \begin{block}
       
   526     {A classic problem}
       
   527     How to interchange values of two variables? Please note that the type of either variable is unknown and it is not necessary that both be of the same type even!
       
   528   \end{block}
       
   529   \inctime{30}
       
   530 \end{frame}
       
   531 
       
   532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   533 % TIME: 25 m+ Interlude break 5 mins, running 60m 
       
   534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   535 
       
   536 \subsection{Control flow}
       
   537 \begin{frame}
       
   538   \frametitle{Control flow constructs}  
       
   539   \begin{itemize}
       
   540   \item \kwrd{if/elif/else}: branching
       
   541   \item \kwrd{while}: looping
       
   542   \item \kwrd{for}: iterating 
       
   543   \item \kwrd{break, continue}: modify loop 
       
   544   \item \kwrd{pass}: syntactic filler
       
   545   \end{itemize}
       
   546 \end{frame}
       
   547 
       
   548 \begin{frame}[fragile]
       
   549   \frametitle{Basic conditional flow}
       
   550   \begin{lstlisting}
       
   551 In [21]: a = 7
       
   552 In [22]: b = 8
       
   553 In [23]: if a > b:
       
   554    ....:    print 'Hello'
       
   555    ....: else:
       
   556    ....:     print 'World'
       
   557    ....:
       
   558    ....:
       
   559 World
       
   560   \end{lstlisting}
       
   561   Let us switch to creating a file
       
   562 \end{frame}
       
   563 
       
   564 \begin{frame}
       
   565   {Creating python files}
       
   566   \begin{itemize}
       
   567     \item aka scripts
       
   568     \item use your editor
       
   569     \item Note that white space is the way to specify blocks!
       
   570     \item extension \typ{.py}
       
   571     \item run with \texttt{python hello.py} at the command line
       
   572     \item in IPython\ldots
       
   573   \end{itemize}
       
   574 \end{frame}
       
   575 
       
   576 \begin{frame}[fragile]
       
   577   \frametitle{\typ{If...elif...else} example}
       
   578 \begin{lstlisting}
       
   579 x = int(raw_input("Enter an integer:"))
       
   580 if x < 0:
       
   581      print 'Be positive!'
       
   582 elif x == 0:
       
   583      print 'Zero'
       
   584 elif x == 1:
       
   585      print 'Single'
       
   586 else:
       
   587      print 'More'
       
   588 \end{lstlisting}
       
   589 \end{frame}
       
   590 
       
   591 \begin{frame}{Simple IO}
       
   592   \begin{block}
       
   593     {Console Input}
       
   594     \texttt{raw\_input(}) waits for user input.\\Prompt string is optional.\\
       
   595     All keystrokes are Strings!\\\texttt{int()} converts string to int.
       
   596   \end{block}
       
   597   \begin{block}
       
   598     {Console output}
       
   599     \texttt{print} is straight forward. Major point to remember is the distinction between \texttt{print x} and \texttt{print x,}
       
   600   \end{block}
       
   601 \end{frame}
       
   602 
       
   603 \begin{frame}[fragile]
       
   604   \frametitle{Basic looping}
       
   605   \begin{lstlisting}
       
   606 # Fibonacci series:
       
   607 # the sum of two elements
       
   608 # defines the next
       
   609 a, b = 0, 1
       
   610 while b < 10:
       
   611     print b,
       
   612     a, b = b, a + b
       
   613  
       
   614 \end{lstlisting}
       
   615 \typ{1 1 2 3 5 8}\\  
       
   616 \alert{Recall it is easy to write infinite loops with \kwrd{while}}
       
   617   \inctime{20}
       
   618 \end{frame}
       
   619 
       
   620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   621 % TIME: 20 m, running 80m 
       
   622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   623 
       
   624 \begin{frame}
       
   625   \frametitle{Problem set 1}
       
   626   \begin{itemize}
       
   627     \item All the problems can be\\
       
   628       solved using \kwrd{if} and \kwrd{while} 
       
   629   \end{itemize}
       
   630 \end{frame}
       
   631 
       
   632 \begin{frame}{Problem 1.1}
       
   633   Write a program that displays all three digit numbers that are equal to the sum of the cubes of their digits. That is, print numbers $abc$ that have the property $abc = a^3 + b^3 + c^3$\\
       
   634 These are called $Armstrong$ numbers.
       
   635 \end{frame}
       
   636   
       
   637 \begin{frame}{Problem 1.2 - Collatz sequence}
       
   638 \begin{enumerate}
       
   639   \item Start with an arbitrary (positive) integer. 
       
   640   \item If the number is even, divide by 2; if the number is odd multiply by 3 and add 1.
       
   641   \item Repeat the procedure with the new number.
       
   642   \item It appears that for all starting values there is a cycle of 4, 2, 1 at which the procedure loops.
       
   643 \end{enumerate}
       
   644     Write a program that accepts the starting value and prints out the Collatz sequence.
       
   645 
       
   646 \end{frame}
       
   647 
       
   648 \begin{frame}{Problem 1.3 - Kaprekar's constant}
       
   649   \begin{enumerate}
       
   650     \item Take a four digit number--with at least two digits different.
       
   651     \item Arrange the digits in ascending and descending order, giving A and D respectively.
       
   652     \item Leave leading zeros in A!
       
   653     \item Subtract A from D.
       
   654     \item With the result, repeat from step 2.
       
   655   \end{enumerate}
       
   656   Write a program to accept a 4-digit number and display the progression to Kaprekar's constant.
       
   657 \end{frame}
       
   658 
       
   659 \begin{frame}[fragile]{Problem 1.4}
       
   660   Write a program that prints the following pyramid on the screen. 
       
   661   \begin{lstlisting}
       
   662 1
       
   663 2  2
       
   664 3  3  3
       
   665 4  4  4  4
       
   666   \end{lstlisting}
       
   667 The number of lines must be obtained from the user as input.\\
       
   668 \pause
       
   669 When can your code fail?
       
   670 \only<2->{\inctime{25}}
       
   671 \end{frame}
       
   672 
       
   673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   674 % TIME: 25 m, running 105m 
       
   675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   676 
       
   677 \subsection{Functions}
       
   678 \begin{frame}[fragile]
       
   679 \frametitle{Functions: examples}
       
   680   \begin{lstlisting}
       
   681 def signum( r ):
       
   682     """returns 0 if r is zero
       
   683     -1 if r is negative
       
   684     +1 if r is positive"""
       
   685     if r < 0:
       
   686         return -1
       
   687     elif r > 0:
       
   688         return 1
       
   689     else:
       
   690         return 0
       
   691   \end{lstlisting}
       
   692 \end{frame}
       
   693 
       
   694 \begin{frame}[fragile]
       
   695   \frametitle{Functions: examples}
       
   696   \begin{lstlisting}
       
   697 def pad( n, size ): 
       
   698     """pads integer n with spaces
       
   699     into a string of length size
       
   700     """
       
   701     SPACE = ' '
       
   702     s = str( n )
       
   703     padSize = size - len( s )
       
   704     return padSize * SPACE + s
       
   705   \end{lstlisting}
       
   706 \pause
       
   707 What about \%3d?
       
   708 \end{frame}
       
   709 
       
   710 \begin{frame}[fragile]
       
   711   {What does this function do?}
       
   712   \begin{lstlisting}
       
   713 def what( n ):
       
   714     if n < 0: n = -n
       
   715     while n > 0:
       
   716         if n % 2 == 1:
       
   717             return False
       
   718         n /= 10
       
   719     return True
       
   720   \end{lstlisting}
       
   721 \end{frame}
       
   722 
       
   723 \begin{frame}[fragile]
       
   724   {What does this function do?}
       
   725 \begin{lstlisting}
       
   726 def what( n ):
       
   727     i = 1    
       
   728     while i * i < n:
       
   729         i += 1
       
   730     return i * i == n, i
       
   731   \end{lstlisting}
       
   732 \end{frame}
       
   733 
       
   734 \begin{frame}[fragile]
       
   735   {What does this function do?}
       
   736   \begin{lstlisting}
       
   737 def what( n, x ):
       
   738     z = 1.0
       
   739     if n < 0:
       
   740         x = 1.0 / x
       
   741         n = -n
       
   742     while n > 0:
       
   743         if n % 2 == 1:
       
   744             z *= x
       
   745         n /= 2
       
   746         x *= x
       
   747     return z
       
   748   \end{lstlisting}
       
   749 \end{frame}
       
   750 
       
   751 \begin{frame}
       
   752   {Before writing a function}
       
   753   \begin{itemize}
       
   754       \item Builtin functions for various and sundry
       
   755       \item \typ{abs, any, all, len, max, min}
       
   756       \item \typ{pow, range, sum, type}
       
   757       \item Refer here:
       
   758           \url{http://docs.python.org/library/functions.html}
       
   759   \end{itemize}
       
   760   \inctime{15} 
       
   761 \end{frame}
       
   762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   763 % TIME: 15 m, running 120m 
       
   764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   765 
       
   766 \begin{frame}{Problem set 2}
       
   767   The focus is on writing functions and calling them.
       
   768 \end{frame}
       
   769 
       
   770 \begin{frame}{Problem 2.1}
       
   771   Write a function to return the gcd of two numbers.
       
   772 \end{frame}
       
   773 
       
   774 \begin{frame}{Problem 2.2}
       
   775 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 should not be displayed by our program. \\
       
   776 Write a program to print primitive pythagorean triads. The program should generate all triads with a, b values in the range 0---100
       
   777 \end{frame}
       
   778 
       
   779 \begin{frame}{Problem 2.3}
       
   780   Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\\For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).
       
   781 \end{frame}
       
   782 
       
   783 \begin{frame}{Problem 2.4}
       
   784   The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, the aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\
       
   785   Write a function that returns the aliquot number of a given number. 
       
   786 \end{frame}
       
   787 
       
   788 \begin{frame}{Problem 2.5}
       
   789   A pair of numbers (a, b) is said to be \alert{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\
       
   790   Example: \texttt{220, 284}\\
       
   791   Write a program that prints all five digit amicable pairs.
       
   792   \inctime{30}
       
   793 \end{frame}
       
   794 
       
   795 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   796 % TIME: 30 m, running 150m 
       
   797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   798 
       
   799 \subsection{Lists}
       
   800 
       
   801 \begin{frame}[fragile]
       
   802   \frametitle{List creation and indexing}
       
   803 \begin{lstlisting}
       
   804 >>> a = [] # An empty list.
       
   805 >>> a = [1, 2, 3, 4] # More useful.
       
   806 >>> len(a) 
       
   807 4
       
   808 >>> a[0] + a[1] + a[2] + a[-1]
       
   809 10
       
   810 \end{lstlisting}
       
   811   \begin{itemize}
       
   812   \item Indices start with ?
       
   813   \item Negative indices indicate ?
       
   814   \end{itemize}
       
   815 \end{frame}
       
   816 
       
   817 \begin{frame}[fragile]
       
   818   \frametitle{List: slices}
       
   819   \begin{itemize}
       
   820   \item Slicing is a basic operation
       
   821   \item \typ{list[initial:final:step]}
       
   822   \item  The step is optional
       
   823   \end{itemize}
       
   824 \begin{lstlisting}
       
   825 >>> a[1:3] # A slice.
       
   826 [2, 3]
       
   827 >>> a[1:-1]
       
   828 [2, 3, 4]
       
   829 >>> a[1:] == a[1:-1]
       
   830 False  
       
   831 \end{lstlisting}
       
   832 Explain last result
       
   833 \end{frame}
       
   834 
       
   835 \begin{frame}[fragile]
       
   836   \frametitle{List: more slices}
       
   837 \begin{lstlisting}
       
   838 >>> a[0:-1:2] # Notice the step!
       
   839 [1, 3]
       
   840 >>> a[::2]
       
   841 [1, 3]
       
   842 >>> a[-1::-1]
       
   843 \end{lstlisting}
       
   844 What do you think the last one will do?
       
   845   \emphbar{Note: Strings also use same indexing and slicing.}
       
   846 \end{frame}
       
   847 
       
   848 \begin{frame}[fragile]
       
   849   \frametitle{List: examples}
       
   850 \begin{lstlisting}
       
   851 >>> a = [1, 2, 3, 4]
       
   852 >>> a[:2]
       
   853 [1, 3]
       
   854 >>> a[0:-1:2]
       
   855 [1, 3]
       
   856 \end{lstlisting}
       
   857 \pause
       
   858 \alert{Lists are mutable (unlike strings)}
       
   859 \begin{lstlisting}
       
   860 >>> a[1] = 20
       
   861 >>> a
       
   862 [1, 20, 3, 4]
       
   863 \end{lstlisting}
       
   864 \end{frame}
       
   865 
       
   866 \begin{frame}[fragile]
       
   867   \frametitle{Lists are mutable and heterogenous}
       
   868 \begin{lstlisting}
       
   869 >>> a = ['spam', 'eggs', 100, 1234]
       
   870 >>> a[2] = a[2] + 23
       
   871 >>> a
       
   872 ['spam', 'eggs', 123, 1234]
       
   873 >>> a[0:2] = [1, 12] # Replace items
       
   874 >>> a
       
   875 [1, 12, 123, 1234]
       
   876 >>> a[0:2] = [] # Remove items
       
   877 >>> a.append( 12345 )
       
   878 >>> a
       
   879 [123, 1234, 12345]
       
   880 \end{lstlisting}
       
   881 \end{frame}
       
   882 
       
   883 \begin{frame}[fragile]
       
   884   \frametitle{List methods}
       
   885 \begin{lstlisting}
       
   886 >>> a = ['spam', 'eggs', 1, 12]
       
   887 >>> a.reverse() # in situ
       
   888 >>> a
       
   889 [12, 1, 'eggs', 'spam']
       
   890 >>> a.append(['x', 1]) 
       
   891 >>> a
       
   892 [12, 1, 'eggs', 'spam', ['x', 1]]
       
   893 >>> a.extend([1,2]) # Extend the list.
       
   894 >>> a.remove( 'spam' )
       
   895 >>> a
       
   896 [12, 1, 'eggs', ['x', 1], 1, 2]
       
   897 \end{lstlisting}
       
   898 \end{frame}
       
   899 
       
   900 \begin{frame}[fragile]
       
   901   \frametitle{List containership}
       
   902   \begin{lstlisting}
       
   903 >>> a = ['cat', 'dog', 'rat', 'croc']
       
   904 >>> 'dog' in a
       
   905 True
       
   906 >>> 'snake' in a
       
   907 False
       
   908 >>> 'snake' not in a
       
   909 True
       
   910 >>> 'ell' in 'hello world'
       
   911 True
       
   912   \end{lstlisting}
       
   913 \end{frame}
       
   914 
       
   915 \begin{frame}[fragile]
       
   916   \frametitle{Tuples: immutable}
       
   917 \begin{lstlisting}
       
   918 >>> t = (0, 1, 2)
       
   919 >>> print t[0], t[1], t[2], t[-1] 
       
   920 0 1 2 2
       
   921 >>> t[0] = 1
       
   922 Traceback (most recent call last):
       
   923   File "<stdin>", line 1, in ?
       
   924 TypeError: object does not support item
       
   925            assignment
       
   926 \end{lstlisting}  
       
   927 \begin{itemize}
       
   928     \item Multiple return values are actually a tuple.
       
   929     \item Exchange is tuple (un)packing
       
   930 \end{itemize}
       
   931 
       
   932 \end{frame}
       
   933 
       
   934 \begin{frame}[fragile]
       
   935   \frametitle{\typ{range()} function}
       
   936   \begin{lstlisting}
       
   937 >>> range(7)
       
   938 [0, 1, 2, 3, 4, 5, 6]
       
   939 >>> range( 3, 9)
       
   940 [3, 4, 5, 6, 7, 8]
       
   941 >>> range( 4, 17, 3)
       
   942 [4, 7, 10, 13, 16]
       
   943 >>> range( 5, 1, -1)
       
   944 [5, 4, 3, 2]
       
   945 >>> range( 8, 12, -1)
       
   946 []
       
   947   \end{lstlisting}
       
   948 \end{frame}
       
   949 
       
   950 \begin{frame}[fragile]
       
   951   \frametitle{\typ{for\ldots range(\ldots)} idiom}
       
   952   \begin{lstlisting}
       
   953 In [83]: for i in range(5):
       
   954    ....:     print i, i * i
       
   955    ....:     
       
   956    ....:     
       
   957 0 0
       
   958 1 1
       
   959 2 4
       
   960 3 9
       
   961 4 16
       
   962 \end{lstlisting}
       
   963 \end{frame}
       
   964 
       
   965 \begin{frame}[fragile]
       
   966   \frametitle{\typ{for}: the list companion}
       
   967   
       
   968   \begin{lstlisting}
       
   969 In [84]: a = ['a', 'b', 'c']
       
   970 In [85]: for x in a:
       
   971    ....:    print x, chr( ord(x) + 10 )
       
   972    ....:
       
   973 a  k
       
   974 b  l
       
   975 c  m
       
   976   \end{lstlisting}
       
   977   Iterating over the list and not the index + reference\\
       
   978   what if you want the index?
       
   979 \end{frame}
       
   980 
       
   981 \begin{frame}[fragile]
       
   982   \frametitle{\typ{for}: the list companion}
       
   983   \begin{lstlisting}
       
   984 In [89]: for p, ch in enumerate( a ):
       
   985    ....:     print p, ch
       
   986    ....:     
       
   987    ....:     
       
   988 0 a
       
   989 1 b
       
   990 2 c
       
   991   \end{lstlisting}
       
   992 Try: \typ{print enumerate(a)}
       
   993 \inctime{20}
       
   994 \end{frame}
       
   995 
       
   996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   997 % TIME: 20 m, running 170m 
       
   998 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   999 
       
  1000 \begin{frame}
       
  1001   {Problem set 3}
       
  1002   As you can guess, idea is to use \kwrd{for}!
       
  1003 \end{frame}
       
  1004 
       
  1005 \begin{frame}{Problem 3.1}
       
  1006   Which of the earlier problems is simpler when we use \kwrd{for} instead of \kwrd{while}? 
       
  1007 \end{frame}
       
  1008 
       
  1009 \begin{frame}{Problem 3.2}
       
  1010   Given an empty chessboard and one Bishop placed in any square, say (r, c), generate the list of all squares the Bishop could move to.
       
  1011 \end{frame}
       
  1012 
       
  1013 \begin{frame}[fragile]
       
  1014   \frametitle{Problem 3.3}
       
  1015 
       
  1016   Given two real numbers \typ{a, b}, and an integer \typ{N}, write a
       
  1017   function named \typ{linspace( a, b, N)} that returns an ordered list
       
  1018   of \typ{N} points starting with \typ{a} and ending in \typ{b} and
       
  1019   equally spaced.\\
       
  1020 
       
  1021   For example, \typ{linspace(0, 5, 11)}, should return, \\
       
  1022 \begin{lstlisting}
       
  1023 [ 0.0 ,  0.5,  1.0 ,  1.5,  2.0 ,  2.5,  
       
  1024   3.0 ,  3.5,  4.0 ,  4.5,  5.0 ]
       
  1025 \end{lstlisting}
       
  1026 \end{frame}
       
  1027 
       
  1028 \begin{frame}[fragile]
       
  1029   \frametitle{Problem 3.4a (optional)}
       
  1030 
       
  1031 Use the \typ{linspace} function and generate a list of N tuples of the form\\
       
  1032 \typ{[($x_1$,f($x_1$)),($x_2$,f($x_2$)),\ldots,($x_N$,f($x_N$))]}\\for the following functions,\begin{itemize}
       
  1033   \item \typ{f(x) = sin(x)}
       
  1034   \item \typ{f(x) = sin(x) + sin(10*x)}.
       
  1035 \end{itemize}
       
  1036 \end{frame}
       
  1037 
       
  1038 \begin{frame}[fragile]
       
  1039   \frametitle{Problem 3.4b (optional)}
       
  1040 
       
  1041   Using the tuples generated earlier, determine the intervals where the roots of the functions lie.
       
  1042 
       
  1043   \inctime{15}
       
  1044 \end{frame}
       
  1045 
       
  1046 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
  1047 % TIME: 15 m, running 185m 
       
  1048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
  1049 
       
  1050 \subsection{IO}
       
  1051 
       
  1052 \begin{frame}[fragile]
       
  1053   \frametitle{Simple tokenizing and parsing}
       
  1054   \begin{lstlisting}
       
  1055 s = """The quick brown fox jumped
       
  1056        over the lazy dog"""
       
  1057 for word in s.split():
       
  1058     print word.capitalize()
       
  1059   \end{lstlisting}
       
  1060 \end{frame}
       
  1061 
       
  1062 \begin{frame}[fragile]
       
  1063   \frametitle{Problem 4.1}
       
  1064   Given a string like, ``1, 3-7, 12, 15, 18-21'', produce the list \\
       
  1065   \begin{lstlisting}
       
  1066     [1,3,4,5,6,7,12,15,18,19,20,21]
       
  1067   \end{lstlisting}
       
  1068 \end{frame}
       
  1069 
       
  1070 \begin{frame}[fragile]
       
  1071   \frametitle{File handling}
       
  1072 \begin{lstlisting}
       
  1073 >>> f = open('/path/to/file_name')
       
  1074 >>> data = f.read() # Read entire file.
       
  1075 >>> line = f.readline() # Read one line.
       
  1076 >>> f.close() # close the file.
       
  1077 \end{lstlisting}
       
  1078 Writing files
       
  1079 \begin{lstlisting}
       
  1080 >>> f = open('/path/to/file_name', 'w')
       
  1081 >>> f.write('hello world\n')
       
  1082 >>> f.close()
       
  1083 \end{lstlisting}
       
  1084 \begin{itemize}
       
  1085     \item Everything read or written is a string
       
  1086 \end{itemize}
       
  1087 \emphbar{Try \typ{file?} for more help}
       
  1088 \end{frame}
       
  1089 
       
  1090 \begin{frame}[fragile]
       
  1091     \frametitle{File and \kwrd{for}}
       
  1092 \begin{lstlisting}
       
  1093 >>> f = open('/path/to/file_name')
       
  1094 >>> for line in f:
       
  1095 ...     print line
       
  1096 ...
       
  1097 \end{lstlisting}
       
  1098 \end{frame}
       
  1099 
       
  1100 \begin{frame}{Problem 4.2}
       
  1101     The given file has lakhs of records in the form:\\
       
  1102     \typ{RGN;ID;NAME;MARK1;\ldots;MARK5;TOTAL;PFW}\\
       
  1103     Some entries may be empty.  Read the data from this file and print the
       
  1104     name of the student with the maximum total marks.
       
  1105 \end{frame}
       
  1106 
       
  1107 \begin{frame}{Problem 4.3}
       
  1108     For the same data file compute the average marks in different
       
  1109     subjects, the student with the maximum mark in each subject and also
       
  1110     the standard deviation of the marks.  Do this efficiently.
       
  1111 
       
  1112     \inctime{20}
       
  1113 \end{frame}
       
  1114 
       
  1115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
  1116 % TIME: 20 m, running 205m 
       
  1117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
  1118 
       
  1119 \subsection{Modules}
       
  1120 
       
  1121 \begin{frame}[fragile]
       
  1122     {Modules}
       
  1123 \begin{lstlisting}
       
  1124 >>> sqrt(2)
       
  1125 Traceback (most recent call last):
       
  1126   File "<stdin>", line 1, in <module>
       
  1127 NameError: name 'sqrt' is not defined
       
  1128 >>> import math        
       
  1129 >>> math.sqrt(2)
       
  1130 1.4142135623730951
       
  1131 \end{lstlisting}
       
  1132 \end{frame}
       
  1133 
       
  1134 \begin{frame}[fragile]
       
  1135     {Modules}
       
  1136   \begin{itemize}
       
  1137     \item The \kwrd{import} keyword ``loads'' a module
       
  1138     \item One can also use:
       
  1139       \begin{lstlisting}
       
  1140 >>> from math import sqrt
       
  1141 >>> from math import *
       
  1142       \end{lstlisting}    
       
  1143     \item What is the difference?
       
  1144     \item \alert{Use the later only in interactive mode}
       
  1145     \end{itemize}
       
  1146   \emphbar{Package hierarchies}
       
  1147       \begin{lstlisting}
       
  1148 >>> from os.path import exists
       
  1149       \end{lstlisting}
       
  1150 \end{frame}
       
  1151 
       
  1152 \begin{frame}
       
  1153   \frametitle{Modules: Standard library}
       
  1154   \begin{itemize}
       
  1155   \item Very powerful, ``Batteries included''
       
  1156   \item Some standard modules:
       
  1157     \begin{itemize}
       
  1158     \item Math: \typ{math}, \typ{random}
       
  1159     \item Internet access: \typ{urllib2}, \typ{smtplib}
       
  1160     \item System, Command line arguments: \typ{sys}
       
  1161     \item Operating system interface: \typ{os}
       
  1162     \item Regular expressions: \typ{re}
       
  1163     \item Compression: \typ{gzip}, \typ{zipfile}, and \typ{tarfile}
       
  1164     \item And a whole lot more!
       
  1165     \end{itemize}
       
  1166   \item Check out the Python Library reference:
       
  1167     \url{http://docs.python.org/library/}
       
  1168   \end{itemize}
       
  1169 \end{frame}
       
  1170 
       
  1171 \begin{frame}[fragile]
       
  1172     {Modules of special interest}
       
  1173     \begin{description}[matplotlibfor2d]
       
  1174 
       
  1175         \item[\typ{numpy}] Efficient, powerful numeric arrays
       
  1176 
       
  1177         \item[\typ{matplotlib}] Easy, interactive, 2D plotting
       
  1178 
       
  1179         \item[\typ{scipy}] statistics, optimization, integration, linear
       
  1180             algebra, Fourier transforms, signal and image processing,
       
  1181             genetic algorithms, ODE solvers, special functions, and more
       
  1182 
       
  1183         \item[Mayavi] Easy, interactive, 3D plotting
       
  1184 
       
  1185     \end{description}
       
  1186 \end{frame}
       
  1187 
       
  1188 \begin{frame}[fragile]
       
  1189     {Creating your own modules}
       
  1190   \begin{itemize}
       
  1191   \item Define variables, functions and classes in a file with a
       
  1192     \typ{.py} extension
       
  1193   \item This file becomes a module!
       
  1194   \item Accessible when in the current directory
       
  1195   \item Use \typ{cd} in IPython to change directory
       
  1196 
       
  1197   \item Naming your module
       
  1198       \end{itemize}
       
  1199 \end{frame}
       
  1200 
       
  1201 \begin{frame}[fragile]
       
  1202   \frametitle{Modules: example}
       
  1203   \begin{lstlisting}
       
  1204 # --- arith.py ---
       
  1205 def gcd(a, b):
       
  1206     if a%b == 0: return b
       
  1207     return gcd(b, a%b)
       
  1208 def lcm(a, b):
       
  1209     return a*b/gcd(a, b)
       
  1210 # ------------------
       
  1211 >>> import arith
       
  1212 >>> arith.gcd(26, 65)
       
  1213 13
       
  1214 >>> arith.lcm(26, 65)
       
  1215 130
       
  1216   \end{lstlisting}
       
  1217 \end{frame}
       
  1218 
       
  1219 \begin{frame}[fragile]
       
  1220   \frametitle{Problem 5.1}
       
  1221 
       
  1222   Put all the functions you have written so far as part of the problems
       
  1223   into one module called \typ{iitb.py} and use this module from IPython.
       
  1224 
       
  1225 \inctime{20}
       
  1226 \end{frame}
       
  1227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
  1228 % TIME: 20 m, running 225m 
       
  1229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
  1230 
       
  1231 \begin{frame}
       
  1232   \frametitle{Did we meet the goal?}
       
  1233   \tableofcontents
       
  1234   % You might wish to add the option [pausesections]
       
  1235   \end{frame}
       
  1236 
       
  1237   \begin{frame}
       
  1238     {Tomorrow}
       
  1239     \begin{itemize}
       
  1240       \item Plotting: 2D, 3D
       
  1241       \item NumPy, SciPy
       
  1242       \item Dictionary, Set
       
  1243       \item Debugging
       
  1244       \item Testing
       
  1245       \item \ldots
       
  1246     \end{itemize}
       
  1247     11:30--13:00 Discussion of answers to problems OPTIONAL
       
  1248   \end{frame}
       
  1249 \end{document}
       
  1250 
       
  1251 
       
  1252 \begin{frame}[fragile]
       
  1253   \frametitle{More on functions}
       
  1254   \begin{itemize}
       
  1255   \item Support default and keyword arguments
       
  1256   \item Scope of variables in the function is local
       
  1257   \item Mutable items are \alert{passed by reference}
       
  1258   \item First line after definition may be a documentation string
       
  1259     (\alert{recommended!})
       
  1260   \item Function definition and execution defines a name bound to the
       
  1261     function
       
  1262   \item You \emph{can} assign a variable to a function!
       
  1263   \end{itemize}
       
  1264 \end{frame}
       
  1265 
       
  1266 
       
  1267 \begin{frame}[fragile]
       
  1268   \frametitle{Functions: default arguments}
       
  1269   \begin{lstlisting}
       
  1270 def ask_ok(prompt, retries=4, complaint='Yes or no!'):
       
  1271     while True:
       
  1272         ok = raw_input(prompt)
       
  1273         if ok in ('y', 'ye', 'yes'): 
       
  1274             return True
       
  1275         if ok in ('n', 'no', 'nop', 'nope'): 
       
  1276             return False
       
  1277         retries = retries - 1
       
  1278         if retries < 0: 
       
  1279             raise IOError, 'bad user'
       
  1280         print complaint
       
  1281   \end{lstlisting}
       
  1282 \end{frame}
       
  1283 
       
  1284 \begin{frame}[fragile]
       
  1285   \frametitle{Functions: keyword arguments}
       
  1286   \begin{lstlisting}
       
  1287 def parrot(voltage, state='a stiff', 
       
  1288            action='voom', type='Norwegian Blue'):
       
  1289     print "-- This parrot wouldn't", action,
       
  1290     print "if you put", voltage, "Volts through it."
       
  1291     print "-- Lovely plumage, the", type
       
  1292     print "-- It's", state, "!"
       
  1293 
       
  1294 parrot(1000)
       
  1295 parrot(action = 'VOOOOOM', voltage = 1000000)
       
  1296 parrot('a thousand', state = 'pushing up the daisies')
       
  1297 parrot('a million', 'bereft of life', 'jump')
       
  1298 \end{lstlisting}
       
  1299 \end{frame}
       
  1300 
       
  1301 \begin{frame}[fragile]
       
  1302   \frametitle{Functions: arbitrary argument lists}
       
  1303   \begin{itemize}
       
  1304   \item Arbitrary number of arguments using \verb+*args+ or
       
  1305     \verb+*whatever+
       
  1306   \item Keyword arguments using \verb+**kw+
       
  1307   \item Given a tuple/dict how do you call a function?
       
  1308     \begin{itemize}
       
  1309     \item Using argument unpacking
       
  1310     \item For positional arguments: \verb+foo(*[5, 10])+
       
  1311     \item For keyword args: \verb+foo(**{'a':5, 'b':10})+
       
  1312     \end{itemize}
       
  1313   \end{itemize}
       
  1314 \begin{lstlisting}
       
  1315 def foo(a=10, b=100):
       
  1316     print a, b
       
  1317 def func(*args, **keyword):
       
  1318     print args, keyword
       
  1319 # Unpacking:
       
  1320 args = [5, 10]
       
  1321 foo(*args)
       
  1322 kw = {'a':5, 'b':10}
       
  1323 foo(**kw)
       
  1324 \end{lstlisting}
       
  1325 \end{frame}
       
  1326 
       
  1327 \subsection{Modules, exceptions, classes}
       
  1328 
       
  1329 \begin{frame}
       
  1330   \frametitle{Modules}
       
  1331   \begin{itemize}
       
  1332   \item Define variables, functions and classes in a file with a
       
  1333     \typ{.py} extension
       
  1334   \item This file becomes a module!
       
  1335   \item Modules are searched in the following:
       
  1336     \begin{itemize}
       
  1337     \item Current directory
       
  1338     \item Standard: \typ{/usr/lib/python2.3/site-packages/} etc.
       
  1339     \item Directories specified in PYTHONPATH
       
  1340     \item \typ{sys.path}: current path settings (from the \typ{sys}
       
  1341       module)
       
  1342     \end{itemize}
       
  1343   \item The \typ{import} keyword ``loads'' a module
       
  1344   \item One can also use:
       
  1345     \mbox{\typ{from module import name1, name2, name2}}\\
       
  1346     where \typ{name1} etc. are names in the module, ``module''
       
  1347   \item \typ{from module import *} \ --- imports everything from module,
       
  1348     \alert{use only in interactive mode}
       
  1349   \end{itemize}
       
  1350 \end{frame}
       
  1351 
       
  1352 \begin{frame}[fragile]
       
  1353   \frametitle{Modules: example}
       
  1354   \begin{lstlisting}
       
  1355 # --- foo.py ---
       
  1356 some_var = 1
       
  1357 def fib(n): # write Fibonacci series up to n
       
  1358     """Print a Fibonacci series up to n."""
       
  1359     a, b = 0, 1
       
  1360     while b < n:
       
  1361         print b,
       
  1362         a, b = b, a+b
       
  1363 # EOF
       
  1364 
       
  1365 >>> import foo
       
  1366 >>> foo.fib(10)
       
  1367 1 1 2 3 5 8 
       
  1368 >>> foo.some_var
       
  1369 1
       
  1370   \end{lstlisting}
       
  1371 \end{frame}
       
  1372 
       
  1373 \begin{frame}[fragile]
       
  1374   \frametitle{Namespaces}
       
  1375   \begin{itemize}
       
  1376   \item A mapping from names to objects
       
  1377   \item Modules introduce a namespace 
       
  1378   \item So do classes
       
  1379   \item The running script's namespace is \verb+__main__+
       
  1380   \item A modules namespace is identified by its name
       
  1381   \item The standard functions (like \typ{len}) are in the
       
  1382     \verb+__builtin__+ namespace
       
  1383   \item Namespaces help organize different names and their bindings to
       
  1384     different objects
       
  1385   \end{itemize}
       
  1386 \end{frame}
       
  1387 
       
  1388 \begin{frame}
       
  1389   \frametitle{Exceptions}
       
  1390   \begin{itemize}
       
  1391   \item Python's way of notifying you of errors
       
  1392   \item Several standard exceptions: \typ{SyntaxError}, \typ{IOError}
       
  1393     etc.
       
  1394   \item Users can also \typ{raise} errors
       
  1395   \item Users can create their own exceptions
       
  1396   \item Exceptions can be ``caught'' via \typ{try/except} blocks
       
  1397   \end{itemize}
       
  1398 \end{frame}
       
  1399 
       
  1400 \begin{frame}[fragile]
       
  1401   \frametitle{Exception: examples}
       
  1402 \begin{lstlisting}
       
  1403 >>> 10 * (1/0)
       
  1404 Traceback (most recent call last):
       
  1405   File "<stdin>", line 1, in ?
       
  1406 ZeroDivisionError: integer division or modulo by zero
       
  1407 >>> 4 + spam*3
       
  1408 Traceback (most recent call last):
       
  1409   File "<stdin>", line 1, in ?
       
  1410 NameError: name 'spam' is not defined
       
  1411 >>> '2' + 2
       
  1412 Traceback (most recent call last):
       
  1413   File "<stdin>", line 1, in ?
       
  1414 TypeError: cannot concatenate 'str' and 'int' objects
       
  1415 \end{lstlisting}
       
  1416 \end{frame}
       
  1417 
       
  1418 \begin{frame}[fragile]
       
  1419   \frametitle{Exception: examples}
       
  1420 \begin{lstlisting}
       
  1421 >>> while True:
       
  1422 ...     try:
       
  1423 ...         x = int(raw_input("Enter a number: "))
       
  1424 ...         break
       
  1425 ...     except ValueError:
       
  1426 ...         print "Invalid number, try again..."
       
  1427 ...
       
  1428 >>> # To raise exceptions
       
  1429 ... raise ValueError, "your error message"
       
  1430 Traceback (most recent call last):
       
  1431   File "<stdin>", line 2, in ?
       
  1432 ValueError: your error message
       
  1433 \end{lstlisting}
       
  1434 \end{frame}
       
  1435 
       
  1436 \begin{frame}[fragile]
       
  1437   \frametitle{Classes: the big picture}
       
  1438   \begin{itemize}
       
  1439   \item Lets you create new data types
       
  1440   \item Class is a template for an object belonging to that class
       
  1441   \item Note: in Python a class is also an object
       
  1442   \item Instantiating a class creates an instance (an object)
       
  1443   \item An instance encapsulates the state (data) and behavior
       
  1444     (methods)
       
  1445   \item Allows you to define an inheritance hierarchy
       
  1446     \begin{itemize}
       
  1447     \item ``A Honda car \alert{is a} car.''
       
  1448     \item ``A car \alert{is an} automobile.''
       
  1449     \item ``A Python \alert{is a} reptile.''
       
  1450     \end{itemize}
       
  1451   \item Programmers need to think OO
       
  1452   \end{itemize}
       
  1453 \end{frame}
       
  1454 
       
  1455 \begin{frame}[fragile]
       
  1456   \frametitle{Classes: what's the big deal?}
       
  1457   \begin{itemize}
       
  1458   \item Lets you create objects that mimic a real problem being
       
  1459     simulated
       
  1460   \item Makes problem solving more natural and elegant
       
  1461   \item Easier to create code
       
  1462   \item Allows for code-reuse
       
  1463   \item Polymorphism
       
  1464   \end{itemize}
       
  1465 \end{frame}
       
  1466 
       
  1467 \begin{frame}[fragile]
       
  1468   \frametitle{Class definition and instantiation}
       
  1469   \begin{itemize}
       
  1470   \item Class definitions when executed create class objects
       
  1471   \item Instantiating the class object creates an instance of the
       
  1472     class
       
  1473   \end{itemize}
       
  1474 \footnotesize
       
  1475 \begin{lstlisting}
       
  1476 class Foo(object):
       
  1477     pass
       
  1478 # class object created.
       
  1479 # Create an instance of Foo.
       
  1480 f = Foo()
       
  1481 # Can assign an attribute to the instance
       
  1482 f.a = 100
       
  1483 print f.a
       
  1484 100
       
  1485 \end{lstlisting}
       
  1486 \end{frame}
       
  1487 
       
  1488 \begin{frame}[fragile]
       
  1489   \frametitle{Classes \ldots}
       
  1490   \begin{itemize}
       
  1491   \item All attributes are accessed via the \typ{object.attribute}
       
  1492     syntax
       
  1493   \item Both class and instance attributes are supported
       
  1494   \item \emph{Methods} represent the behavior of an object: crudely
       
  1495     think of them as functions ``belonging'' to the object
       
  1496   \item All methods in Python are ``virtual''
       
  1497   \item Inheritance through subclassing
       
  1498   \item Multiple inheritance is supported
       
  1499   \item No special public and private attributes: only good
       
  1500     conventions
       
  1501     \begin{itemize}
       
  1502     \item \verb+object.public()+: public
       
  1503     \item \verb+object._private()+ \& \verb+object.__priv()+: 
       
  1504       non-public
       
  1505     \end{itemize}
       
  1506   \end{itemize}
       
  1507 \end{frame}
       
  1508 
       
  1509 \begin{frame}[fragile]
       
  1510   \frametitle{Classes: examples}
       
  1511 \begin{lstlisting}
       
  1512 class MyClass(object):
       
  1513     """Example class (this is the class docstring)."""
       
  1514     i = 12345 # A class attribute
       
  1515     def f(self):
       
  1516         """This is the method docstring"""
       
  1517         return 'hello world'
       
  1518 
       
  1519 >>> a = MyClass() # creates an instance
       
  1520 >>> a.f()
       
  1521 'hello world'
       
  1522 >>> # a.f() is equivalent to MyClass.f(a)
       
  1523 ... # This also explains why f has a 'self' argument.
       
  1524 ... MyClass.f(a)
       
  1525 'hello world'
       
  1526 \end{lstlisting}
       
  1527 \end{frame}
       
  1528 
       
  1529 \begin{frame}[fragile]
       
  1530   \frametitle{Classes (continued)}
       
  1531   \begin{itemize}
       
  1532   \item \typ{self} is \alert{conventionally} the first argument for a
       
  1533     method
       
  1534   \item In previous example, \typ{a.f} is a method object
       
  1535   \item When \typ{a.f} is called, it is passed the instance \typ{a} as
       
  1536     the first argument
       
  1537   \item If a method called \verb+__init__+ exists, it is called when
       
  1538     the object is created
       
  1539   \item If a method called \verb+__del__+ exists, it is called before
       
  1540     the object is garbage collected
       
  1541   \item Instance attributes are set by simply ``setting'' them in
       
  1542     \typ{self}
       
  1543   \item Other special methods (by convention) like \verb+__add__+ let
       
  1544     you define numeric types:
       
  1545     {\footnotesize \url{http://docs.python.org/ref/specialnames.html}
       
  1546       \\ \url{http://docs.python.org/ref/numeric-types.html}
       
  1547     }
       
  1548   \end{itemize}
       
  1549 \end{frame}
       
  1550 
       
  1551 \begin{frame}[fragile]
       
  1552   \frametitle{Classes: examples}
       
  1553 \begin{lstlisting}
       
  1554 class Bag(MyClass): # Shows how to derive classes
       
  1555     def __init__(self): # called on object creation.
       
  1556         self.data = [] # an instance attribute
       
  1557     def add(self, x):
       
  1558         self.data.append(x)
       
  1559     def addtwice(self, x):
       
  1560         self.add(x)
       
  1561         self.add(x)
       
  1562 >>> a = Bag()
       
  1563 >>> a.f() # Inherited method
       
  1564 'hello world'
       
  1565 >>> a.add(1); a.addtwice(2)
       
  1566 >>> a.data
       
  1567 [1, 2, 2]
       
  1568 \end{lstlisting}
       
  1569 \end{frame}
       
  1570 
       
  1571 \begin{frame}[fragile]
       
  1572   \frametitle{Derived classes}
       
  1573   \begin{itemize}
       
  1574   \item Call the parent's \verb+__init__+ if needed
       
  1575   \item If you don't need a new constructor, no need to define it in subclass
       
  1576   \item Can also use the \verb+super+ built-in function
       
  1577   \end{itemize}
       
  1578 \begin{lstlisting}
       
  1579 class AnotherBag(Bag):
       
  1580     def __init__(self):
       
  1581         # Must call parent's __init__ explicitly
       
  1582         Bag.__init__(self)
       
  1583         # Alternatively use this:
       
  1584         super(AnotherBag, self).__init__()
       
  1585         # Now setup any more data.
       
  1586         self.more_data = []
       
  1587 \end{lstlisting}
       
  1588 \end{frame}
       
  1589 
       
  1590 \begin{frame}[fragile]
       
  1591   \frametitle{Classes: polymorphism}
       
  1592 \begin{lstlisting}
       
  1593 class Drawable(object):
       
  1594     def draw(self):
       
  1595         # Just a specification.
       
  1596         pass
       
  1597 \end{lstlisting}
       
  1598 \mode<presentation>{\pause}
       
  1599 \begin{lstlisting}
       
  1600 class Square(Drawable):
       
  1601     def draw(self):
       
  1602         # draw a square.
       
  1603 class Circle(Drawable):
       
  1604     def draw(self):
       
  1605         # draw a circle.
       
  1606 \end{lstlisting}
       
  1607 \mode<presentation>{\pause}
       
  1608 \begin{lstlisting}
       
  1609 class Artist(Drawable):
       
  1610     def draw(self):
       
  1611         for obj in self.drawables:
       
  1612             obj.draw()
       
  1613 \end{lstlisting}
       
  1614 \end{frame}
       
  1615 
       
  1616 \subsection{Miscellaneous}
       
  1617 
       
  1618 \begin{frame}[fragile]
       
  1619   \frametitle{Stand-alone scripts}
       
  1620 Consider a file \typ{f.py}:
       
  1621 \begin{lstlisting}
       
  1622 #!/usr/bin/env python
       
  1623 """Module level documentation."""
       
  1624 # First line tells the shell that it should use Python
       
  1625 # to interpret the code in the file.
       
  1626 def f():
       
  1627     print "f"
       
  1628 
       
  1629 # Check if we are running standalone or as module.
       
  1630 # When imported, __name__ will not be '__main__'
       
  1631 if __name__ == '__main__':
       
  1632     # This is not executed when f.py is imported.
       
  1633     f() 
       
  1634 \end{lstlisting}
       
  1635 \end{frame}
       
  1636 
       
  1637 \begin{frame}[fragile]
       
  1638   \frametitle{List comprehensions}
       
  1639 \begin{lstlisting}
       
  1640 >>> veg = ['tomato', 'cabbage', 'carrot', 'potato']
       
  1641 >>> [x.upper() for x in veg]
       
  1642 ['TOMATO', 'CABBAGE', 'CARROT', 'POTATO']
       
  1643 >>> vec = range(0, 8)
       
  1644 >>> even = [x for x in vec if x%2 == 0]
       
  1645 >>> even
       
  1646 [0, 2, 4, 6]
       
  1647 >>> [x*x for x in even]
       
  1648 [0, 4, 16, 36]
       
  1649 >>> odd = [x for x in vec if x%2 == 1]
       
  1650 >>> odd
       
  1651 [1, 3, 5, 7]
       
  1652 >>> [x*y for x in even for y in odd]
       
  1653 [0, 0, 0, 0, 2, 6, 10, 14, 4, 12, 20, 28, 6, 18,30,42]
       
  1654 \end{lstlisting}
       
  1655 \end{frame}
       
  1656 
       
  1657 \begin{frame}[fragile]
       
  1658   \frametitle{More IPython features}
       
  1659   \begin{itemize}
       
  1660   \item Input and output caching:
       
  1661     \begin{itemize}
       
  1662     \item \verb+In+: a list of all entered input
       
  1663     \item \verb+Out+: a dict of all output
       
  1664     \item \verb+_+, \verb+__+, \verb+__+ are the last three results as
       
  1665       is \verb+_N+
       
  1666     \item \verb+%hist [-n]+ macro shows previous history, \verb+-n+
       
  1667       suppresses line number information
       
  1668     \end{itemize}
       
  1669   \item Log the session using \verb+%logstart+, \verb+%logon+ and
       
  1670     \verb+%logoff+
       
  1671   \item \verb+%run [options] file[.py]+ -- running Python code
       
  1672     \begin{itemize}
       
  1673     \item \verb+%run -d [-b<N>]+: debug script with pdb
       
  1674           \verb+N+ is the line number to break at (defaults to 1)
       
  1675     \item \verb+%run -t+: time the script
       
  1676     \item \verb+%run -p+: Profile the script
       
  1677     \end{itemize}
       
  1678   \item \verb+%prun+ runs a statement/expression under the profiler
       
  1679   \item \verb+%macro [options] macro_name n1-n2 n3-n4 n6+ save specified
       
  1680     lines to a macro with name \verb+macro_name+
       
  1681   \end{itemize}
       
  1682 \end{frame}
       
  1683 
       
  1684 \begin{frame}[fragile]
       
  1685   \frametitle{More IPython features \ldots}
       
  1686   \begin{itemize}
       
  1687   \item \verb+%edit [options] [args]+: edit lines of code or file
       
  1688     specified in editor (configure editor via \verb+$EDITOR+)
       
  1689   \item \verb+%cd+ changes directory, see also \verb+%pushd, %popd, %dhist+
       
  1690   \item Shell access
       
  1691     \begin{itemize}
       
  1692     \item \verb+!command+ runs a shell command and returns its output
       
  1693     \item \verb+files = %sx ls+ or \verb+files = !ls+ sets
       
  1694       \verb+files+ to all result of the \verb+ls+ command
       
  1695     \item \verb+%sx+ is quiet
       
  1696     \item \verb+!ls $files+ passes the \verb+files+ variable to the
       
  1697       shell command
       
  1698     \item \verb+%alias alias_name cmd+ creates an alias for a system
       
  1699       command
       
  1700     \end{itemize}
       
  1701   \item \verb+%colors+ lets you change the color scheme to
       
  1702     \verb+NoColor, Linux, LightBG+
       
  1703   \end{itemize}
       
  1704 \end{frame}
       
  1705 
       
  1706 \begin{frame}[fragile]
       
  1707   \frametitle{More IPython features \ldots}
       
  1708   \begin{itemize}
       
  1709   \item Use \verb+;+ at the end of a statement to suppress printing
       
  1710     output
       
  1711   \item \verb+%bookmark+: store a bookmarked location, for use with \verb+%cd+
       
  1712   \item \verb+%who, %whos+: print information on variables
       
  1713   \item \verb+%save [options] filename n1-n2 n3-n4+: save lines to a
       
  1714     file
       
  1715   \item \verb+%time statement+: Time execution of a Python statement or
       
  1716     expression
       
  1717   \item \verb+%timeit [-n<N> -r<R> [-t|-c]] statement+: time execution
       
  1718     using Python's timeit module
       
  1719   \item Can define and use profiles to setup IPython differently:
       
  1720     \verb+math, scipy, numeric, pysh+ etc.
       
  1721   \item \verb+%magic+: \alert{Show help on all magics}
       
  1722   \end{itemize}
       
  1723 \end{frame}
       
  1724 
       
  1725 \begin{frame}[fragile]
       
  1726   \frametitle{File handling}
       
  1727 \begin{lstlisting}
       
  1728 >>> # Reading files:
       
  1729 ... f = open('/path/to/file_name')
       
  1730 >>> data = f.read() # Read entire file.
       
  1731 >>> line = f.readline() # Read one line.
       
  1732 >>> # Read entire file appending each line into a list
       
  1733 ... lines = f.readlines()
       
  1734 >>> f.close() # close the file.
       
  1735 >>> # Writing files:
       
  1736 ... f = open('/path/to/file_name', 'w')
       
  1737 >>> f.write('hello world\n')
       
  1738 \end{lstlisting}
       
  1739   \begin{itemize}
       
  1740   \item \typ{tell()}: returns int of current position
       
  1741   \item \typ{seek(pos)}: moves current position to specified byte
       
  1742   \item Call \typ{close()} when done using a file
       
  1743   \end{itemize}
       
  1744 \end{frame}
       
  1745 
       
  1746 \begin{frame}[fragile]
       
  1747   \frametitle{Math}
       
  1748   \begin{itemize}
       
  1749   \item \typ{math} module provides basic math routines for
       
  1750     floats
       
  1751   \item \typ{cmath} module provides math routies for complex
       
  1752     numbers
       
  1753   \item \typ{random}: provides pseudo-random number generators
       
  1754     for various distributions
       
  1755   \item These are always available and part of the standard library
       
  1756   \item More serious math is provided by the NumPy/SciPy modules --
       
  1757     these are not standard and need to be installed separately
       
  1758   \end{itemize}
       
  1759 \end{frame}
       
  1760 
       
  1761 \begin{frame}[fragile]
       
  1762   \frametitle{Timing and profiling}
       
  1763   \begin{itemize}
       
  1764   \item Timing code: use the \typ{time} module
       
  1765   \item Read up on \typ{time.time()} and \typ{time.clock()}
       
  1766   \item \typ{timeit}: is a better way of doing timing
       
  1767   \item IPython has handy \typ{time} and \typ{timeit} macros (type
       
  1768     \typ{timeit?} for help)
       
  1769   \item IPython lets you debug and profile code via the \typ{run}
       
  1770     macro (type \typ{run?} on the prompt to learn more)
       
  1771   \end{itemize}
       
  1772 \end{frame}
       
  1773 
       
  1774 \begin{frame}[fragile]
       
  1775   \frametitle{Odds and ends}
       
  1776   \begin{itemize}
       
  1777   \item \typ{dir([object])} function: attributes of given object
       
  1778   \item \typ{type(object)}: returns type information
       
  1779   \item \typ{str(), repr()}: convert object to string representation
       
  1780   \item \typ{isinstance, issubclass}
       
  1781   \item \typ{assert} statements let you do debugging assertions in
       
  1782     code
       
  1783   \item \typ{csv} module: reading and writing CSV files
       
  1784   \item \typ{pickle}: lets you save and load Python objects
       
  1785     (\alert{serialization})
       
  1786   \item \typ{sys.argv}: command line arguments
       
  1787   \item \typ{os.path}: common path manipulations
       
  1788   \item Check out the Python Library reference:
       
  1789     \url{http://docs.python.org/lib/lib.html}
       
  1790   \end{itemize}
       
  1791 \end{frame}
       
  1792 
       
  1793 \begin{frame}[fragile]
       
  1794   \frametitle{Test driven development (TDD)}
       
  1795   \begin{itemize}
       
  1796       \item Why?
       
  1797           \begin{itemize}
       
  1798 
       
  1799             \item Forces you to write reusable code!
       
  1800 
       
  1801             \item Think about the API
       
  1802 
       
  1803             \item More robust
       
  1804 
       
  1805             \item Makes refactoring very easy
       
  1806 
       
  1807           \end{itemize}
       
  1808       \item How?  Python offers three major ways of doing this
       
  1809           \begin{itemize}
       
  1810               \item doctest
       
  1811               \item unittest
       
  1812               \item nosetest (and similar like py.test)
       
  1813           \end{itemize}
       
  1814 
       
  1815       \item Test every piece of functionality you offer
       
  1816 
       
  1817       \item This isn't a formal introduction but more a practical one
       
  1818 
       
  1819   \end{itemize}
       
  1820 \end{frame}
       
  1821 
       
  1822 \begin{frame}[fragile]
       
  1823   \frametitle{Unit test}
       
  1824 \begin{lstlisting}
       
  1825 import unittest
       
  1826 
       
  1827 class MyTestCase(unittest.TestCase):
       
  1828     def setUp(self):
       
  1829         # Called *before* each test_* 
       
  1830     def tearDown(self):
       
  1831         # Called *after* each test_* 
       
  1832     def test_something(self):
       
  1833         "docstring"
       
  1834         #  Test code.
       
  1835         self.assertEqual(x, y)
       
  1836         self.assertRaises(ValueError, func, arg1, arg2 ...)
       
  1837 
       
  1838 if __name__ == '__main__':
       
  1839     unittest.main() 
       
  1840 \end{lstlisting}
       
  1841 \end{frame}
       
  1842 
       
  1843 \begin{frame}[fragile]
       
  1844     \frametitle{Nosetest}
       
  1845 \begin{lstlisting}
       
  1846 import particle
       
  1847 def test_particle():
       
  1848     # Use asserts here.
       
  1849     p = particle.Particle(1.0)
       
  1850     assert p.property[0] == 1.0
       
  1851     assert p.property[2] == 0.0
       
  1852 
       
  1853 if __name__ == '__main__':
       
  1854     import nose
       
  1855     nose.main()
       
  1856 \end{lstlisting}
       
  1857 \end{frame}
       
  1858 
       
  1859 \begin{frame}[fragile]
       
  1860   \frametitle{Testing}
       
  1861   \begin{itemize}
       
  1862       \item More details: see library reference and search for nosetest
       
  1863   \end{itemize}
       
  1864 \end{frame}
       
  1865 
       
  1866 \section{Numerics \& Plotting}
       
  1867 
       
  1868 \subsection{NumPy Arrays}
       
  1869 
       
  1870 \newcommand{\num}{\texttt{numpy}}
       
  1871 
       
  1872 \begin{frame}
       
  1873   \frametitle{The \num\ module}
       
  1874   \begin{itemize}
       
  1875   \item Manipulating large Python lists for scientific computing is
       
  1876     \alert{slow}
       
  1877   \item Most complex computations can be reduced to a few standard
       
  1878     operations
       
  1879   \item The \num\ module provides:
       
  1880     \begin{itemize}
       
  1881     \item An efficient and powerful array type for various common data
       
  1882       types
       
  1883     \item Abstracts out the most commonly used standard operations on
       
  1884       arrays
       
  1885     \end{itemize}
       
  1886   \item Numeric was the first, then came \texttt{numarray}.
       
  1887     \texttt{numpy} is the latest and is the future
       
  1888   \item This course uses \num\ and only covers the absolute basics
       
  1889   \end{itemize}
       
  1890 \end{frame}
       
  1891 
       
  1892 \begin{frame}
       
  1893   \frametitle{Basic concepts}
       
  1894   \begin{itemize}
       
  1895   \item \num\ arrays are of a fixed size (\typ{arr.size}) and have the
       
  1896     same type (\typ{arr.dtype})
       
  1897   \item \num\ arrays may have arbitrary dimensionality
       
  1898   \item The \typ{shape} of an array is the extent (length) of the
       
  1899     array along each dimension
       
  1900   \item The \typ{rank(arr)} of an array is the ``dimensionality'' of the
       
  1901     array
       
  1902   \item The \typ{arr.itemsize} is the number of bytes (8-bits) used for
       
  1903     each element of the array
       
  1904   \item \alert{Note:} The \typ{shape} and \typ{rank} may change as
       
  1905     long as the \typ{size} of the array is fixed
       
  1906   \item \alert{Note:} \typ{len(arr) != arr.size} in general
       
  1907   \item \alert{Note:} By default array operations are performed
       
  1908     \alert{elementwise}
       
  1909   \item Indices start from 0
       
  1910   \end{itemize}
       
  1911 \end{frame}
       
  1912 
       
  1913 \begin{frame}[fragile]
       
  1914   \frametitle{Examples of \num}
       
  1915 \begin{lstlisting}
       
  1916 # Simple array math example
       
  1917 >>> from numpy import *
       
  1918 >>> a = array([1,2,3,4])
       
  1919 >>> b = array([2,3,4,5])
       
  1920 >>> a + b # Element wise addition!
       
  1921 array([3, 5, 7, 9])
       
  1922 
       
  1923 >>> print pi, e  # Pi and e are defined.
       
  1924 3.14159265359 2.71828182846
       
  1925 # Create array from 0 to 10
       
  1926 >>> x = arange(0.0, 10.0, 0.05)
       
  1927 >>> x *= 2*pi/10 # multiply array by scalar value
       
  1928 array([ 0.,0.0314,...,6.252])
       
  1929 # apply functions to array.
       
  1930 >>> y = sin(x)
       
  1931 \end{lstlisting}
       
  1932 \end{frame}
       
  1933 
       
  1934 \begin{frame}[fragile]
       
  1935   \frametitle{More examples of \num}
       
  1936 \vspace*{-8pt}
       
  1937 \begin{lstlisting}
       
  1938 # Size, shape, rank, type etc.
       
  1939 >>> x = array([1., 2, 3, 4])
       
  1940 >>> size(x)
       
  1941 4
       
  1942 >>> x.dtype # or x.dtype.char
       
  1943 'd'
       
  1944 >>> x.shape
       
  1945 (4,)
       
  1946 >>> print rank(x), x.itemsize
       
  1947 1 8
       
  1948 >>> x.tolist()
       
  1949 [1.0, 2.0, 3.0, 4.0]
       
  1950 # Array indexing
       
  1951 >>> x[0] = 10
       
  1952 >>> print x[0], x[-1]
       
  1953 10.0 4.0
       
  1954 \end{lstlisting}
       
  1955 \end{frame}
       
  1956 
       
  1957 \begin{frame}[fragile]
       
  1958   \frametitle{Multi-dimensional arrays}
       
  1959 \begin{lstlisting}
       
  1960 >>> a = array([[ 0, 1, 2, 3],
       
  1961 ...            [10,11,12,13]])
       
  1962 >>> a.shape # (rows, columns)
       
  1963 (2, 4)
       
  1964 # Accessing and setting values
       
  1965 >>> a[1,3] 
       
  1966 13
       
  1967 >>> a[1,3] = -1
       
  1968 >>> a[1] # The second row
       
  1969 array([10,11,12,-1])
       
  1970 
       
  1971 # Flatten/ravel arrays to 1D arrays
       
  1972 >>> a.flat  # or ravel(a)
       
  1973 array([0,1,2,3,10,11,12,-1])
       
  1974 # Note: flat references original memory
       
  1975 \end{lstlisting}
       
  1976 \end{frame}
       
  1977 
       
  1978 \begin{frame}[fragile]
       
  1979   \frametitle{Slicing arrays}
       
  1980 \begin{lstlisting}
       
  1981 >>> a = array([[1,2,3], [4,5,6], [7,8,9]])
       
  1982 >>> a[0,1:3]
       
  1983 array([2, 3])
       
  1984 >>> a[1:,1:]
       
  1985 array([[5, 6],
       
  1986        [8, 9]])
       
  1987 >>> a[:,2]
       
  1988 array([3, 6, 9])
       
  1989 # Striding...
       
  1990 >>> a[0::2,0::2]
       
  1991 array([[1, 3],
       
  1992        [7, 9]])
       
  1993 # All these slices are references to the same memory!
       
  1994 \end{lstlisting}
       
  1995 \end{frame}
       
  1996 
       
  1997 % \begin{frame}[fragile]
       
  1998 %   \frametitle{Array types and typecodes}
       
  1999 %   \begin{tabular}[c]{|c|c|p{2.75in}|}
       
  2000 %     \hline
       
  2001 %     Character & Bits (bytes) & Type name \\
       
  2002 %     \hline
       
  2003 %     D & 128 (16) & \typ{Complex, Complex64}\\
       
  2004 %     F & 64   (8) & \typ{Complex0, Complex8, Complex16} \\
       
  2005 %     d & 64   (8) & \typ{Float, Float64} \\
       
  2006 %     f & 32   (4) & \typ{Float0, Float8, Float16} \\
       
  2007 %     i & 32   (4) & \typ{Int32} \\
       
  2008 %     l & 32   (4) & \typ{Int} \\
       
  2009 %     O & 4    (1) & \typ{PyObject} \\
       
  2010 %     %b 8     (1) UnsignedInt8
       
  2011 %     %1 (one) 8     (1) Int8
       
  2012 %     %s 16   (2) Int16
       
  2013 %     \hline
       
  2014 %   \end{tabular}
       
  2015 % \begin{lstlisting}
       
  2016 % # Examples
       
  2017 % >>> f = array([1,2,3], Float32)
       
  2018 % >>> c = array([1,2,3], Complex32)
       
  2019 % >>> print f, c
       
  2020 % [ 1.  2.  3.] [ 1.+0.j  2.+0.j  3.+0.j]
       
  2021 % \end{lstlisting}
       
  2022 % \end{frame}
       
  2023 
       
  2024 \begin{frame}[fragile]
       
  2025   \frametitle{Array creation functions}
       
  2026   \begin{itemize}
       
  2027   \item \typ{array(object, dtype=None, copy=1,order=None, subok=0,ndmin=0)}
       
  2028   \item \typ{arange(start, stop=None, step=1, dtype=None)}
       
  2029   \item \typ{linspace(start, stop, num=50, endpoint=True, retstep=False)}
       
  2030   \item \typ{ones(shape, dtype=None, order='C')}
       
  2031   \item \typ{zeros((d1,...,dn),dtype=float,order='C')}
       
  2032   \item \typ{identity(n)}
       
  2033   \item \typ{empty((d1,...,dn),dtype=float,order='C')}
       
  2034   \item \typ{ones\_like(x)}, \typ{zeros\_like(x)}, \typ{empty\_like(x)}
       
  2035   \end{itemize}
       
  2036 \end{frame}
       
  2037 
       
  2038 \begin{frame}[fragile]
       
  2039   \frametitle{Array math}
       
  2040   \begin{itemize}
       
  2041   \item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
       
  2042     \begin{itemize}
       
  2043     \item \typ{a + b $\rightarrow$ add(a, b)} 
       
  2044     \item \typ{a - b, $\rightarrow$ subtract(a, b)} 
       
  2045     \item \typ{a * b, $\rightarrow$ multiply(a, b)} 
       
  2046     \item \typ{a / b, $\rightarrow$ divide(a, b)} 
       
  2047     \item \typ{a \% b, $\rightarrow$ remainder(a, b)} 
       
  2048     \item \typ{a ** b, $\rightarrow$ power(a, b)}
       
  2049     \end{itemize}
       
  2050   \item Inplace operators: \typ{a += b}, or \typ{add(a, b,
       
  2051       a)} etc.
       
  2052   \item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)},
       
  2053     \typ{less (<)}, \typ{greater (>)} etc.
       
  2054   \item Trig and other functions: \typ{sin(x), arcsin(x), sinh(x),
       
  2055       exp(x), sqrt(x)} etc.
       
  2056   \item \typ{sum(x, axis=0), product(x, axis=0)}: sum and product of array elements 
       
  2057   \item \typ{dot(a, b)}
       
  2058   \end{itemize}
       
  2059 \end{frame}
       
  2060 
       
  2061 \begin{frame}[fragile]
       
  2062   \frametitle{Advanced}
       
  2063   \begin{itemize}
       
  2064   \item Only scratched the surface of \num
       
  2065   \item Ufunc methods: \typ{reduce, accumulate, outer, reduceat}
       
  2066   \item Typecasting
       
  2067   \item More functions: \typ{take, choose, where, compress,
       
  2068       concatenate}
       
  2069   \item Array broadcasting and \typ{None}
       
  2070   \end{itemize}
       
  2071 \end{frame}
       
  2072 
       
  2073 \subsection{Plotting: Matplotlib}
       
  2074 
       
  2075 \begin{frame}
       
  2076   \frametitle{About \texttt{matplotlib}}
       
  2077   \begin{itemize}
       
  2078   \item Easy to use, scriptable, ``Matlab-like'' 2D plotting
       
  2079   \item Publication quality figures and interactive capabilities
       
  2080   \item Plots, histograms, power spectra, bar charts, errorcharts,
       
  2081     scatterplots, etc.
       
  2082   \item Also does polar plots, maps, contours
       
  2083   \item Support for simple \TeX\ markup
       
  2084   \item Multiple output backends (images, EPS, SVG, wx, Agg, Tk, GTK)
       
  2085   \item Cross-platform: Linux, Win32, Mac OS X
       
  2086   \item Good idea to use via IPython:  \typ{ipython -pylab}
       
  2087   \item From scripts use: \typ{import pylab}
       
  2088   \end{itemize}
       
  2089 \end{frame}
       
  2090 
       
  2091 \begin{frame}
       
  2092   \frametitle{More information}
       
  2093   \begin{itemize}
       
  2094   \item More information here: \url{http://matplotlib.sf.net}
       
  2095   \item \url{http://matplotlib.sf.net/tutorial.html}
       
  2096   \item \url{http://matplotlib.sf.net/screenshots.html}
       
  2097   \end{itemize}
       
  2098 \end{frame}
       
  2099 
       
  2100 \begin{frame}[fragile]
       
  2101   \frametitle{Basic plotting with \texttt{matplotlib}}
       
  2102 \begin{lstlisting}
       
  2103 >>> x = arange(0, 2*pi, 0.05)
       
  2104 >>> plot(x, sin(x)) # Same as plot(x, sin(x), 'b-') 
       
  2105 >>> plot(x, sin(x), 'ro')
       
  2106 >>> axis([0,2*pi, -1,1])
       
  2107 >>> xlabel(r'$\chi$', color='g')
       
  2108 >>> ylabel(r'sin($\chi$)', color='r')
       
  2109 >>> title('A simple figure', fontsize=20)
       
  2110 >>> savefig('/tmp/test.eps')
       
  2111 # Multiple plots in one figure
       
  2112 >>> t = arange(0.0, 5.2, 0.2)
       
  2113 # red dashes, blue squares and green triangles
       
  2114 >>> plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
       
  2115 \end{lstlisting}
       
  2116 \end{frame}
       
  2117 
       
  2118 \begin{frame}[fragile]
       
  2119   \frametitle{Basic plotting \ldots}
       
  2120 \begin{lstlisting}
       
  2121 # Set properties of objects:
       
  2122 >>> plot(x, sin(x), linewidth=2.0, color='r')
       
  2123 >>> l, = plot(x, sin(x))
       
  2124 >>> setp(l, linewidth=2.0, color='r')
       
  2125 >>> l.set_linewidth(2.0); l.set_color('r')
       
  2126 >>> draw() # Redraws current figure.
       
  2127 >>> setp(l) # Prints available properties
       
  2128 >>> close() # Closes the figure.
       
  2129 # Multiple figures:
       
  2130 >>> figure(1); plot(x, sin(x))
       
  2131 >>> figure(2); plot(x, tanh(x))
       
  2132 >>> figure(1); title('Easy as 1,2,3')
       
  2133 \end{lstlisting}
       
  2134 \end{frame}
       
  2135 
       
  2136 \begin{frame}[fragile]
       
  2137   \frametitle{Basic plotting \ldots}
       
  2138 \begin{lstlisting}
       
  2139 >>> figure(1)
       
  2140 >>> subplot(211) # Same as subplot(2, 1, 1)
       
  2141 >>> plot(x, cos(5*x)*exp(-x))
       
  2142 >>> subplot(2, 1, 2)
       
  2143 >>> plot(x, cos(5*x), 'r--', label='cosine')
       
  2144 >>> plot(x, sin(5*x), 'g--', label='sine')
       
  2145 >>> legend() # Or legend(['cosine', 'sine'])
       
  2146 >>> text(1,0, '(1,0)')
       
  2147 >>> axes = gca() # Current axis
       
  2148 >>> fig = gcf() # Current figure
       
  2149 \end{lstlisting}
       
  2150 \end{frame}
       
  2151 
       
  2152 \begin{frame}[fragile]
       
  2153   \frametitle{X-Y plot}
       
  2154   \begin{columns}
       
  2155     \column{0.5\textwidth}
       
  2156     \hspace*{-0.5in}
       
  2157     \includegraphics[height=2in, interpolate=true]{data/xyplot}
       
  2158     \column{0.45\textwidth}
       
  2159     \begin{block}{Example code}
       
  2160     \tiny
       
  2161 \begin{lstlisting}
       
  2162 t1 = arange(0.0, 5.0, 0.1)
       
  2163 t2 = arange(0.0, 5.0, 0.02)
       
  2164 t3 = arange(0.0, 2.0, 0.01)
       
  2165 subplot(211)
       
  2166 plot(t1, cos(2*pi*t1)*exp(-t1), 'bo', 
       
  2167      t2, cos(2*pi*t2)*exp(-t2), 'k')
       
  2168 grid(True)
       
  2169 title('A tale of 2 subplots')
       
  2170 ylabel('Damped')
       
  2171 subplot(212)
       
  2172 plot(t3, cos(2*pi*t3), 'r--')
       
  2173 grid(True)
       
  2174 xlabel('time (s)')
       
  2175 ylabel('Undamped')
       
  2176 \end{lstlisting}
       
  2177     \end{block}
       
  2178   \end{columns}
       
  2179 \end{frame}
       
  2180 
       
  2181 \begin{frame}[fragile] \frametitle{Errorbar}
       
  2182   \begin{columns}
       
  2183     \column{0.5\textwidth}
       
  2184     \hspace*{-0.5in}
       
  2185   \includegraphics[height=2in, interpolate=true]{data/errorbar}  
       
  2186     \column{0.45\textwidth}
       
  2187     \begin{block}{Example code}
       
  2188     \tiny
       
  2189 \begin{lstlisting}
       
  2190 t = arange(0.1, 4, 0.1)
       
  2191 s = exp(-t)
       
  2192 e = 0.1*abs(randn(len(s)))
       
  2193 f = 0.1*abs(randn(len(s)))
       
  2194 g = 2*e
       
  2195 h = 2*f
       
  2196 errorbar(t, s, [e,g], f, fmt='o')
       
  2197 xlabel('Distance (m)')
       
  2198 ylabel('Height (m)')
       
  2199 title('Mean and standard error '\
       
  2200       'as a function of distance')
       
  2201 \end{lstlisting}
       
  2202   \end{block}
       
  2203 \end{columns}
       
  2204 \end{frame}
       
  2205 
       
  2206 \begin{frame}[fragile] \frametitle{Semi-log and log-log plots}
       
  2207   \begin{columns}
       
  2208     \column{0.5\textwidth}
       
  2209     \hspace*{-0.5in}
       
  2210   \includegraphics[height=2in, interpolate=true]{data/log}  
       
  2211     \column{0.45\textwidth}
       
  2212     \begin{block}{Example code}
       
  2213     \tiny
       
  2214 \begin{lstlisting}
       
  2215 dt = 0.01
       
  2216 t = arange(dt, 20.0, dt)
       
  2217 subplot(311)
       
  2218 semilogy(t, exp(-t/5.0))
       
  2219 ylabel('semilogy')
       
  2220 grid(True)
       
  2221 subplot(312)
       
  2222 semilogx(t, sin(2*pi*t))
       
  2223 ylabel('semilogx')
       
  2224 grid(True)
       
  2225 # minor grid on too
       
  2226 gca().xaxis.grid(True, which='minor')  
       
  2227 subplot(313)
       
  2228 loglog(t, 20*exp(-t/10.0), basex=4)
       
  2229 grid(True)
       
  2230 ylabel('loglog base 4 on x')
       
  2231 \end{lstlisting}
       
  2232   \end{block}
       
  2233 \end{columns}
       
  2234 \end{frame}
       
  2235 
       
  2236 \begin{frame}[fragile] \frametitle{Histogram}
       
  2237   \begin{columns}
       
  2238     \column{0.5\textwidth}
       
  2239     \hspace*{-0.5in}
       
  2240   \includegraphics[height=2in, interpolate=true]{data/histogram}  
       
  2241     \column{0.45\textwidth}
       
  2242     \begin{block}{Example code}
       
  2243     \tiny
       
  2244 \begin{lstlisting}
       
  2245 mu, sigma = 100, 15
       
  2246 x = mu + sigma*randn(10000)
       
  2247 # the histogram of the data
       
  2248 n, bins, patches = hist(x, 100, normed=1)
       
  2249 # add a 'best fit' line
       
  2250 y = normpdf( bins, mu, sigma)
       
  2251 l = plot(bins, y, 'r--', linewidth=2)
       
  2252 xlim(40, 160)
       
  2253 xlabel('Smarts')
       
  2254 ylabel('P')
       
  2255 title(r'$\rm{IQ:}\/ \mu=100,\/ \sigma=15$')
       
  2256 \end{lstlisting}
       
  2257   \end{block}
       
  2258 \end{columns}
       
  2259 \end{frame}
       
  2260 
       
  2261 \begin{frame}[fragile] \frametitle{Bar charts}
       
  2262   \begin{columns}
       
  2263     \column{0.5\textwidth}
       
  2264     \hspace*{-0.5in}
       
  2265   \includegraphics[height=2in, interpolate=true]{data/barchart}  
       
  2266     \column{0.45\textwidth}
       
  2267     \begin{block}{Example code}
       
  2268     \tiny
       
  2269 \begin{lstlisting}
       
  2270 N = 5
       
  2271 menMeans = (20, 35, 30, 35, 27)
       
  2272 menStd =   ( 2,  3,  4,  1,  2)
       
  2273 # the x locations for the groups
       
  2274 ind = arange(N) 
       
  2275 # the width of the bars
       
  2276 width = 0.35       
       
  2277 p1 = bar(ind, menMeans, width, 
       
  2278          color='r', yerr=menStd)
       
  2279 womenMeans = (25, 32, 34, 20, 25)
       
  2280 womenStd =   ( 3,  5,  2,  3,  3)
       
  2281 p2 = bar(ind+width, womenMeans, width, 
       
  2282          color='y', yerr=womenStd)
       
  2283 ylabel('Scores')
       
  2284 title('Scores by group and gender')
       
  2285 xticks(ind+width, 
       
  2286        ('G1', 'G2', 'G3', 'G4', 'G5'))
       
  2287 xlim(-width,len(ind))
       
  2288 yticks(arange(0,41,10))
       
  2289 legend((p1[0], p2[0]), 
       
  2290        ('Men', 'Women'), shadow=True)
       
  2291 \end{lstlisting}
       
  2292   \end{block}
       
  2293 \end{columns}
       
  2294 \end{frame}
       
  2295 
       
  2296 \begin{frame}[fragile] \frametitle{Pie charts}
       
  2297   \begin{columns}
       
  2298     \column{0.5\textwidth}
       
  2299     \hspace*{-0.4in}
       
  2300   \includegraphics[height=2.0in, interpolate=true]{data/piechart}  
       
  2301     \column{0.45\textwidth}
       
  2302     \begin{block}{Example code}
       
  2303     \tiny
       
  2304 \begin{lstlisting}
       
  2305 # make a square figure and axes
       
  2306 figure(1, figsize=(8,8))
       
  2307 ax = axes([0.1, 0.1, 0.8, 0.8])
       
  2308 labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
       
  2309 fracs = [15,30,45, 10]
       
  2310 explode=(0, 0.05, 0, 0)
       
  2311 pie(fracs, explode=explode, labels=labels, 
       
  2312     autopct='%1.1f%%', shadow=True)
       
  2313 title('Raining Hogs and Dogs', 
       
  2314       bbox={'facecolor':'0.8', 'pad':5})
       
  2315 \end{lstlisting}
       
  2316   \end{block}
       
  2317 \end{columns}
       
  2318 \end{frame}
       
  2319 
       
  2320 \begin{frame}[fragile] \frametitle{Scatter plots}
       
  2321   \begin{columns}
       
  2322     \column{0.5\textwidth}
       
  2323     \hspace*{-0.4in}
       
  2324   \includegraphics[height=2in, interpolate=true]{data/scatter}  
       
  2325     \column{0.45\textwidth}
       
  2326     \begin{block}{Example code}
       
  2327     \tiny
       
  2328 \begin{lstlisting}
       
  2329 N = 30
       
  2330 x = 0.9*rand(N)
       
  2331 y = 0.9*rand(N)
       
  2332 # 0 to 10 point radiuses
       
  2333 area = pi*(10 * rand(N))**2 
       
  2334 volume = 400 + rand(N)*450
       
  2335 scatter(x,y,s=area, marker='o', c=volume, 
       
  2336         alpha=0.75)
       
  2337 xlabel(r'$\Delta_i$', size='x-large')
       
  2338 ylabel(r'$\Delta_{i+1}$', size='x-large')
       
  2339 title(r'Volume and percent change')
       
  2340 grid(True)
       
  2341 colorbar()
       
  2342 savefig('scatter')
       
  2343 \end{lstlisting}
       
  2344   \end{block}
       
  2345 \end{columns}
       
  2346 \end{frame}
       
  2347 
       
  2348 \begin{frame}[fragile] \frametitle{Polar}
       
  2349   \begin{columns}
       
  2350     \column{0.5\textwidth}
       
  2351     \hspace*{-0.5in}
       
  2352   \includegraphics[height=2in, interpolate=true]{data/polar}  
       
  2353     \column{0.45\textwidth}
       
  2354     \begin{block}{Example code}
       
  2355     \tiny
       
  2356 \begin{lstlisting}
       
  2357 figure(figsize=(8,8))
       
  2358 ax = axes([0.1, 0.1, 0.8, 0.8], polar=True, 
       
  2359           axisbg='#d5de9c')
       
  2360 r = arange(0,1,0.001)
       
  2361 theta = 2*2*pi*r
       
  2362 polar(theta, r, color='#ee8d18', lw=3)
       
  2363 # the radius of the grid labels
       
  2364 setp(ax.thetagridlabels, y=1.075) 
       
  2365 title(r"$\theta=4\pi r", fontsize=20)
       
  2366 \end{lstlisting}
       
  2367   \end{block}
       
  2368 \end{columns}
       
  2369 \end{frame}
       
  2370 
       
  2371 \begin{frame}[fragile] \frametitle{Contours}
       
  2372   \begin{columns}
       
  2373     \column{0.45\textwidth}
       
  2374     \hspace*{-0.5in}
       
  2375   \includegraphics[height=2in, interpolate=true]{data/contour}  
       
  2376     \column{0.525\textwidth}
       
  2377     \begin{block}{Example code}
       
  2378     \tiny
       
  2379 \begin{lstlisting}
       
  2380 x = arange(-3.0, 3.0, 0.025)
       
  2381 y = arange(-2.0, 2.0, 0.025)
       
  2382 X, Y = meshgrid(x, y)
       
  2383 Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
       
  2384 Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
       
  2385 # difference of Gaussians
       
  2386 Z = 10.0 * (Z2 - Z1)
       
  2387 im = imshow(Z, interpolation='bilinear', 
       
  2388             origin='lower',
       
  2389             cmap=cm.gray, extent=(-3,3,-2,2))
       
  2390 levels = arange(-1.2, 1.6, 0.2)
       
  2391 # label every second level
       
  2392 clabel(CS, levels[1::2],  inline=1,
       
  2393        fmt='%1.1f', fontsize=14)
       
  2394 CS = contour(Z, levels,
       
  2395              origin='lower',
       
  2396              linewidths=2,
       
  2397              extent=(-3,3,-2,2))
       
  2398 # make a colorbar for the contour lines
       
  2399 CB = colorbar(CS, shrink=0.8, extend='both')
       
  2400 title('Lines with colorbar')
       
  2401 hot(); flag()
       
  2402 \end{lstlisting}
       
  2403   \end{block}
       
  2404 \end{columns}
       
  2405 \end{frame}
       
  2406 
       
  2407 \begin{frame}[fragile] \frametitle{Velocity vectors}
       
  2408   \begin{columns}
       
  2409     \column{0.5\textwidth}
       
  2410     \hspace*{-0.5in}
       
  2411   \includegraphics[height=2in, interpolate=true]{data/quiver}  
       
  2412     \column{0.45\textwidth}
       
  2413     \begin{block}{Example code}
       
  2414     \tiny
       
  2415 \begin{lstlisting}
       
  2416 X,Y = meshgrid(arange(0,2*pi,.2),
       
  2417                arange(0,2*pi,.2) )
       
  2418 U = cos(X)
       
  2419 V = sin(Y)
       
  2420 Q = quiver(X[::3, ::3], Y[::3, ::3], 
       
  2421            U[::3, ::3], V[::3, ::3],
       
  2422            color='r', units='x', 
       
  2423            linewidths=(2,), 
       
  2424            edgecolors=('k'), 
       
  2425            headaxislength=5 )
       
  2426 qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s', 
       
  2427                fontproperties=
       
  2428                {'weight': 'bold'})
       
  2429 axis([-1, 7, -1, 7])
       
  2430 title('triangular head; scale '\
       
  2431       'with x view; black edges')
       
  2432 \end{lstlisting}
       
  2433   \end{block}
       
  2434 \end{columns}
       
  2435 \end{frame}
       
  2436 
       
  2437 \begin{frame}[fragile] \frametitle{Maps}
       
  2438   \includegraphics[height=2.5in, interpolate=true]{data/plotmap}  
       
  2439   \begin{center}
       
  2440     \tiny
       
  2441     For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py}
       
  2442   \end{center}
       
  2443 \end{frame}
       
  2444 
       
  2445 
       
  2446 \subsection{SciPy}
       
  2447 
       
  2448 \begin{frame}
       
  2449   \frametitle{Using \texttt{SciPy}}
       
  2450   \begin{itemize}
       
  2451   \item SciPy is Open Source software for mathematics, science, and
       
  2452     engineering
       
  2453   \item \typ{import scipy}
       
  2454   \item Built on NumPy
       
  2455   \item Provides modules for statistics, optimization, integration,
       
  2456     linear algebra, Fourier transforms, signal and image processing,
       
  2457     genetic algorithms, ODE solvers, special functions, and more
       
  2458   \item Used widely by scientists world over
       
  2459   \item Details are beyond the scope of this tutorial
       
  2460   \end{itemize}
       
  2461 \end{frame}
       
  2462 
       
  2463 \section{Standard library}
       
  2464 
       
  2465 \subsection{Quick Tour}
       
  2466 
       
  2467 \begin{frame}
       
  2468   \frametitle{Standard library}
       
  2469   \begin{itemize}
       
  2470   \item Very powerful
       
  2471   \item ``Batteries included''
       
  2472   \item Example standard modules taken from the tutorial
       
  2473     \begin{itemize}
       
  2474     \item Operating system interface: \typ{os}
       
  2475     \item System, Command line arguments: \typ{sys}
       
  2476     \item Regular expressions: \typ{re}
       
  2477     \item Math: \typ{math}, \typ{random}
       
  2478     \item Internet access: \typ{urllib2}, \typ{smtplib}
       
  2479     \item Data compression: \typ{zlib}, \typ{gzip}, \typ{bz2},
       
  2480       \typ{zipfile}, and \typ{tarfile}
       
  2481     \item Unit testing: \typ{doctest} and \typ{unittest}
       
  2482     \item And a whole lot more!
       
  2483     \end{itemize}
       
  2484   \item Check out the Python Library reference:
       
  2485     \url{http://docs.python.org/lib/lib.html}
       
  2486   \end{itemize}
       
  2487 \end{frame}
       
  2488 
       
  2489 \begin{frame}[fragile]
       
  2490   \frametitle{Stdlib: examples}
       
  2491 \begin{lstlisting}
       
  2492 >>> import os
       
  2493 >>> os.system('date')
       
  2494 Fri Jun 10 22:13:09 IST 2005
       
  2495 0
       
  2496 >>> os.getcwd()
       
  2497 '/home/prabhu'
       
  2498 >>> os.chdir('/tmp')
       
  2499 >>> import os
       
  2500 >>> dir(os)
       
  2501 <returns a list of all module functions>
       
  2502 >>> help(os)
       
  2503 <extensive manual page from module's docstrings>
       
  2504 \end{lstlisting}
       
  2505 \end{frame}
       
  2506 
       
  2507 \begin{frame}[fragile]
       
  2508   \frametitle{Stdlib: examples}
       
  2509 \begin{lstlisting}
       
  2510 >>> import sys
       
  2511 >>> # Print the list of command line args to Python
       
  2512 ... print sys.argv 
       
  2513 ['']
       
  2514 >>> import re # Regular expressions
       
  2515 >>> re.findall(r'\bf[a-z]*', 
       
  2516 ... 'which foot or hand fell fastest')
       
  2517 ['foot', 'fell', 'fastest']
       
  2518 >>> re.sub(r'(\b[a-z]+) \1', r'\1', 
       
  2519 ... 'cat in the the hat')
       
  2520 'cat in the hat'
       
  2521 \end{lstlisting}
       
  2522 \end{frame}
       
  2523 
       
  2524 \begin{frame}[fragile]
       
  2525   \frametitle{Stdlib: examples}
       
  2526 \begin{lstlisting}
       
  2527 >>> import math
       
  2528 >>> math.cos(math.pi / 4.0)
       
  2529 0.70710678118654757
       
  2530 >>> math.log(1024, 2)
       
  2531 10.0
       
  2532 >>> import random
       
  2533 >>> random.choice(['apple', 'pear', 'banana'])
       
  2534 'pear'
       
  2535 \end{lstlisting}
       
  2536 \end{frame}
       
  2537 
       
  2538 \begin{frame}[fragile]
       
  2539   \frametitle{Stdlib: examples}
       
  2540 \begin{lstlisting}
       
  2541 >>> import urllib2
       
  2542 >>> f = urllib2.urlopen('http://www.python.org/')
       
  2543 >>> print f.read(100)
       
  2544 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
       
  2545 <?xml-stylesheet href="./css/ht2html
       
  2546 \end{lstlisting}
       
  2547 \end{frame}
       
  2548 
       
  2549 \begin{frame}[fragile]
       
  2550   \frametitle{Stdlib: examples}
       
  2551 \begin{lstlisting}
       
  2552 >>> import zlib
       
  2553 >>> s = 'witch which has which witches wrist watch'
       
  2554 >>> len(s)
       
  2555 41
       
  2556 >>> t = zlib.compress(s)
       
  2557 >>> len(t)
       
  2558 37
       
  2559 >>> zlib.decompress(t)
       
  2560 'witch which has which witches wrist watch'
       
  2561 >>> zlib.crc32(t)
       
  2562 -1438085031
       
  2563 \end{lstlisting}
       
  2564 \end{frame}
       
  2565 
       
  2566 \begin{frame}
       
  2567   \frametitle{Summary}
       
  2568   \begin{itemize}
       
  2569   \item Introduced Python
       
  2570   \item Basic syntax
       
  2571   \item Basic types and data structures
       
  2572   \item Control flow
       
  2573   \item Functions
       
  2574   \item Modules
       
  2575   \item Exceptions
       
  2576   \item Classes
       
  2577   \item Standard library
       
  2578   \end{itemize}
       
  2579 \end{frame}
       
  2580 
       
  2581 \end{document}
       
  2582 
       
  2583 \subsection{Basic data structures}
       
  2584 \begin{frame}{Lists}
       
  2585   \begin{itemize}
       
  2586     \item \texttt{species = [ 'humans', 'orcs', 'elves', 'dwarves' ]}
       
  2587     \item \texttt{ ids = [ 107, 109, 124, 141, 142, 144 ]}
       
  2588     \item \texttt{ oneliners = [ 'I will be back', 'Do or do not! No try!!', 42 ] }
       
  2589   \end{itemize}
       
  2590 
       
  2591   \begin{block}{List operations}
       
  2592     ids + [ 100, 102 ]\\
       
  2593     species.append( 'unicorns')\\
       
  2594     print oneliners[ 1 ]\\
       
  2595     look up \alert{docs.python.org/tutorial/datastructures.html}
       
  2596   \end{block}
       
  2597 \end{frame}
       
  2598 \end{document}
       
  2599 \section{Python Tutorial}
       
  2600 \subsection{Preliminaries}
       
  2601 \begin{frame}
       
  2602   \frametitle{Using the interpreter}
       
  2603   \begin{itemize}
       
  2604   \item Starting up: \typ{python} or \typ{ipython}
       
  2605   \item Quitting: \typ{Control-D} or \typ{Control-Z} (on Win32)
       
  2606   \item Can use it like a calculator
       
  2607   \item Can execute one-liners via the \typ{-c} option:
       
  2608     \typ{python -c "print 'hello world'"}
       
  2609   \item Other options via \typ{python -h}
       
  2610   \end{itemize}
       
  2611 \end{frame}
       
  2612 
       
  2613 \begin{frame}
       
  2614   \frametitle{IPython}
       
  2615   \begin{itemize}
       
  2616   \item Recommended interpreter, IPython:
       
  2617     \url{http://ipython.scipy.org}
       
  2618   \item Better than the default Python shell
       
  2619   \item Supports tab completion by default
       
  2620   \item Easier object introspection
       
  2621   \item Shell access!
       
  2622   \item Command system to allow extending its own behavior
       
  2623   \item Supports history (across sessions) and logging
       
  2624   \item Can be embedded in your own Python code
       
  2625   \item Support for macros
       
  2626   \item A flexible framework for your own custom interpreter
       
  2627   \item Other miscellaneous conveniences
       
  2628   \item We'll get back to this later
       
  2629   \end{itemize}
       
  2630 \end{frame}
       
  2631 
       
  2632 \begin{frame}[fragile]
       
  2633   \frametitle{Basic IPython features}
       
  2634   \begin{itemize}
       
  2635   \item Startup: \verb+ipython [options] files+
       
  2636     \begin{itemize}
       
  2637     \item \verb+ipython [-wthread|-gthread|-qthread]+:
       
  2638       Threading modes to support wxPython, pyGTK and Qt
       
  2639     \item \verb+ipython -pylab+: Support for matplotlib
       
  2640     \end{itemize}
       
  2641   \item TAB completion:
       
  2642     \begin{itemize}
       
  2643     \item Type \verb+object_name.<TAB>+ to see list of options
       
  2644     \item Also completes on file and directory names
       
  2645     \end{itemize}
       
  2646   \item \verb+object?+ shows docstring/help for any Python object
       
  2647   \item \verb+object??+ presents more docs (and source if possible)
       
  2648   \item Debugging with \verb+%pdb+ magic: pops up pdb on errors
       
  2649   \item Access history (saved over earlier sessions also)
       
  2650     \begin{itemize}
       
  2651     \item Use \texttt{<UpArrow>}: move up history
       
  2652     \item Use \texttt{<Ctrl-r> string}: search history backwards
       
  2653     \item Use \texttt{Esc >}: get back to end of history
       
  2654     \end{itemize}
       
  2655   \item \verb+%run [options] file[.py]+ lets you run Python code
       
  2656   \end{itemize}
       
  2657 \end{frame}
       
  2658 % LocalWords:  BDFL Guido Rossum PSF Nokia OO Zope CMS RedHat SciPy MayaVi spam
       
  2659 % LocalWords:  IPython ipython stdin TypeError dict int elif PYTHONPATH IOError
       
  2660 % LocalWords:  namespace Namespaces SyntaxError ZeroDivisionError NameError str
       
  2661 % LocalWords:  ValueError subclassed def
       
  2662 
       
  2663 
       
  2664   \item Types are of two kinds: \alert{mutable} and \alert{immutable}
       
  2665   \item Immutable types: numbers, strings, \typ{None} and tuples
       
  2666   \item Immutables cannot be changed ``in-place''
       
  2667   \item Mutable types: lists, dictionaries, instances, etc.
       
  2668   \item Mutable objects can be ``changed''
       
  2669   \end{itemize}
       
  2670 
       
  2671 
       
  2672 \begin{frame}
       
  2673   \frametitle{Important!}
       
  2674   \begin{itemize}
       
  2675     \item Assignment to an object is by reference
       
  2676     \item Essentially, \alert{names are bound to objects}
       
  2677   \end{itemize}
       
  2678 \end{frame}
       
  2679 
       
  2680 
       
  2681 \end{document}
       
  2682 \begin{frame}[fragile]
       
  2683   \frametitle{Dictionaries}
       
  2684   \begin{itemize}
       
  2685   \item Associative arrays/mappings
       
  2686   \item Indexed by ``keys'' (keys must be immutable)
       
  2687   \item \typ{dict[key] = value}
       
  2688   \item \typ{keys()} returns all keys of the dict
       
  2689   \item \typ{values()} returns the values of the dict
       
  2690   \item \verb+has_key(key)+ returns if \typ{key} is in the dict
       
  2691   \end{itemize}
       
  2692 \end{frame}
       
  2693 
       
  2694 \begin{frame}[fragile]
       
  2695   \frametitle{Dictionaries: example}
       
  2696   \begin{lstlisting}
       
  2697 >>> tel = {'jack': 4098, 'sape': 4139}
       
  2698 >>> tel['guido'] = 4127
       
  2699 >>> tel
       
  2700 {'sape': 4139, 'guido': 4127, 'jack': 4098}
       
  2701 >>> tel['jack']
       
  2702 4098
       
  2703 >>> del tel['sape']
       
  2704 >>> tel['irv'] = 4127
       
  2705 >>> tel
       
  2706 {'guido': 4127, 'irv': 4127, 'jack': 4098}
       
  2707 >>> tel.keys()
       
  2708 ['guido', 'irv', 'jack']
       
  2709 >>> tel.has_key('guido')
       
  2710 True
       
  2711   \end{lstlisting}
       
  2712 \end{frame}
       
  2713 
       
  2714 \subsection{Control flow, functions}
       
  2715 
       
  2716 
       
  2717 
       
  2718 \begin{frame}[fragile]
       
  2719   \frametitle{\typ{If} example}
       
  2720   \begin{lstlisting}
       
  2721 >>> a = ['cat', 'window', 'defenestrate']
       
  2722 >>> if 'cat' in a:
       
  2723 ...    print "meaw"
       
  2724 ...
       
  2725 meaw
       
  2726 >>> pets = {'cat': 1, 'dog':2, 'croc': 10}
       
  2727 >>> if 'croc' in pets:
       
  2728 ...    print pets['croc']
       
  2729 ...
       
  2730 10
       
  2731   \end{lstlisting}
       
  2732 \end{frame}
       
  2733 
       
  2734 \begin{frame}[fragile]
       
  2735   \frametitle{\typ{for} example}
       
  2736   \begin{lstlisting}
       
  2737 >>> a = ['cat', 'window', 'defenestrate']
       
  2738 >>> for x in a:
       
  2739 ...     print x, len(x)
       
  2740 ...
       
  2741 cat 3
       
  2742 window 6
       
  2743 defenestrate 12
       
  2744 >>> knights = {'gallahad': 'the pure', 
       
  2745 ... 'robin': 'the brave'}
       
  2746 >>> for k, v in knights.iteritems():
       
  2747 ...     print k, v
       
  2748 ...
       
  2749 gallahad the pure
       
  2750 robin the brave
       
  2751 \end{lstlisting}
       
  2752 \end{frame}