day1/Session-1.tex~
changeset 53 fc74987a475d
parent 52 fccb3551c7d5
parent 51 d3a8a9556a90
child 55 09c73bf81987
child 74 a476c09dcf24
equal deleted inserted replaced
52:fccb3551c7d5 53:fc74987a475d
     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[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}
       
    79 
       
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
       
    81 \date[] {10, October 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 	Successful participants will be able to use python as their scripting and problem solving language. 
       
   154   \end{block}
       
   155   \begin{block}{Workshop Audience}
       
   156         Aimed at Engg., Mathematics and Science teachers  but should serve a similar purpose for others. 
       
   157   \end{block}
       
   158   \begin{block}{Focus of the workshop}
       
   159         Focus on basic numerics and plotting
       
   160   \end{block}
       
   161 \end{frame}
       
   162 
       
   163 \begin{frame}{Checklist}
       
   164 	\begin{block}{python}
       
   165           Type python at the command line. Do you see version 2.5 or later?
       
   166         \end{block}
       
   167         \begin{block}{IPython}
       
   168           Is IPython available?
       
   169         \end{block}
       
   170         \begin{block}{Editor}
       
   171           Which editor? scite, vim, emacs, \ldots
       
   172         \end{block}
       
   173 \end{frame}
       
   174 
       
   175 \section{Overview}
       
   176 \begin{frame}{Session 1}
       
   177   \begin{itemize}
       
   178 	\item Introduction and motivation
       
   179 	\item Using the interpreter(s)
       
   180 	\item Basic data types: int, float, string
       
   181 	\item Basic data structures: list
       
   182 	\item Basic console IO: \texttt{raw\_input(), print}
       
   183     \item Basic control flow: \texttt{if, while}
       
   184 	\item Problem set 1
       
   185     \item Functions $\rightarrow$ Problem set 2
       
   186     \item lists, \texttt{for}  $\rightarrow$ Problem set 3
       
   187     \item IO, Modules $\rightarrow$ Problem sets 4,5, \ldots
       
   188   \end{itemize}
       
   189 \end{frame}
       
   190 
       
   191 \begin{frame}
       
   192   \frametitle{Introduction}
       
   193   \begin{itemize}
       
   194   \item Creator and BDFL: Guido van Rossum
       
   195   \item December 1989
       
   196   \item ``Python'' as in Monty Python's Flying Circus
       
   197   \item 2.6.x
       
   198   \item PSF license (like BSD: no strings attached)
       
   199   \item Highly cross platform
       
   200   \item Nokia series 60!
       
   201   \item \alert{Philosophy:} Simple and complete by design
       
   202   \end{itemize}
       
   203 \end{frame}
       
   204 
       
   205 \begin{frame}
       
   206   \frametitle{Resources}
       
   207   \begin{itemize}
       
   208   \item Part of many GNU/Linux distributions
       
   209   \item Web: \url{http://www.python.org}
       
   210   \item Doc: \url{http://www.python.org/doc}
       
   211   \item Free Tutorials:
       
   212     \begin{itemize}
       
   213     \item Official Python tutorial: \url{http://docs.python.org/tut/tut.html}
       
   214     \item Byte of Python: \url{http://www.byteofpython.info/}
       
   215     \item Dive into Python: \url{http://diveintopython.org/}
       
   216     \end{itemize}
       
   217   \end{itemize}
       
   218 \end{frame}
       
   219 
       
   220 \begin{frame}
       
   221   \frametitle{Why Python?}
       
   222   \begin{itemize}
       
   223   \item Readable and easy to use
       
   224   \item High level, interpreted, modular, OO
       
   225   \item Much faster development cycle
       
   226   \item Powerful interactive environment
       
   227   \item Rapid application development
       
   228   \item Rich standard library and modules
       
   229   \item Interfaces well with C++, C and FORTRAN
       
   230   \item \alert{More than a math package $\Rightarrow$ some extra work compared to math packages}
       
   231   \end{itemize}
       
   232 \end{frame}
       
   233 
       
   234 \begin{frame}
       
   235   \frametitle{Use cases}
       
   236   \begin{itemize}
       
   237   \item NASA: Space Shuttle Mission Design
       
   238   \item AstraZeneca: Collaborative Drug Discovery
       
   239   \item ForecastWatch.com: Helps Meteorologists
       
   240   \item Industrial Light \& Magic: Runs on Python
       
   241   \item Zope: Commercial grade Toolkit
       
   242   \item Plone: Professional high feature CMS
       
   243   \item RedHat: install scripts, sys-admin tools
       
   244   \item Django: A great web application framework
       
   245   \item Google: A strong python shop
       
   246   \end{itemize}
       
   247 \end{frame}
       
   248 
       
   249 \begin{frame}
       
   250   \frametitle{To sum up, python is\ldots}
       
   251   \begin{itemize}
       
   252   \item dynamically typed, interpreted $\rightarrow$ rapid testing/prototyping
       
   253   \item powerful, very high level
       
   254   \item has full introspection 
       
   255   \item Did we mention powerful?
       
   256   \end{itemize}
       
   257   \begin{block}{But \ldots}
       
   258     may be wanting in performance. specialised resources such as SWIG, \alert{Cython} are available 
       
   259   \end{block}
       
   260   \inctime{15}
       
   261 \end{frame}
       
   262 
       
   263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   264 % TIME: 10 m, running 10m
       
   265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   266 
       
   267 \section{Python}
       
   268 
       
   269 \subsection{Getting Started}
       
   270 
       
   271 \begin{frame}[fragile]{At the prompt, type the following}
       
   272    \begin{lstlisting}
       
   273 >>> print 'Hello Python' 
       
   274 >>> print 3124 * 126789
       
   275 >>> 1786 % 12
       
   276 >>> 3124 * 126789
       
   277 >>> a = 3124 * 126789
       
   278 >>> big = 12345678901234567890 ** 3
       
   279 >>> verybig = big * big * big * big 
       
   280 >>> 12345**6, 12345**67, 12345**678
       
   281   \end{lstlisting}
       
   282 \end{frame}
       
   283 
       
   284 \begin{frame}[fragile]{At the prompt, type the following}
       
   285    \begin{lstlisting}
       
   286 >>> s = 'Hello '
       
   287 >>> p = 'World'
       
   288 >>> s + p 
       
   289 >>> s * 12 
       
   290 >>> s * s
       
   291 >>> s + p * 12, (s + p)* 12
       
   292 >>> s * 12 + p * 12
       
   293 >>> 12 * s 
       
   294     \end{lstlisting}
       
   295 \end{frame}
       
   296 
       
   297 \begin{frame}[fragile]{At the prompt, type the following}
       
   298   \begin{lstlisting}
       
   299 >>> 17/2
       
   300 >>> 17/2.0
       
   301 >>> 17.0/2
       
   302 >>> 17.0/8.5
       
   303 >>> int(17/2.0)
       
   304 >>> float(17/2)
       
   305 >>> str(17/2.0)
       
   306 >>> round( 7.5 )
       
   307   \end{lstlisting}
       
   308   \begin{block}{Mini exercise}
       
   309 	Round a float to the nearest integer, using \texttt{int()}?
       
   310   \end{block}
       
   311 \end{frame}
       
   312 
       
   313 \begin{frame}{Midi exercises}
       
   314   \begin{center}
       
   315     \begin{itemize}
       
   316       \item What does this do?
       
   317       \item \texttt{round(amount * 10) /10.0 }
       
   318     \end{itemize}
       
   319   \end{center}
       
   320 \end{frame}
       
   321 
       
   322 \begin{frame}{More exercises?}
       
   323   \begin{center}
       
   324     \begin{block}{Round sums}
       
   325       How to round a number to the nearest  5 paise?\\
       
   326       \begin{description}
       
   327         \item[Remember] 17.23 $\rightarrow$ 17.25,\\ while 17.22 $\rightarrow$ 17.20\\
       
   328       \end{description}
       
   329       How to round a number to the nearest 20 paise?
       
   330     \end{block}
       
   331   \end{center}
       
   332 \end{frame}
       
   333 
       
   334 \begin{frame}[fragile] {A question of good style}
       
   335   \begin{lstlisting}
       
   336     amount = 12.68
       
   337     denom = 0.05
       
   338     nCoins = round(amount/denom)
       
   339     rAmount = nCoins * denom
       
   340   \end{lstlisting}
       
   341   \pause
       
   342   \begin{block}{Style Rule \#1}
       
   343     Naming is 80\% of programming
       
   344   \end{block}
       
   345 \end{frame}
       
   346 
       
   347 
       
   348 \begin{frame}[fragile]
       
   349   \frametitle{Odds and ends}
       
   350   \begin{itemize}
       
   351     \item Case sensitive
       
   352     \item Dynamically typed $\Rightarrow$ need not specify a type
       
   353       \begin{lstlisting}
       
   354 a = 1
       
   355 a = 1.1
       
   356 a = "Now I am a string!"
       
   357       \end{lstlisting}
       
   358     \item Comments:
       
   359       \begin{lstlisting}
       
   360 a = 1  # In-line comments
       
   361 # Comment in a line to itself.
       
   362 a = "# This is not a comment!"
       
   363       \end{lstlisting}
       
   364   \end{itemize}
       
   365   \inctime{15}
       
   366 \end{frame}
       
   367 
       
   368 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   369 % TIME: 10 m, running 20m
       
   370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   371 
       
   372 \subsection{Data types}
       
   373 \begin{frame}
       
   374   \frametitle{Basic types}
       
   375   \begin{itemize}
       
   376     \item Numbers: float, int, long, complex
       
   377     \item Strings
       
   378     \item Boolean
       
   379   \end{itemize}
       
   380   \begin{block}{Also to be discussed later}
       
   381     tuples, lists, dictionaries, functions, objects\ldots
       
   382   \end{block}
       
   383 \end{frame}
       
   384 
       
   385 \begin{frame}[fragile]
       
   386   \frametitle{Numbers}
       
   387   \vspace*{-0.25in}
       
   388   \begin{lstlisting}
       
   389 >>> a = 1 # Int.
       
   390 >>> l = 1000000L # Long
       
   391 >>> e = 1.01325e5 # float
       
   392 >>> f = 3.14159 # float
       
   393 >>> c = 1+1j # Complex!
       
   394 >>> print f*c/a
       
   395 (3.14159+3.14159j)
       
   396 >>> print c.real, c.imag
       
   397 1.0 1.0
       
   398 >>> abs(c)
       
   399 1.4142135623730951
       
   400 >>> abs( 8 - 9.5 )
       
   401 1.5
       
   402   \end{lstlisting}
       
   403 \end{frame}
       
   404 
       
   405 \begin{frame}[fragile]
       
   406   \frametitle{Boolean}
       
   407   \begin{lstlisting}
       
   408 >>> t = True
       
   409 >>> f = not t
       
   410 False
       
   411 >>> f or t
       
   412 True
       
   413 >>> f and t
       
   414 False
       
   415   \end{lstlisting}
       
   416   \begin{block}{Try:}
       
   417   NOT True\\
       
   418   not TRUE
       
   419   \end{block}
       
   420 \end{frame}
       
   421 
       
   422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   423 % TIME: 10 m, running 30m
       
   424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   425 
       
   426 \begin{frame}[fragile]
       
   427   \frametitle{Relational and logical operators}
       
   428   \begin{lstlisting}
       
   429 >>> a, b, c = -1, 0, 1
       
   430 >>> a == b
       
   431 False
       
   432 >>> a <= b 
       
   433 True
       
   434 >>> a + b != c
       
   435 True
       
   436 >>> a < b < c
       
   437 True
       
   438 >>> c >= a + b
       
   439 True
       
   440   \end{lstlisting}
       
   441 \end{frame}
       
   442 
       
   443 \begin{frame}[fragile]
       
   444   \frametitle{Strings}
       
   445   \begin{lstlisting}
       
   446 s = 'this is a string'
       
   447 s = 'This one has "quotes" inside!'
       
   448 s = "I have 'single-quotes' inside!"
       
   449 l = "A string spanning many lines\
       
   450 one more line\
       
   451 yet another"
       
   452 t = """A triple quoted string does
       
   453 not need to be escaped at the end and 
       
   454 "can have nested quotes" etc."""
       
   455   \end{lstlisting}
       
   456 \end{frame}
       
   457 
       
   458 \begin{frame}[fragile]
       
   459   \frametitle{More Strings}
       
   460   \vspace*{-0.2in}
       
   461   \begin{lstlisting}
       
   462 >>> w = "hello"    
       
   463 >>> print w[0] + w[2] + w[-1]
       
   464 hlo
       
   465 >>> len(w) # guess what
       
   466 5
       
   467 >>> s = u'Unicode strings!'
       
   468 >>> # Raw strings (note the leading 'r')
       
   469 ... r_s = r'A string $\alpha \nu$'
       
   470   \end{lstlisting}
       
   471 \pause
       
   472   \begin{lstlisting}
       
   473 >>> w[0] = 'H' # Can't do that!
       
   474 Traceback (most recent call last):
       
   475   File "<stdin>", line 1, in ?
       
   476 TypeError: object does not support item assignment
       
   477   \end{lstlisting}
       
   478 \end{frame}
       
   479 
       
   480 \begin{frame}
       
   481   \frametitle{Let us switch to IPython}
       
   482   Why?
       
   483   \begin{block}
       
   484     {Better help (and a lot more)}
       
   485     Tab completion\\
       
   486     ?\\
       
   487     .?\\
       
   488     object.function?
       
   489   \end{block}
       
   490 \end{frame}
       
   491 
       
   492 \begin{frame}[fragile]
       
   493   \frametitle{More on strings}
       
   494   \begin{lstlisting}
       
   495 In [1]: a = 'hello world'
       
   496 In [2]: a.startswith('hell')
       
   497 Out[2]: True
       
   498 In [3]: a.endswith('ld')
       
   499 Out[3]: True
       
   500 In [4]: a.upper()
       
   501 Out[4]: 'HELLO WORLD'
       
   502 In [5]: a.upper().lower()
       
   503 Out[5]: 'hello world'
       
   504   \end{lstlisting}
       
   505 \end{frame}
       
   506 
       
   507 \begin{frame}[fragile]{Still with strings}
       
   508   \begin{lstlisting}
       
   509 In [6]: a.split()
       
   510 Out[6]: ['hello', 'world']
       
   511 In [7]: ''.join(['a', 'b', 'c'])
       
   512 Out[7]: 'abc'
       
   513 In [8] 'd' in ''.join( 'a', 'b', 'c')
       
   514 Out[8]: False
       
   515   \end{lstlisting}
       
   516   \begin{block}{Try:}
       
   517     \texttt{a.split( 'o' )}\\
       
   518     \texttt{'x'.join( a.split( 'o' ) )}
       
   519   \end{block}
       
   520 \end{frame}
       
   521 
       
   522 \begin{frame}[fragile]{Surprise! strings!!}
       
   523   \begin{lstlisting}
       
   524 In [11]: x, y = 1, 1.2
       
   525 In [12]: 'x is %s, y is %s' %(x, y)
       
   526 Out[12]: 'x is 1, y is 1.234'
       
   527   \end{lstlisting}
       
   528   \begin{block}{Try:}
       
   529     \texttt{'x is \%d, y is \%f' \%(x, y) }\\
       
   530     \texttt{'x is \%3d, y is \%4.2f' \%(x, y) }
       
   531   \end{block}
       
   532   \small
       
   533 \url{docs.python.org/lib/typesseq-strings.html}\\
       
   534 \end{frame}
       
   535 
       
   536 \begin{frame}
       
   537   {Interlude}
       
   538   \begin{block}
       
   539     {A classic problem}
       
   540     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!
       
   541   \end{block}
       
   542   \inctime{30}
       
   543 \end{frame}
       
   544 
       
   545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   546 % TIME: 25 m+ Interlude break 5 mins, running 60m 
       
   547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   548 
       
   549 \subsection{Control flow}
       
   550 \begin{frame}
       
   551   \frametitle{Control flow constructs}  
       
   552   \begin{itemize}
       
   553   \item \kwrd{if/elif/else}: branching
       
   554   \item \kwrd{while}: looping
       
   555   \item \kwrd{for}: iterating 
       
   556   \item \kwrd{break, continue}: modify loop 
       
   557   \item \kwrd{pass}: syntactic filler
       
   558   \end{itemize}
       
   559 \end{frame}
       
   560 
       
   561 \begin{frame}[fragile]
       
   562   \frametitle{Basic conditional flow}
       
   563   \begin{lstlisting}
       
   564 In [21]: a = 7
       
   565 In [22]: b = 8
       
   566 In [23]: if a > b:
       
   567    ....:    print 'Hello'
       
   568    ....: else:
       
   569    ....:     print 'World'
       
   570    ....:
       
   571    ....:
       
   572 World
       
   573   \end{lstlisting}
       
   574   Let us switch to creating a file
       
   575 \end{frame}
       
   576 
       
   577 \begin{frame}
       
   578   {Creating python files}
       
   579   \begin{itemize}
       
   580     \item aka scripts
       
   581     \item use your editor
       
   582     \item Note that white space is the way to specify blocks!
       
   583     \item extension \typ{.py}
       
   584     \item run with \texttt{python hello.py} at the command line
       
   585     \item in IPython\ldots
       
   586   \end{itemize}
       
   587 \end{frame}
       
   588 
       
   589 \begin{frame}[fragile]
       
   590   \frametitle{\typ{If...elif...else} example}
       
   591 \begin{lstlisting}
       
   592 x = int(raw_input("Enter an integer:"))
       
   593 if x < 0:
       
   594      print 'Be positive!'
       
   595 elif x == 0:
       
   596      print 'Zero'
       
   597 elif x == 1:
       
   598      print 'Single'
       
   599 else:
       
   600      print 'More'
       
   601 \end{lstlisting}
       
   602 \end{frame}
       
   603 
       
   604 \begin{frame}{Simple IO}
       
   605   \begin{block}
       
   606     {Console Input}
       
   607     \texttt{raw\_input()} waits for user input.\\Prompt string is optional.\\
       
   608     All keystrokes are Strings!\\\texttt{int()} converts string to int.
       
   609   \end{block}
       
   610   \begin{block}
       
   611     {Console output}
       
   612     \texttt{print} is straight forward. Major point to remember is the distinction between \texttt{print x} and \texttt{print x,}
       
   613   \end{block}
       
   614 \end{frame}
       
   615 
       
   616 \begin{frame}[fragile]
       
   617   \frametitle{Basic looping}
       
   618   \begin{lstlisting}
       
   619 # Fibonacci series:
       
   620 # the sum of two elements
       
   621 # defines the next
       
   622 a, b = 0, 1
       
   623 while b < 10:
       
   624     print b,
       
   625     a, b = b, a + b
       
   626  
       
   627 \end{lstlisting}
       
   628 \typ{1 1 2 3 5 8}\\  
       
   629 \alert{Recall it is easy to write infinite loops with \kwrd{while}}
       
   630   \inctime{20}
       
   631 \end{frame}
       
   632 
       
   633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   634 % TIME: 20 m, running 80m 
       
   635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   636 \end{document}