day2/PythonMachinery.tex
author Shantanu <shantanu@fossee.in>
Mon, 05 Oct 2009 21:58:16 +0530
changeset 23 14c817585247
parent 0 9243d75024cc
permissions -rwxr-xr-x
Added Exercise for testing in Session3.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     2
% Tutorial slides on Python.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     3
%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     4
% Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in>
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     5
% Copyright (c) 2005-2008, Prabhu Ramachandran
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     7
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     8
\documentclass[14pt,compress]{beamer}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
%\documentclass[draft]{beamer}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    10
%\documentclass[compress,handout]{beamer}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
%\usepackage{pgfpages} 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    13
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    14
% Modified from: generic-ornate-15min-45min.de.tex
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
\mode<presentation>
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
{
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
  \usetheme{Warsaw}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    18
  \useoutertheme{split}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
  \setbeamercovered{transparent}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
\usepackage[english]{babel}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
\usepackage[latin1]{inputenc}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
%\usepackage{times}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
\usepackage[T1]{fontenc}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
% Taken from Fernando's slides.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
\usepackage{ae,aecompl}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
\usepackage{mathpazo,courier,euler}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
\usepackage[scaled=.95]{helvet}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
\definecolor{darkgreen}{rgb}{0,0.5,0}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
\usepackage{listings}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
\lstset{language=Python,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    36
    basicstyle=\ttfamily\bfseries,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
    commentstyle=\color{red}\itshape,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
  stringstyle=\color{darkgreen},
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
  showstringspaces=false,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
  keywordstyle=\color{blue}\bfseries}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
% Macros
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
\setbeamercolor{emphbar}{bg=blue!20, fg=black}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
\newcommand{\emphbar}[1]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
{\begin{beamercolorbox}[rounded=true]{emphbar} 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
      {#1}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
 \end{beamercolorbox}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    50
\newcounter{time}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
\setcounter{time}{0}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
\newcommand{\typ}[1]{\texttt{#1}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    56
\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
%%% This is from Fernando's setup.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    59
% \usepackage{color}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
% \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
% % Use and configure listings package for nicely formatted code
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
% \usepackage{listings}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    63
% \lstset{
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
%    language=Python,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
%    basicstyle=\small\ttfamily,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
%    commentstyle=\ttfamily\color{blue},
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
%    stringstyle=\ttfamily\color{orange},
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
%    showstringspaces=false,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
%    breaklines=true,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
%    postbreak = \space\dots
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
% }
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
% Title page
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
\title[Basic Python]{Python,\\a great programming toolkit:\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
numerics and plotting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    78
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
\author[Asokan \& Prabhu] {Asokan Pichai\\Prabhu Ramachandran}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    81
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    82
\date[] {26, July 2009}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    83
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    85
%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    86
%\logo{\pgfuseimage{iitmlogo}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    87
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    88
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    89
%% Delete this, if you do not want the table of contents to pop up at
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
%% the beginning of each subsection:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    91
\AtBeginSubsection[]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    92
{
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    93
  \begin{frame}<beamer>
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    94
    \frametitle{Outline}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    95
    \tableofcontents[currentsection,currentsubsection]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    96
  \end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    97
}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    98
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    99
\AtBeginSection[]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   100
{
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   101
  \begin{frame}<beamer>
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   102
    \frametitle{Outline}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   103
    \tableofcontents[currentsection,currentsubsection]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   104
  \end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   105
}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   106
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   107
% If you wish to uncover everything in a step-wise fashion, uncomment
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
% the following command: 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   109
%\beamerdefaultoverlayspecification{<+->}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   110
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   111
%\includeonlyframes{current,current1,current2,current3,current4,current5,current6}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   112
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   113
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   114
% DOCUMENT STARTS
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   115
\begin{document}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   116
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   117
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   118
  \frametitle{Outline}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   119
  \tableofcontents
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   120
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   121
\section{Pythonicity}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   122
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   123
    \frametitle{The Zen of Python}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   124
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   125
Try this!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   126
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   127
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   128
>>> import this
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   129
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   130
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   131
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   132
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   133
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   134
    {Style Guide}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   135
    
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   136
    Read PEP8
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   137
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   138
    \url{http://www.python.org/dev/peps/pep-0008/}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   139
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   140
    \inctime{10}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   141
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   142
\section{More Python Machinery}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   143
\subsection{Objects}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   144
\begin{frame}{Objects in Python}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   145
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   146
        \item What is an Object? (Types and classes)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   147
        \item identity
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   148
        \item type
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   149
        \item method
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   150
      \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   151
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   152
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   153
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   154
  \frametitle{Why are they useful?}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   155
  \small
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   156
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   157
for element in (1, 2, 3):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   158
    print element
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   159
for key in {'one':1, 'two':2}:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   160
    print key
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   161
for char in "123":
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   162
    print char
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   163
for line in open("myfile.txt"):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   164
    print line
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   165
for line in urllib2.urlopen('http://site.com'):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   166
    print line
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   167
  \end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   168
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   169
\begin{frame}{And the winner is \ldots OBJECTS!}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   170
  All objects providing a similar inteface can be used the same way.\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   171
  Functions (and others) are first-class objects. Can be passed to and returned from functions.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   172
  \inctime{10}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   173
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   174
\subsection{Dictionary}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   175
\begin{frame}{Dictionary}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   176
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   177
    \item aka associative arrays, key-value pairs, hashmaps, hashtables \ldots    
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   178
    \item \typ{ d = \{ ``Hitchhiker's guide'' : 42, ``Terminator'' : ``I'll be back''\}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   179
    \item lists and tuples index: 0 \ldots n
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   180
    \item dictionaries index using strings
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   181
    \item aka key-value pairs
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   182
    \item what can be keys?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   183
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   184
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   185
    
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   186
\begin{frame}{Dict \ldots }
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   187
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   188
    \item \alert{Unordered}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   189
      \begin{block}{Standard usage}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   190
        for key in dict:\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   191
            <use> dict[key] \# => value
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   192
      \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   193
    \item \typ{d.keys()} returns a list
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   194
    \item can we have duplicate keys?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   195
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   196
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   197
\begin{frame} {Problem Set 2.1}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   198
  \begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   199
\item[2.1.1] You are given date strings of the form ``29, Jul 2009'', or ``4 January 2008''. In other words a number a string and another number, with a comma sometimes separating the items.Write a function that takes such a string and returns a tuple (yyyy, mm, dd) where all three elements are ints.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   200
    \item[2.1.2] Count word frequencies in a file.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   201
    \item[2.1.3] Find the most used Python keywords in your Python code (import keyword).
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   202
\end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   203
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   204
\inctime{20}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   205
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   206
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   207
\subsection{Set}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   208
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   209
  \frametitle{Set}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   210
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   211
      \item Simplest container, mutable
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   212
      \item No ordering, no duplicates
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   213
      \item usual suspects: union, intersection, subset \ldots
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   214
      \item >, >=, <, <=, in, \ldots
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   215
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   216
    \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   217
f10 = set([1,2,3,5,8])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   218
p10 = set([2,3,5,7])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   219
f10|p10, f10&p10
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   220
f10-p10, p10-f10, f10^p10
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   221
set([2,3]) < p10, set([2,3]) <= p10
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   222
2 in p10, 4 in p10
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   223
len(f10)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   224
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   225
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   226
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   227
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   228
  \frametitle{Problem set 2.2}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   229
  \begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   230
    \item[2.2.1] Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   231
    \item[2.2.2] Given a string of the form ``4-7, 9, 12, 15'' find the numbers missing in this list for a given range.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   232
\end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   233
\inctime{15}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   234
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   235
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   236
\subsection{Functions Reloaded!}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   237
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   238
    \frametitle{Advanced functions}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   239
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   240
        \item default args
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   241
        \item varargs
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   242
        \item keyword args
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   243
        \item scope
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   244
        \item \typ{global}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   245
      \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   246
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   247
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   248
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   249
  \frametitle{Functions: default arguments}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   250
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   251
def ask_ok(prompt, retries=4, complaint='Yes or no!'):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   252
    while True:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   253
        ok = raw_input(prompt)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   254
        if ok in ('y', 'ye', 'yes'): 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   255
            return True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   256
        if ok in ('n', 'no', 'nop', 'nope'): 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   257
            return False
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   258
        retries = retries - 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   259
        if retries < 0: 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   260
            raise IOError, 'bad user'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   261
        print complaint
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   262
  \end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   263
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   264
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   265
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   266
  \frametitle{Functions: keyword arguments}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   267
  \small
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   268
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   269
def parrot(voltage, state='a stiff', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   270
           action='voom', type='Royal Blue'):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   271
    print "-- This parrot wouldn't", action,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   272
    print "if you supply", voltage, "Volts."
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   273
    print "-- Lovely plumage, the", type
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   274
    print "-- It's", state, "!"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   275
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   276
parrot(1000)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   277
parrot(action = 'VOOOOOM', voltage = 1000000)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   278
parrot('a thousand', state = 'pushing up the daisies')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   279
parrot('a million', 'bereft of life', 'jump')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   280
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   281
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   282
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   283
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   284
  \frametitle{Functions: arbitrary argument lists}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   285
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   286
  \item Arbitrary number of arguments using \verb+*args+ or
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   287
    \verb+*whatever+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   288
  \item Keyword arguments using \verb+**kw+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   289
  \item Given a tuple/dict how do you call a function?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   290
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   291
    \item Using argument unpacking
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   292
    \item For positional arguments: \verb+foo(*[5, 10])+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   293
    \item For keyword args: \verb+foo(**{'a':5, 'b':10})+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   294
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   295
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   296
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   297
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   298
  \begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   299
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   300
def foo(a=10, b=100):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   301
    print a, b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   302
def func(*args, **keyword):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   303
    print args, keyword
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   304
# Unpacking:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   305
args = [5, 10]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   306
foo(*args)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   307
kw = {'a':5, 'b':10}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   308
foo(**kw)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   309
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   310
    \inctime{15} 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   311
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   312
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   313
\subsection{Functional programming}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   314
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   315
    \frametitle{Functional programming}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   316
What is the basic idea?\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   317
Why is it interesting?\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   318
\typ{map, reduce, filter}\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   319
list comprehension\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   320
generators
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   321
    \inctime{10} 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   322
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   323
\end{document}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   324
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   325
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%