%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tutorial slides on Python.
%
% Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in>
% Copyright (c) 2005-2009, Prabhu Ramachandran
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[14pt,compress]{beamer}
%\documentclass[draft]{beamer}
%\documentclass[compress,handout]{beamer}
%\usepackage{pgfpages} 
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

% Modified from: generic-ornate-15min-45min.de.tex
\mode<presentation>
{
  \usetheme{Warsaw}
  \useoutertheme{split}
  \setbeamercovered{transparent}
}

\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
%\usepackage{times}
\usepackage[T1]{fontenc}

% Taken from Fernando's slides.
\usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler}
\usepackage[scaled=.95]{helvet}

\definecolor{darkgreen}{rgb}{0,0.5,0}

\usepackage{listings}
\lstset{language=Python,
    basicstyle=\ttfamily,
    commentstyle=\color{red}\itshape,
  stringstyle=\color{darkgreen},
  showstringspaces=false,
  keywordstyle=\color{blue}\bfseries}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros
\setbeamercolor{emphbar}{bg=blue!20, fg=black}
\newcommand{\emphbar}[1]
{\begin{beamercolorbox}[rounded=true]{emphbar} 
      {#1}
 \end{beamercolorbox}
}
\newcounter{time}
\setcounter{time}{0}
\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}

\newcommand{\typ}[1]{\texttt{#1}}

\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }

%%% This is from Fernando's setup.
% \usepackage{color}
% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
% % Use and configure listings package for nicely formatted code
% \usepackage{listings}
% \lstset{
%    language=Python,
%    basicstyle=\small\ttfamily,
%    commentstyle=\ttfamily\color{blue},
%    stringstyle=\ttfamily\color{orange},
%    showstringspaces=false,
%    breaklines=true,
%    postbreak = \space\dots
% }


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
\title[Basic Python]{Python:\\A great programming toolkit}

\author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}

\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
\date[] {10, October 2009\\Day 1, Session 1}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
%\logo{\pgfuseimage{iitmlogo}}


%% Delete this, if you do not want the table of contents to pop up at
%% the beginning of each subsection:
\AtBeginSubsection[]
{
  \begin{frame}<beamer>
    \frametitle{Outline}
    \tableofcontents[currentsection,currentsubsection]
  \end{frame}
}


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command: 
%\beamerdefaultoverlayspecification{<+->}

%\includeonlyframes{current,current1,current2,current3,current4,current5,current6}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DOCUMENT STARTS
\begin{document}

\begin{frame}
  \titlepage
\end{frame}
\begin{frame}
  {Acknowledgements}
  \begin{center}
  This program is conducted by\\
  IIT, Bombay\\
  through CDEEP\\as part of  the open source initiatives\\
  under the aegis of\\
  \alert{National Mission on Education through ICT,} \\
  Ministry of HRD.
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Outline}
  \tableofcontents
  % You might wish to add the option [pausesections]
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TODO
%
%  * Add slide on Python packages (modules)
%  * Add slides on reference counting.

\section{Agenda}
\begin{frame}{About the Workshop}
  \begin{description}
	\item[Day 1, Session 1] Sat 09:30--11:00
	\item[Day 1, Session 2] Sat 11:15--12:45
	\item[Day 1, Session 3] Sat 13:45--15:15
	\item[Day 1, Session 4] Sat 15:30--17:00
        \item[Day 2, Quiz] Sun 09:00--09:30
        \item[Day 2, Session 1] Sun 09:30--11:00
	\item[Day 2, Session 2] Sun 11:15--12:45
	\item[Day 2, Session 3] Sun 13:45--15:15
	\item[Day 2, Session 4] Sun 15:30--17:00
  \end{description}
\end{frame}

\begin{frame}{About the Workshop}
  \begin{block}{Intended Audience}
  \begin{itemize}
       \item Aimed at Engg., Mathematics and Science teachers.
       \item Interested students from similar streams.
  \end{itemize}
  \end{block}  

  \begin{block}{Goal}
	Successful participants will be able to use python as their scripting and problem solving language. 
  \end{block}
\end{frame}

\begin{frame}{Checklist}
        \begin {block}{Live Python}
          Have you booted using the Live Python DVD?
        \end {block}
	\begin{block}{python}
          Type python at the command line. Do you see version 2.5 or later?
        \end{block}
        \begin{block}{IPython}
          Type ipython at the command line. Is it available?
        \end{block}
        \begin{block}{Editor}
          We recommend scite. 
        \end{block}
\end{frame}

\section{Overview}
\begin{frame}
  \frametitle{Introduction}
  \begin{itemize}
  \item Creator and BDFL: Guido van Rossum
  \item December 1989
  \item ``Python'' as in \emph {Monty Python's Flying Circus}
  \item 2.6.x
  \item PSF license (like BSD: no strings attached)
  \item Highly cross platform
  \item Nokia series 60!
  \item \alert{Philosophy:} Simple and complete by design
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Resources}
  \begin{itemize}
  \item Part of many GNU/Linux distributions
  \item Web: \url{http://www.python.org}
  \item Doc: \url{http://www.python.org/doc}
  \item Free Tutorials:
    \begin{itemize}
    \item Official Python tutorial: \url{http://docs.python.org/tut/tut.html}
    \item Byte of Python: \url{http://www.byteofpython.info/}
    \item Dive into Python: \url{http://diveintopython.org/}
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Why Python?}
  \begin{itemize}
  \item Readable and easy to use
  \item High level, interpreted, modular, OO
  \item Much faster development cycle
  \item Powerful interactive environment
  \item Rapid application development
  \item Rich standard library and modules
  \item Interfaces well with C++, C and FORTRAN
  \item \alert{More than a math package $\Rightarrow$ some extra work compared to math packages}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Use cases}
  \begin{itemize}
  \item NASA: Space Shuttle Mission Design
  \item AstraZeneca: Collaborative Drug Discovery
  \item ForecastWatch.com: Helps Meteorologists
  \item Industrial Light \& Magic: Runs on Python
  \item Zope: Commercial grade Toolkit
  \item Plone: Professional high feature CMS
  \item RedHat: install scripts, sys-admin tools
  \item Django: A great web application framework
  \item Google: A strong python shop
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{To sum up, python is\ldots}
  \begin{itemize}
  \item dynamically typed, interpreted $\rightarrow$ rapid testing/prototyping
  \item powerful, very high level
  \item has full introspection 
  \item Did we mention powerful?
  \end{itemize}
  \begin{block}{But \ldots}
    may be wanting in performance. specialised resources such as SWIG, \alert{Cython} are available 
  \end{block}
  \inctime{15}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TIME: 10 m, running 10m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Python}

\subsection{Getting Started}

\begin{frame}[fragile]{At the prompt, type the following}
   \begin{lstlisting}
>>> print 'Hello Python' 
>>> print 3124 * 126789
>>> 1786 % 12
>>> 3124 * 126789
>>> a = 3124 * 126789
>>> big = 12345678901234567890 ** 3
>>> verybig = big * big * big * big 
>>> 12345**6, 12345**67, 12345**678
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]{At the prompt, type the following}
   \begin{lstlisting}
>>> s = 'Hello '
>>> p = 'World'
>>> s + p 
>>> s * 12 
>>> s * s
>>> s + p * 12, (s + p)* 12
>>> s * 12 + p * 12
>>> 12 * s 
    \end{lstlisting}
\end{frame}

\begin{frame}[fragile]{At the prompt, type the following}
  \begin{lstlisting}
>>> 17/2
>>> 17/2.0
>>> 17.0/2
>>> 17.0/8.5
>>> int(17/2.0)
>>> float(17/2)
>>> str(17/2.0)
>>> round( 7.5 )
  \end{lstlisting}
  \begin{block}{Mini exercise}
	Round a float to the nearest integer, using \texttt{int()}?
  \end{block}
\end{frame}

\begin{frame}{Midi exercises}
  \begin{center}
    \begin{itemize}
      \item What does this do?
      \item \texttt{round(amount * 10) /10.0 }
    \end{itemize}
  \end{center}
\end{frame}

\begin{frame}{More exercises?}
  \begin{center}
    \begin{block}{Round sums}
      How to round a number to the nearest  5 paise?\\
      \begin{description}
        \item[Remember] 17.23 $\rightarrow$ 17.25,\\ while 17.22 $\rightarrow$ 17.20\\
      \end{description}
      How to round a number to the nearest 20 paise?
    \end{block}
  \end{center}
\end{frame}

\begin{frame}[fragile] {A question of good style}
  \begin{lstlisting}
    amount = 12.68
    denom = 0.05
    nCoins = round(amount/denom)
    rAmount = nCoins * denom
  \end{lstlisting}
  \pause
  \begin{block}{Style Rule \#1}
    Naming is 80\% of programming
  \end{block}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Odds and ends}
  \begin{itemize}
    \item Case sensitive
    \item Dynamically typed $\Rightarrow$ need not specify a type
      \begin{lstlisting}
a = 1
a = 1.1
a = "Now I am a string!"
      \end{lstlisting}
    \item Comments:
      \begin{lstlisting}
a = 1  # In-line comments
# Comment in a line to itself.
a = "# This is not a comment!"
      \end{lstlisting}
  \end{itemize}
  \inctime{15}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TIME: 10 m, running 20m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Data types}
\begin{frame}
  \frametitle{Basic types}
  \begin{itemize}
    \item Numbers: float, int, long, complex
    \item Strings
    \item Boolean
  \end{itemize}
  \begin{block}{Also to be discussed later}
    tuples, lists, dictionaries, functions, objects\ldots
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Numbers}
  \vspace*{-0.25in}
  \begin{lstlisting}
>>> a = 1 # Int.
>>> l = 1000000L # Long
>>> e = 1.01325e5 # float
>>> f = 3.14159 # float
>>> c = 1+1j # Complex!
>>> print f*c/a
(3.14159+3.14159j)
>>> print c.real, c.imag
1.0 1.0
>>> abs(c)
1.4142135623730951
>>> abs( 8 - 9.5 )
1.5
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Boolean}
  \begin{lstlisting}
>>> t = True
>>> f = not t
False
>>> f or t
True
>>> f and t
False
  \end{lstlisting}
  \begin{block}{Try:}
  NOT True\\
  not TRUE
  \end{block}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TIME: 10 m, running 30m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
  \frametitle{Relational and logical operators}
  \begin{lstlisting}
>>> a, b, c = -1, 0, 1
>>> a == b
False
>>> a <= b 
True
>>> a + b != c
True
>>> a < b < c
True
>>> c >= a + b
True
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Strings}
  \begin{lstlisting}
s = 'this is a string'
s = 'This one has "quotes" inside!'
s = "I have 'single-quotes' inside!"
l = "A string spanning many lines\
one more line\
yet another"
t = """A triple quoted string does
not need to be escaped at the end and 
"can have nested quotes" etc."""
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{More Strings}
  \vspace*{-0.2in}
  \begin{lstlisting}
>>> w = "hello"    
>>> print w[0] + w[2] + w[-1]
hlo
>>> len(w) # guess what
5
>>> s = u'Unicode strings!'
>>> # Raw strings (note the leading 'r')
... r_s = r'A string $\alpha \nu$'
  \end{lstlisting}
\pause
  \begin{lstlisting}
>>> w[0] = 'H' # Can't do that!
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: object does not support item assignment
  \end{lstlisting}
\end{frame}

\begin{frame}
  \frametitle{Let us switch to IPython}
  Why?
  \begin{block}
    {Better help (and a lot more)}
    Tab completion\\
    ?\\
    .?\\
    object.function?
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{More on strings}
  \begin{lstlisting}
In [1]: a = 'hello world'
In [2]: a.startswith('hell')
Out[2]: True
In [3]: a.endswith('ld')
Out[3]: True
In [4]: a.upper()
Out[4]: 'HELLO WORLD'
In [5]: a.upper().lower()
Out[5]: 'hello world'
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Still with strings}
  \begin{lstlisting}
In [6]: a.split()
Out[6]: ['hello', 'world']
In [7]: ''.join(['a', 'b', 'c'])
Out[7]: 'abc'
In [8] 'd' in ''.join( 'a', 'b', 'c')
Out[8]: False
  \end{lstlisting}
  \begin{block}{Try:}
    \texttt{a.split( 'o' )}\\
    \texttt{'x'.join( a.split( 'o' ) )}
  \end{block}
\end{frame}

\begin{frame}[fragile]{Surprise! strings!!}
  \begin{lstlisting}
In [11]: x, y = 1, 1.2
In [12]: 'x is %s, y is %s' %(x, y)
Out[12]: 'x is 1, y is 1.234'
  \end{lstlisting}
  \begin{block}{Try:}
    \texttt{'x is \%d, y is \%f' \%(x, y) }\\
    \texttt{'x is \%3d, y is \%4.2f' \%(x, y) }
  \end{block}
  \small
\url{docs.python.org/lib/typesseq-strings.html}\\
\end{frame}

\begin{frame}
  {Interlude}
  \begin{block}
    {A classic problem}
    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!
  \end{block}
  \inctime{30}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TIME: 25 m+ Interlude break 5 mins, running 60m 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Control flow}
\begin{frame}
  \frametitle{Control flow constructs}  
  \begin{itemize}
  \item \kwrd{if/elif/else}: branching
  \item \kwrd{while}: looping
  \item \kwrd{for}: iterating 
  \item \kwrd{break, continue}: modify loop 
  \item \kwrd{pass}: syntactic filler
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Basic conditional flow}
  \begin{lstlisting}
In [21]: a = 7
In [22]: b = 8
In [23]: if a > b:
   ....:    print 'Hello'
   ....: else:
   ....:     print 'World'
   ....:
   ....:
World
  \end{lstlisting}
  Let us switch to creating a file
\end{frame}

\begin{frame}
  {Creating python files}
  \begin{itemize}
    \item aka scripts
    \item use your editor
    \item Note that white space is the way to specify blocks!
    \item extension \typ{.py}
    \item run with \texttt{python hello.py} at the command line
    \item in IPython\ldots
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\typ{If...elif...else} example}
\begin{lstlisting}
x = int(raw_input("Enter an integer:"))
if x < 0:
     print 'Be positive!'
elif x == 0:
     print 'Zero'
elif x == 1:
     print 'Single'
else:
     print 'More'
\end{lstlisting}
\end{frame}

\begin{frame}{Simple IO}
  \begin{block}
    {Console Input}
    \texttt{raw\_input()} waits for user input.\\Prompt string is optional.\\
    All keystrokes are Strings!\\\texttt{int()} converts string to int.
  \end{block}
  \begin{block}
    {Console output}
    \texttt{print} is straight forward. Note the distinction between \texttt{print x} and \texttt{print x,}
  \end{block}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Basic looping}
  \begin{lstlisting}
# Fibonacci series:
# the sum of two elements
# defines the next
a, b = 0, 1
while b < 10:
    print b,
    a, b = b, a + b
 
\end{lstlisting}
\typ{1 1 2 3 5 8}\\  
\alert{Recall it is easy to write infinite loops with \kwrd{while}}
  \inctime{20}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TIME: 20 m, running 80m 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Session Summary}
\begin{frame}{So what have we learnt so far?}
  \begin{itemize}
    \item The interactive interpreter
    \item Basic Data Types-Numbers
    \item \typ{if/elif/else, while}
    \item Simple IO
    \item Creating and running a Python script
  \end{itemize}
\end{frame}
\end{document}
