day2/EngPython.tex
author Puneeth Chaganti <punchagan@fossee.in>
Fri, 02 Oct 2009 18:02:38 +0530
changeset 3 18a010e4caa0
parent 0 9243d75024cc
permissions -rwxr-xr-x
Added day2 session1.
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
  \titlepage
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   119
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   120
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   121
  {Acknowledgements}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   122
  \begin{center}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   123
  This program is conducted by\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   124
  IIT, Bombay\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   125
  through CDEEP\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   126
  as part of  the open source initiatives\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   127
  under the aegis of\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   128
  \alert{National Mission on Education through ICT,} \\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   129
  Ministry of HRD.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   130
  \end{center}
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
  \frametitle{Outline}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   135
  \tableofcontents
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   136
  % You might wish to add the option [pausesections]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   137
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   138
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   139
\begin{frame}{Goal of the Workshop}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   140
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   141
        At the end of this program, successful participants will be able
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   142
        to use python as their scripting and problem solving language.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   143
        Aimed at Engg. students--focus on basic numerics and plotting--
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   144
        but should serve a similar purpose for others.\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   145
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   146
        At the minimum you will be able to use Python for your plotting immediately.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   147
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   148
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   149
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   150
\begin{frame}{Checklist}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   151
    
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   152
  \begin{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   153
	\item[pylab] matplotlib interface 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   154
	\item[numpy] Array computing
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   155
        \item[scipy] numerical work
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   156
        \item[mayavi] \typ{enthought.mayavi}: 3D viz.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   157
  \end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   158
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   159
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   160
\section{30000 feet view}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   161
\begin{frame}{Lets see what we can do!}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   162
  \huge
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   163
  Hold on to your seatbelts
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   164
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   165
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   166
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   167
  {That was done by\ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   168
  \begin{description}[CalisthenicsIsAnArt]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   169
      \item[Arrays] 2--3 lines; 5 minutes to learn
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   170
      \item[2D plots] 5 lines; 10 minutes to learn
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   171
      \item[Simple 3D plots] 5 lines; 10 minutes to learn; GUI
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   172
          exploration!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   173
      \item[Complex plots] relatively short (10-15 lines); more time to master; 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   174
  \end{description}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   175
  \inctime{15}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   176
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   177
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   178
\section{Matplotlib}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   179
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   180
\subsection{Basic \typ{numpy} }
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   181
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   182
\newcommand{\num}{\texttt{numpy}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   183
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   184
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   185
  \frametitle{The \num\ module}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   186
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   187
      \item Why?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   188
  \item What:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   189
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   190
    \item An efficient and powerful array type for various common data
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   191
      types
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   192
    \item Abstracts out the most commonly used standard operations on
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   193
      arrays
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   194
    \end{itemize}
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
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   198
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   199
  \frametitle{Examples of \num}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   200
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   201
# Simple array math example
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   202
>>> from numpy import *
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   203
>>> a = array([1,2,3,4])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   204
>>> b = array([2,3,4,5])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   205
>>> a*2 + b + 1 # Basic math!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   206
array([5, 8, 11, 14])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   207
# Pi and e are defined.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   208
>>> x = linspace(0.0, 10.0, 1000)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   209
>>> x *= 2*pi/10 # inplace.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   210
# apply functions to array.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   211
>>> y = sin(x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   212
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   213
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   214
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   215
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   216
  \frametitle{Basic concepts}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   217
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   218
  \item fixed size (\typ{arr.size});
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   219
  \item Same type (\typ{arr.dtype}) of data
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   220
  \item arbitrary dimensionality
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   221
  \item \typ{arr.shape}: size in each dimension
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   222
  \item \alert{Note:} \typ{len(arr) != arr.size} in general
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   223
  \item \alert{Note:} By default array operations are performed
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   224
    \alert{elementwise}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   225
  \item Indices, slicing: just like lists 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   226
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   227
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   228
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   229
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   230
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   231
  \frametitle{More examples of \num}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   232
\vspace*{-8pt}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   233
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   234
>>> x = array([1., 2, 3, 4])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   235
>>> size(x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   236
4
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   237
>>> x.dtype # What is a.dtype?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   238
dtype('float64')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   239
>>> x.shape
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   240
(4,)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   241
>>> print rank(x), x.itemsize
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   242
1 8
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   243
>>> x[0] = 10
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   244
>>> print x[0], x[-1]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   245
10.0 4.0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   246
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   247
    
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   248
\inctime{10}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   249
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   250
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   251
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   252
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   253
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   254
\subsection{Plotting with \typ{pylab}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   255
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   256
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   257
    {IPython's \typ{pylab} mode}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   258
\begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   259
    \item \typ{pylab}: convenient 2D plotting interface to MPL
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   260
    \item Immediate use: \typ{ipython -pylab}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   261
    \item Imports all of pylab for you!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   262
    \item Allows for interactive plotting
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   263
\end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   264
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   265
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   266
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   267
    \frametitle{Basic 2D plotting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   268
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   269
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   270
>>> x = linspace(0, 2*pi, 1000)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   271
>>> plot(x, sin(x)) 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   272
>>> plot(x, sin(x), 'ro')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   273
>>> xlabel(r'$\chi$', color='g')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   274
# LaTeX markup!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   275
>>> ylabel(r'sin($\chi$)', color='r')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   276
>>> title('Simple figure', fontsize=20)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   277
>>> savefig('/tmp/test.eps')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   278
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   279
\begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   280
  \item Also: PNG, PDF, PS, EPS, SVG, PDF
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   281
\end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   282
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   283
       
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   284
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   285
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   286
  \frametitle{Basic plotting \ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   287
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   288
# Set properties of objects:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   289
>>> l, = plot(x, sin(x))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   290
# Why "l,"?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   291
>>> setp(l, linewidth=2.0, color='r')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   292
>>> l.set_linewidth(2.0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   293
>>> draw() # Redraw.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   294
>>> setp(l) # Print properties
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   295
>>> clf() # Clear figure.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   296
>>> close() # Close figure.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   297
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   298
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   299
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   300
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   301
    \frametitle{Multiple figures}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   302
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   303
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   304
>>> figure(1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   305
>>> plot(x, sin(x))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   306
>>> figure(2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   307
>>> plot(x, tanh(x))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   308
>>> figure(1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   309
>>> title('Easy as 1,2,3')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   310
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   311
    
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   312
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   313
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   314
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   315
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   316
  \frametitle{Legends and Annotation}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   317
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   318
>>> plot(x, cos(5*x), 'r--', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   319
         label='cosine')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   320
>>> plot(x, sin(5*x), 'g--', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   321
         label='sine')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   322
>>> legend() 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   323
# Or use:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   324
>>> legend(['cosine', 'sine'])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   325
# Annotation:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   326
>>> text(1,0, '(1,0)')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   327
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   328
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   329
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   330
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   331
    \frametitle{More commands \ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   332
    \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   333
# semilog, loglog 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   334
>>> x = 10.**(-arange(100)*0.1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   335
>>> semilogx(x, x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   336
>>> semilogy(x, x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   337
>>> loglog(x, x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   338
>>> loglog(x, x*x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   339
    \end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   340
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   341
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   342
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   343
    \frametitle{More plots \ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   344
    \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   345
>>> clf()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   346
>>> t = arange(0.1, 4, 0.1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   347
>>> s = exp(-t)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   348
>>> e = 0.1*abs(randn(len(s)))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   349
>>> errorbar(t, s, e)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   350
# Scatter plots
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   351
>>> clf()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   352
>>> t = randn(len(e))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   353
>>> scatter(t, e, c=s)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   354
    \end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   355
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   356
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   357
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   358
    \frametitle{Note: \typ{pylab} in Python scripts}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   359
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   360
import pylab
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   361
x = pylab.linspace(0, 20, 1000)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   362
pylab.plot(x, pylab.sin(x))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   363
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   364
# Can also use:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   365
from pylab import linspace, sin, plot
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   366
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   367
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   368
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   369
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   370
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   371
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   372
  \frametitle{X-Y plot}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   373
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   374
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   375
    \hspace*{-0.5in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   376
    \includegraphics[height=2in, interpolate=true]{data/xyplot}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   377
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   378
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   379
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   380
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   381
t1 = arange(0.0, 5.0, 0.1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   382
t2 = arange(0.0, 5.0, 0.02)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   383
t3 = arange(0.0, 2.0, 0.01)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   384
subplot(211)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   385
plot(t1, cos(2*pi*t1)*exp(-t1), 'bo', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   386
     t2, cos(2*pi*t2)*exp(-t2), 'k')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   387
grid(True)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   388
title('A tale of 2 subplots')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   389
ylabel('Damped')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   390
subplot(212)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   391
plot(t3, cos(2*pi*t3), 'r--')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   392
grid(True)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   393
xlabel('time (s)')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   394
ylabel('Undamped')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   395
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   396
    \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   397
  \end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   398
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   399
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   400
\begin{frame}[fragile] \frametitle{Semi-log and log-log plots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   401
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   402
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   403
    \hspace*{-0.5in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   404
  \includegraphics[height=2in, interpolate=true]{data/log}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   405
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   406
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   407
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   408
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   409
dt = 0.01
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   410
t = arange(dt, 20.0, dt)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   411
subplot(311)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   412
semilogy(t, exp(-t/5.0))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   413
ylabel('semilogy')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   414
grid(True)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   415
subplot(312)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   416
semilogx(t, sin(2*pi*t))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   417
ylabel('semilogx')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   418
grid(True)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   419
# minor grid on too
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   420
gca().xaxis.grid(True, which='minor')  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   421
subplot(313)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   422
loglog(t, 20*exp(-t/10.0), basex=4)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   423
grid(True)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   424
ylabel('loglog base 4 on x')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   425
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   426
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   427
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   428
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   429
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   430
\begin{frame}[fragile] \frametitle{Errorbar}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   431
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   432
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   433
    \hspace*{-0.5in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   434
  \includegraphics[height=2in, interpolate=true]{data/errorbar}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   435
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   436
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   437
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   438
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   439
t = arange(0.1, 4, 0.1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   440
s = exp(-t)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   441
e = 0.1*abs(randn(len(s)))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   442
f = 0.1*abs(randn(len(s)))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   443
g = 2*e
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   444
h = 2*f
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   445
errorbar(t, s, [e,g], f, fmt='o')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   446
xlabel('Distance (m)')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   447
ylabel('Height (m)')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   448
title('Mean and standard error '\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   449
      'as a function of distance')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   450
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   451
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   452
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   453
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   454
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   455
\begin{frame}[fragile] \frametitle{Histogram}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   456
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   457
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   458
    \hspace*{-0.5in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   459
  \includegraphics[height=2in, interpolate=true]{data/histogram}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   460
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   461
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   462
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   463
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   464
mu, sigma = 100, 15
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   465
x = mu + sigma*randn(10000)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   466
# the histogram of the data
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   467
n, bins, patches = hist(x, 100, normed=1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   468
# add a 'best fit' line
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   469
y = normpdf( bins, mu, sigma)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   470
l = plot(bins, y, 'r--', linewidth=2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   471
xlim(40, 160)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   472
xlabel('Smarts')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   473
ylabel('P')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   474
title(r'$\rm{IQ:}\/ \mu=100,\/ \sigma=15$')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   475
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   476
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   477
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   478
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   479
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   480
\begin{frame}[fragile] \frametitle{Bar charts}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   481
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   482
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   483
    \hspace*{-0.5in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   484
  \includegraphics[height=2in, interpolate=true]{data/barchart}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   485
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   486
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   487
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   488
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   489
N = 5
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   490
menMeans = (20, 35, 30, 35, 27)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   491
menStd =   ( 2,  3,  4,  1,  2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   492
# the x locations for the groups
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   493
ind = arange(N) 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   494
# the width of the bars
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   495
width = 0.35       
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   496
p1 = bar(ind, menMeans, width, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   497
         color='r', yerr=menStd)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   498
womenMeans = (25, 32, 34, 20, 25)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   499
womenStd =   ( 3,  5,  2,  3,  3)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   500
p2 = bar(ind+width, womenMeans, width, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   501
         color='y', yerr=womenStd)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   502
ylabel('Scores')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   503
title('Scores by group and gender')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   504
xticks(ind+width, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   505
       ('G1', 'G2', 'G3', 'G4', 'G5'))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   506
xlim(-width,len(ind))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   507
yticks(arange(0,41,10))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   508
legend((p1[0], p2[0]), 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   509
       ('Men', 'Women'), shadow=True)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   510
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   511
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   512
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   513
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   514
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   515
\begin{frame}[fragile] \frametitle{Pie charts}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   516
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   517
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   518
    \hspace*{-0.4in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   519
  \includegraphics[height=2.0in, interpolate=true]{data/piechart}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   520
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   521
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   522
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   523
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   524
# make a square figure and axes
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   525
figure(1, figsize=(8,8))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   526
ax = axes([0.1, 0.1, 0.8, 0.8])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   527
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   528
fracs = [15,30,45, 10]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   529
explode=(0, 0.05, 0, 0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   530
pie(fracs, explode=explode, labels=labels, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   531
    autopct='%1.1f%%', shadow=True)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   532
title('Raining Hogs and Dogs', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   533
      bbox={'facecolor':'0.8', 'pad':5})
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   534
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   535
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   536
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   537
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   538
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   539
\begin{frame}[fragile] \frametitle{Scatter plots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   540
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   541
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   542
    \hspace*{-0.4in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   543
  \includegraphics[height=2in, interpolate=true]{data/scatter}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   544
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   545
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   546
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   547
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   548
N = 30
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   549
x = 0.9*rand(N)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   550
y = 0.9*rand(N)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   551
# 0 to 10 point radiuses
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   552
area = pi*(10 * rand(N))**2 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   553
volume = 400 + rand(N)*450
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   554
scatter(x,y,s=area, marker='o', c=volume, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   555
        alpha=0.75)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   556
xlabel(r'$\Delta_i$', size='x-large')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   557
ylabel(r'$\Delta_{i+1}$', size='x-large')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   558
title(r'Volume and percent change')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   559
grid(True)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   560
colorbar()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   561
savefig('scatter')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   562
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   563
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   564
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   565
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   566
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   567
\begin{frame}[fragile] \frametitle{Polar}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   568
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   569
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   570
    \hspace*{-0.5in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   571
  \includegraphics[height=2in, interpolate=true]{data/polar}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   572
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   573
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   574
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   575
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   576
figure(figsize=(8,8))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   577
ax = axes([0.1, 0.1, 0.8, 0.8], 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   578
          polar=True, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   579
          axisbg='#d5de9c')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   580
r = arange(0,1,0.001)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   581
theta = 2*2*pi*r
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   582
polar(theta, r, color='#ee8d18', lw=3)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   583
# the radius of the grid labels
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   584
setp(ax.thetagridlabels, y=1.075) 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   585
title(r"$\theta=4\pi r", fontsize=20)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   586
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   587
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   588
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   589
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   590
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   591
\begin{frame}[fragile] \frametitle{Contours}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   592
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   593
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   594
    \hspace*{-0.5in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   595
  \includegraphics[height=2in, interpolate=true]{data/contour}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   596
    \column{0.525\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   597
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   598
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   599
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   600
x = arange(-3.0, 3.0, 0.025)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   601
y = arange(-2.0, 2.0, 0.025)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   602
X, Y = meshgrid(x, y)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   603
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   604
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   605
# difference of Gaussians
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   606
Z = 10.0 * (Z2 - Z1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   607
im = imshow(Z, interpolation='bilinear', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   608
            origin='lower',
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   609
            cmap=cm.gray, extent=(-3,3,-2,2))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   610
levels = arange(-1.2, 1.6, 0.2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   611
# label every second level
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   612
clabel(CS, levels[1::2],  inline=1,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   613
       fmt='%1.1f', fontsize=14)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   614
CS = contour(Z, levels,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   615
             origin='lower',
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   616
             linewidths=2,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   617
             extent=(-3,3,-2,2))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   618
# make a colorbar for the contour lines
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   619
CB = colorbar(CS, shrink=0.8, extend='both')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   620
title('Lines with colorbar')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   621
hot(); flag()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   622
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   623
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   624
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   625
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   626
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   627
\begin{frame}[fragile] \frametitle{Velocity vectors}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   628
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   629
    \column{0.5\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   630
    \hspace*{-0.5in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   631
  \includegraphics[height=2in, interpolate=true]{data/quiver}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   632
    \column{0.45\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   633
    \begin{block}{Example code}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   634
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   635
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   636
X,Y = meshgrid(arange(0,2*pi,.2),
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   637
               arange(0,2*pi,.2) )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   638
U = cos(X)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   639
V = sin(Y)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   640
Q = quiver(X[::3, ::3], Y[::3, ::3], 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   641
           U[::3, ::3], V[::3, ::3],
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   642
           color='r', units='x', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   643
           linewidths=(2,), 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   644
           edgecolors=('k'), 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   645
           headaxislength=5 )
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   646
qk = quiverkey(Q, 0.5, 0.03, 1, '1 m/s', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   647
               fontproperties=
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   648
               {'weight': 'bold'})
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   649
axis([-1, 7, -1, 7])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   650
title('triangular head; scale '\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   651
      'with x view; black edges')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   652
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   653
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   654
\end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   655
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   656
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   657
\begin{frame}[fragile] \frametitle{Maps}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   658
  \includegraphics[height=2.5in, interpolate=true]{data/plotmap}  
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   659
  \begin{center}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   660
    \tiny
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   661
    For details see \url{http://matplotlib.sourceforge.net/screenshots/plotmap.py}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   662
  \end{center}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   663
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   664
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   665
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   666
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   667
  \frametitle{More information}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   668
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   669
  \item More information here: \url{http://matplotlib.sf.net}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   670
  \item \url{http://matplotlib.sf.net/tutorial.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   671
  \item \url{http://matplotlib.sf.net/screenshots.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   672
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   673
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   674
  \inctime{25}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   675
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   676
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   677
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   678
  \frametitle{Problem set 1.0}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   679
  \begin{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   680
      \item Write a function that plots any n-gon given \typ{n}.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   681
      \item Consider the logistic map, $f(x) = kx(1-x)$, plot it for
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   682
          $k=2.5, 3.5$ and $4$
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   683
\end{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   684
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   685
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   686
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   687
  \frametitle{Problem set 1.1}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   688
  \begin{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   689
      \item Consider the iteration $x_{n+1} = f(x_n)$ where $f(x) =
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   690
          kx(1-x)$.  Plot the successive iterates of this process.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   691
      \item Plot this using a cobweb plot as follows:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   692
          \begin{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   693
              \item Start at $(x_0, 0)$
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   694
              \item Draw line to $(x_i, f(x_i))$; 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   695
              \item Set $x_{i+1} = f(x_i)$
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   696
              \item Draw line to $(x_i, x_i)$
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   697
              \item Repeat from 2 for as long as you want 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   698
          \end{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   699
    \end{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   700
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   701
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   702
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   703
  \frametitle{Problem set 1.2}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   704
  \begin{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   705
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   706
      \item Plot the Koch snowflake.  Write a function to generate the
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   707
          necessary points given the two points constituting a line.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   708
          \pause
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   709
          \begin{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   710
              \item Split the line into 4 segments.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   711
              \item The first and last segments are trivial.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   712
              \item To rotate the point you can use complex numbers,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   713
                  recall that $z e^{j \theta}$ rotates a point $z$ in 2D
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   714
                  by $\theta$.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   715
              \item Do this for all line segments till everything is
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   716
                  done.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   717
          \end{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   718
      \item Show rate of convergence for a first and second order finite
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   719
          difference of sin(x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   720
\end{enumerate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   721
\inctime{30}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   722
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   723
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   724
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   725
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   726
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   727
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   728
  \frametitle{More IPython features}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   729
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   730
  \item Input and output caching:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   731
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   732
    \item \verb+In+: a list of all entered input
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   733
    \item \verb+Out+: a dict of all output
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   734
    \item \verb+%hist [-n]+ macro shows previous history, \verb+-n+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   735
      suppresses line number information
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   736
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   737
  \item Log the session using \verb+%logstart+, \verb+%logon+ and
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   738
    \verb+%logoff+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   739
  \item Use \verb+;+ to suppress printing output
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   740
  \item \verb+%time statement+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   741
  \item \verb+%timeit [-n<N> -r<R> [-t|-c]] statement+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   742
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   743
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   744
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   745
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   746
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   747
  \frametitle{More IPython features}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   748
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   749
  \item \verb+%run [options] file[.py]+ -- running Python code
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   750
  \item \verb+%prun+ runs a statement/expression under the profiler
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   751
  \item \verb+%debug+: Helps with debugging after a crash
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   752
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   753
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   754
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   755
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   756
  \frametitle{More IPython features \ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   757
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   758
  \item \verb+%edit [options] [args]+: edit lines of code or file
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   759
    specified in editor (configure editor via \verb+$EDITOR+)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   760
  \item \verb+%cd+ changes directory, see also \verb+%pushd, %popd, %dhist+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   761
  \item Shell access
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   762
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   763
    \item \verb+!command+ runs a shell command and returns its output
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   764
    \item \verb+files = !ls+ sets
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   765
      \verb+files+ to all result of the \verb+ls+ command
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   766
    \item \verb+!ls $files+ passes the \verb+files+ variable to the
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   767
      shell command
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   768
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   769
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   770
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   771
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   772
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   773
  \frametitle{More IPython features \ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   774
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   775
  \item \verb+%bookmark+: store a bookmarked location, for use with \verb+%cd+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   776
  \item \verb+%save [options] filename n1-n2 n3-n4+: save lines to a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   777
    file
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   778
  \item Can define and use profiles to setup IPython differently:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   779
    \verb+math, scipy, numeric, pysh+ etc.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   780
  \item \verb+%magic+: \alert{Show help on all magics}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   781
  \item Check out the \verb+%macro+ magic
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   782
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   783
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   784
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   785
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   786
  \frametitle{Problem set 2}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   787
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   788
    \item Compare your linspace with that of numpy for 1 million
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   789
        elements in terms of speed.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   790
\end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   791
\inctime{10}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   792
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   793
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   794
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   795
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   796
    \frametitle{Debugging effectively}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   797
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   798
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   799
        \item  \kwrd{print} based strategy
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   800
        \item Process: Hypothesis, test, refine, rinse-repeat
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   801
        \item Using \typ{\%debug} and \typ{\%pdb} in IPython
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   802
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   803
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   804
    \inctime{10} 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   805
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   806
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   807
\section{Debugging and testing}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   808
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   809
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   810
    \frametitle{Testing code with \typ{nosetests}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   811
   
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   812
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   813
        \item Writing tests is really simple!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   814
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   815
        \item Using nose
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   816
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   817
        \item Example!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   818
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   819
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   820
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   821
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   822
    \frametitle{Nosetest}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   823
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   824
def gcd(a, b):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   825
    """Returns gcd of a and b, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   826
     handles only positive numbers."""
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   827
    if a%b == 0: return b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   828
    return gcd(b, a%b)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   829
def lcm(a, b):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   830
    return a*b/gcd(a, b)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   831
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   832
if __name__ == '__main__':
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   833
    import nose
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   834
    nose.main()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   835
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   836
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   837
    \inctime{10} 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   838
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   839
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   840
\section{NumPy and SciPy}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   841
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   842
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   843
    {More Numpy}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   844
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   845
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   846
        \item Multi-dimensional arrays
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   847
        \item Random number generation
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   848
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   849
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   850
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   851
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   852
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   853
  \frametitle{Multi-dimensional arrays}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   854
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   855
>>> a = array([[ 0, 1, 2, 3],
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   856
...            [10,11,12,13]])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   857
>>> a.shape # (rows, columns)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   858
(2, 4)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   859
# Accessing and setting values
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   860
>>> a[1,3] 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   861
13
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   862
>>> a[1,3] = -1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   863
>>> a[1] # The second row
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   864
array([10,11,12,-1])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   865
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   866
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   867
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   868
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   869
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   870
  \frametitle{Slicing arrays}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   871
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   872
>>> a = array([[1,2,3], [4,5,6], 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   873
               [7,8,9]])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   874
>>> a[0,1:3]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   875
array([2, 3])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   876
>>> a[1:,1:]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   877
array([[5, 6],
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   878
       [8, 9]])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   879
>>> a[:,2]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   880
array([3, 6, 9])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   881
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   882
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   883
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   884
  \frametitle{Striding arrays}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   885
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   886
>>> a[0::2,0::2]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   887
array([[1, 3],
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   888
       [7, 9]])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   889
# Slices are references to the 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   890
# same memory!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   891
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   892
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   893
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   894
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   895
  \frametitle{Array creation functions}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   896
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   897
  \item \typ{array(object, dtype=None, \ldots)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   898
  \item \typ{arange(start, stop=None, step=1 \ldots)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   899
  \item \typ{linspace(start, stop, num=50, \ldots)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   900
  \item \typ{ones(shape, dtype=None, \ldots)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   901
  \item \typ{zeros(shape, dtype=float,\ldots)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   902
  \item \typ{identity(n)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   903
  \item \typ{empty(shape, dtype=float,\ldots)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   904
  \item \typ{ones\_like(x)}, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   905
  \item \typ{zeros\_like(x)}, \typ{empty\_like(x)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   906
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   907
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   908
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   909
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   910
  \frametitle{Array math}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   911
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   912
  \item Basic \alert{elementwise} math (given two arrays \typ{a, b}):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   913
      \typ{+, -, *, /, \%}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   914
  \item Inplace operators: \typ{a += b}, or \typ{add(a, b,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   915
      a)} etc.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   916
  \item Logical operations: \typ{equal (==)}, \typ{not\_equal (!=)},
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   917
    \typ{less (<)}, \typ{greater (>)} etc.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   918
  \item Trig and other functions: \typ{sin(x), arcsin(x), sinh(x),
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   919
      exp(x), sqrt(x)} etc.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   920
  \item \typ{sum(x, axis=0), product(x, axis=0)} 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   921
  \item \typ{dot(a, b)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   922
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   923
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   924
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   925
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   926
  \frametitle{Advanced}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   927
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   928
  \item Only scratched the surface of \num
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   929
  \item Ufunc methods: \typ{reduce, accumulate, outer, reduceat}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   930
  \item Typecasting
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   931
  \item More functions: \typ{take, choose, where, compress,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   932
      concatenate}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   933
  \item Array broadcasting and \typ{None}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   934
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   935
  \inctime{15}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   936
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   937
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   938
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   939
    {Intro to SciPy}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   940
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   941
  \item \url{http://www.scipy.org}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   942
  \item Open source scientific libraries for Python
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   943
  \item Based on NumPy
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   944
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   945
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   946
    \inctime{25}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   947
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   948
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   949
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   950
  \frametitle{SciPy}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   951
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   952
  \item Provides:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   953
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   954
    \item Linear algebra
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   955
    \item Numerical integration
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   956
    \item Fourier transforms
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   957
    \item Signal processing
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   958
    \item Special functions
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   959
    \item Statistics
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   960
    \item Optimization
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   961
    \item Image processing
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   962
    \item ODE solvers
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   963
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   964
  \item Uses LAPACK, QUADPACK, ODEPACK, FFTPACK etc. from netlib
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   965
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   966
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   967
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   968
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   969
\section{3D Plotting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   970
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   971
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   972
  \frametitle{Introduction to Mayavi}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   973
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   974
  \item Most scientists not interested in details of visualization
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   975
  \item Visualization of data files with a nice UI
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   976
  \item Interactive visualization of data (think Matlab)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   977
  \item Embedding visualizations in applications
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   978
  \item Customization
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   979
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   980
  \pause
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   981
  \begin{block}{The Goal}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   982
      Provide a \alert{flexible} library/app for every one of these needs!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   983
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   984
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   985
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   986
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   987
    \frametitle{\typ{mlab}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   988
  \begin{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   989
    \column{0.62\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   990
    \hspace*{-0.45in}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   991
      \footnotesize
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   992
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   993
from enthought.mayavi import mlab
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   994
from numpy import ogrid, sin
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   995
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   996
x, y, z = ogrid[-10:10:100j, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   997
                -10:10:100j, 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   998
                -10:10:100j]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   999
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1000
mlab.contour3d(sin(x*y*z)/(x*y*z))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1001
mlab.show()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1002
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1003
    \column{0.4\textwidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1004
    \hspace*{-0.1\linewidth}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1005
    \includegraphics[width=1.18\linewidth]{data/mlab.png}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1006
  \end{columns}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1007
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1008
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1009
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1010
    {A Look at the docs }
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1011
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1012
    \inctime{20}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1013
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1014
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1015
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1016
\section{Integration demo: Lorenz equations}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1017
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1018
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1019
    \frametitle{Lorenz equation example}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1020
    \begin{eqnarray*}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1021
        \frac{d x}{dt} &=& s (y-x)\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1022
        \frac{d y}{d t} &=& rx -y -xz\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1023
        \frac{d z}{d t} &=& xy - bz\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1024
    \end{eqnarray*}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1025
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1026
        \item Specifies the evolution of the system
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1027
        \item Think: Velocity of a particle in 3D
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1028
        \item Lets trace its path
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1029
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1030
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1031
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1032
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1033
    {Interactive exploration}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1034
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1035
    \inctime{25}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1036
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1037
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1038
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1039
\end{document}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1040
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1041
- Numpy arrays (30 mins)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1042
    - Matrices
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1043
    - random number generation.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1044
    - Image manipulation: jigsaw puzzle.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1045
    - Monte-carlo integration.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1046
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1047
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1048
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1049
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1050
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1051
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1052
  \frametitle{More on functions}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1053
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1054
  \item Support default and keyword arguments
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1055
  \item Scope of variables in the function is local
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1056
  \item Mutable items are \alert{passed by reference}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1057
  \item First line after definition may be a documentation string
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1058
    (\alert{recommended!})
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1059
  \item Function definition and execution defines a name bound to the
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1060
    function
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1061
  \item You \emph{can} assign a variable to a function!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1062
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1063
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1064
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1065
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1066
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1067
  \frametitle{Functions: default arguments}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1068
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1069
def ask_ok(prompt, retries=4, complaint='Yes or no!'):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1070
    while True:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1071
        ok = raw_input(prompt)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1072
        if ok in ('y', 'ye', 'yes'): 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1073
            return True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1074
        if ok in ('n', 'no', 'nop', 'nope'): 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1075
            return False
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1076
        retries = retries - 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1077
        if retries < 0: 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1078
            raise IOError, 'bad user'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1079
        print complaint
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1080
  \end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1081
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1082
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1083
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1084
  \frametitle{Functions: keyword arguments}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1085
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1086
def parrot(voltage, state='a stiff', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1087
           action='voom', type='Norwegian Blue'):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1088
    print "-- This parrot wouldn't", action,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1089
    print "if you put", voltage, "Volts through it."
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1090
    print "-- Lovely plumage, the", type
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1091
    print "-- It's", state, "!"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1092
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1093
parrot(1000)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1094
parrot(action = 'VOOOOOM', voltage = 1000000)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1095
parrot('a thousand', state = 'pushing up the daisies')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1096
parrot('a million', 'bereft of life', 'jump')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1097
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1098
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1099
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1100
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1101
  \frametitle{Functions: arbitrary argument lists}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1102
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1103
  \item Arbitrary number of arguments using \verb+*args+ or
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1104
    \verb+*whatever+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1105
  \item Keyword arguments using \verb+**kw+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1106
  \item Given a tuple/dict how do you call a function?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1107
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1108
    \item Using argument unpacking
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1109
    \item For positional arguments: \verb+foo(*[5, 10])+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1110
    \item For keyword args: \verb+foo(**{'a':5, 'b':10})+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1111
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1112
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1113
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1114
def foo(a=10, b=100):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1115
    print a, b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1116
def func(*args, **keyword):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1117
    print args, keyword
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1118
# Unpacking:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1119
args = [5, 10]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1120
foo(*args)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1121
kw = {'a':5, 'b':10}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1122
foo(**kw)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1123
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1124
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1125
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1126
\subsection{Modules, exceptions, classes}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1127
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1128
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1129
  \frametitle{Modules}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1130
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1131
  \item Define variables, functions and classes in a file with a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1132
    \typ{.py} extension
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1133
  \item This file becomes a module!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1134
  \item Modules are searched in the following:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1135
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1136
    \item Current directory
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1137
    \item Standard: \typ{/usr/lib/python2.3/site-packages/} etc.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1138
    \item Directories specified in PYTHONPATH
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1139
    \item \typ{sys.path}: current path settings (from the \typ{sys}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1140
      module)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1141
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1142
  \item The \typ{import} keyword ``loads'' a module
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1143
  \item One can also use:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1144
    \mbox{\typ{from module import name1, name2, name2}}\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1145
    where \typ{name1} etc. are names in the module, ``module''
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1146
  \item \typ{from module import *} \ --- imports everything from module,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1147
    \alert{use only in interactive mode}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1148
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1149
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1150
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1151
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1152
  \frametitle{Modules: example}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1153
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1154
# --- foo.py ---
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1155
some_var = 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1156
def fib(n): # write Fibonacci series up to n
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1157
    """Print a Fibonacci series up to n."""
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1158
    a, b = 0, 1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1159
    while b < n:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1160
        print b,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1161
        a, b = b, a+b
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1162
# EOF
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1163
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1164
>>> import foo
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1165
>>> foo.fib(10)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1166
1 1 2 3 5 8 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1167
>>> foo.some_var
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1168
1
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1169
  \end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1170
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1171
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1172
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1173
  \frametitle{Namespaces}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1174
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1175
  \item A mapping from names to objects
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1176
  \item Modules introduce a namespace 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1177
  \item So do classes
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1178
  \item The running script's namespace is \verb+__main__+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1179
  \item A modules namespace is identified by its name
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1180
  \item The standard functions (like \typ{len}) are in the
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1181
    \verb+__builtin__+ namespace
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1182
  \item Namespaces help organize different names and their bindings to
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1183
    different objects
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1184
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1185
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1186
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1187
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1188
  \frametitle{Exceptions}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1189
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1190
  \item Python's way of notifying you of errors
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1191
  \item Several standard exceptions: \typ{SyntaxError}, \typ{IOError}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1192
    etc.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1193
  \item Users can also \typ{raise} errors
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1194
  \item Users can create their own exceptions
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1195
  \item Exceptions can be ``caught'' via \typ{try/except} blocks
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1196
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1197
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1198
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1199
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1200
  \frametitle{Exception: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1201
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1202
>>> 10 * (1/0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1203
Traceback (most recent call last):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1204
  File "<stdin>", line 1, in ?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1205
ZeroDivisionError: integer division or modulo by zero
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1206
>>> 4 + spam*3
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1207
Traceback (most recent call last):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1208
  File "<stdin>", line 1, in ?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1209
NameError: name 'spam' is not defined
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1210
>>> '2' + 2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1211
Traceback (most recent call last):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1212
  File "<stdin>", line 1, in ?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1213
TypeError: cannot concatenate 'str' and 'int' objects
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1214
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1215
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1216
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1217
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1218
  \frametitle{Exception: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1219
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1220
>>> while True:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1221
...     try:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1222
...         x = int(raw_input("Enter a number: "))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1223
...         break
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1224
...     except ValueError:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1225
...         print "Invalid number, try again..."
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1226
...
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1227
>>> # To raise exceptions
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1228
... raise ValueError, "your error message"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1229
Traceback (most recent call last):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1230
  File "<stdin>", line 2, in ?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1231
ValueError: your error message
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1232
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1233
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1234
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1235
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1236
  \frametitle{Classes: the big picture}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1237
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1238
  \item Lets you create new data types
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1239
  \item Class is a template for an object belonging to that class
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1240
  \item Note: in Python a class is also an object
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1241
  \item Instantiating a class creates an instance (an object)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1242
  \item An instance encapsulates the state (data) and behavior
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1243
    (methods)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1244
  \item Allows you to define an inheritance hierarchy
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1245
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1246
    \item ``A Honda car \alert{is a} car.''
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1247
    \item ``A car \alert{is an} automobile.''
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1248
    \item ``A Python \alert{is a} reptile.''
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1249
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1250
  \item Programmers need to think OO
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1251
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1252
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1253
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1254
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1255
  \frametitle{Classes: what's the big deal?}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1256
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1257
  \item Lets you create objects that mimic a real problem being
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1258
    simulated
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1259
  \item Makes problem solving more natural and elegant
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1260
  \item Easier to create code
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1261
  \item Allows for code-reuse
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1262
  \item Polymorphism
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1263
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1264
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1265
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1266
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1267
  \frametitle{Class definition and instantiation}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1268
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1269
  \item Class definitions when executed create class objects
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1270
  \item Instantiating the class object creates an instance of the
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1271
    class
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1272
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1273
\footnotesize
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1274
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1275
class Foo(object):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1276
    pass
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1277
# class object created.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1278
# Create an instance of Foo.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1279
f = Foo()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1280
# Can assign an attribute to the instance
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1281
f.a = 100
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1282
print f.a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1283
100
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1284
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1285
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1286
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1287
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1288
  \frametitle{Classes \ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1289
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1290
  \item All attributes are accessed via the \typ{object.attribute}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1291
    syntax
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1292
  \item Both class and instance attributes are supported
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1293
  \item \emph{Methods} represent the behavior of an object: crudely
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1294
    think of them as functions ``belonging'' to the object
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1295
  \item All methods in Python are ``virtual''
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1296
  \item Inheritance through subclassing
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1297
  \item Multiple inheritance is supported
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1298
  \item No special public and private attributes: only good
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1299
    conventions
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1300
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1301
    \item \verb+object.public()+: public
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1302
    \item \verb+object._private()+ \& \verb+object.__priv()+: 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1303
      non-public
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1304
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1305
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1306
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1307
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1308
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1309
  \frametitle{Classes: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1310
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1311
class MyClass(object):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1312
    """Example class (this is the class docstring)."""
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1313
    i = 12345 # A class attribute
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1314
    def f(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1315
        """This is the method docstring"""
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1316
        return 'hello world'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1317
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1318
>>> a = MyClass() # creates an instance
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1319
>>> a.f()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1320
'hello world'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1321
>>> # a.f() is equivalent to MyClass.f(a)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1322
... # This also explains why f has a 'self' argument.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1323
... MyClass.f(a)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1324
'hello world'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1325
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1326
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1327
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1328
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1329
  \frametitle{Classes (continued)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1330
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1331
  \item \typ{self} is \alert{conventionally} the first argument for a
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1332
    method
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1333
  \item In previous example, \typ{a.f} is a method object
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1334
  \item When \typ{a.f} is called, it is passed the instance \typ{a} as
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1335
    the first argument
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1336
  \item If a method called \verb+__init__+ exists, it is called when
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1337
    the object is created
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1338
  \item If a method called \verb+__del__+ exists, it is called before
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1339
    the object is garbage collected
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1340
  \item Instance attributes are set by simply ``setting'' them in
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1341
    \typ{self}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1342
  \item Other special methods (by convention) like \verb+__add__+ let
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1343
    you define numeric types:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1344
    {\footnotesize \url{http://docs.python.org/ref/specialnames.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1345
      \\ \url{http://docs.python.org/ref/numeric-types.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1346
    }
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1347
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1348
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1349
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1350
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1351
  \frametitle{Classes: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1352
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1353
class Bag(MyClass): # Shows how to derive classes
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1354
    def __init__(self): # called on object creation.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1355
        self.data = [] # an instance attribute
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1356
    def add(self, x):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1357
        self.data.append(x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1358
    def addtwice(self, x):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1359
        self.add(x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1360
        self.add(x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1361
>>> a = Bag()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1362
>>> a.f() # Inherited method
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1363
'hello world'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1364
>>> a.add(1); a.addtwice(2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1365
>>> a.data
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1366
[1, 2, 2]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1367
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1368
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1369
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1370
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1371
  \frametitle{Derived classes}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1372
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1373
  \item Call the parent's \verb+__init__+ if needed
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1374
  \item If you don't need a new constructor, no need to define it in subclass
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1375
  \item Can also use the \verb+super+ built-in function
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1376
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1377
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1378
class AnotherBag(Bag):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1379
    def __init__(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1380
        # Must call parent's __init__ explicitly
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1381
        Bag.__init__(self)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1382
        # Alternatively use this:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1383
        super(AnotherBag, self).__init__()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1384
        # Now setup any more data.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1385
        self.more_data = []
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1386
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1387
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1388
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1389
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1390
  \frametitle{Classes: polymorphism}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1391
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1392
class Drawable(object):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1393
    def draw(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1394
        # Just a specification.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1395
        pass
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1396
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1397
\mode<presentation>{\pause}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1398
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1399
class Square(Drawable):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1400
    def draw(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1401
        # draw a square.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1402
class Circle(Drawable):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1403
    def draw(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1404
        # draw a circle.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1405
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1406
\mode<presentation>{\pause}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1407
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1408
class Artist(Drawable):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1409
    def draw(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1410
        for obj in self.drawables:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1411
            obj.draw()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1412
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1413
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1414
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1415
\subsection{Miscellaneous}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1416
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1417
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1418
  \frametitle{Stand-alone scripts}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1419
Consider a file \typ{f.py}:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1420
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1421
#!/usr/bin/env python
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1422
"""Module level documentation."""
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1423
# First line tells the shell that it should use Python
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1424
# to interpret the code in the file.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1425
def f():
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1426
    print "f"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1427
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1428
# Check if we are running standalone or as module.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1429
# When imported, __name__ will not be '__main__'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1430
if __name__ == '__main__':
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1431
    # This is not executed when f.py is imported.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1432
    f() 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1433
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1434
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1435
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1436
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1437
  \frametitle{List comprehensions}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1438
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1439
>>> veg = ['tomato', 'cabbage', 'carrot', 'potato']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1440
>>> [x.upper() for x in veg]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1441
['TOMATO', 'CABBAGE', 'CARROT', 'POTATO']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1442
>>> vec = range(0, 8)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1443
>>> even = [x for x in vec if x%2 == 0]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1444
>>> even
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1445
[0, 2, 4, 6]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1446
>>> [x*x for x in even]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1447
[0, 4, 16, 36]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1448
>>> odd = [x for x in vec if x%2 == 1]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1449
>>> odd
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1450
[1, 3, 5, 7]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1451
>>> [x*y for x in even for y in odd]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1452
[0, 0, 0, 0, 2, 6, 10, 14, 4, 12, 20, 28, 6, 18,30,42]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1453
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1454
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1455
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1456
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1457
  \frametitle{File handling}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1458
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1459
>>> # Reading files:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1460
... f = open('/path/to/file_name')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1461
>>> data = f.read() # Read entire file.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1462
>>> line = f.readline() # Read one line.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1463
>>> # Read entire file appending each line into a list
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1464
... lines = f.readlines()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1465
>>> f.close() # close the file.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1466
>>> # Writing files:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1467
... f = open('/path/to/file_name', 'w')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1468
>>> f.write('hello world\n')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1469
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1470
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1471
  \item \typ{tell()}: returns int of current position
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1472
  \item \typ{seek(pos)}: moves current position to specified byte
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1473
  \item Call \typ{close()} when done using a file
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1474
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1475
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1476
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1477
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1478
  \frametitle{Math}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1479
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1480
  \item \typ{math} module provides basic math routines for
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1481
    floats
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1482
  \item \typ{cmath} module provides math routies for complex
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1483
    numbers
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1484
  \item \typ{random}: provides pseudo-random number generators
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1485
    for various distributions
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1486
  \item These are always available and part of the standard library
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1487
  \item More serious math is provided by the NumPy/SciPy modules --
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1488
    these are not standard and need to be installed separately
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1489
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1490
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1491
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1492
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1493
  \frametitle{Timing and profiling}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1494
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1495
  \item Timing code: use the \typ{time} module
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1496
  \item Read up on \typ{time.time()} and \typ{time.clock()}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1497
  \item \typ{timeit}: is a better way of doing timing
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1498
  \item IPython has handy \typ{time} and \typ{timeit} macros (type
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1499
    \typ{timeit?} for help)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1500
  \item IPython lets you debug and profile code via the \typ{run}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1501
    macro (type \typ{run?} on the prompt to learn more)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1502
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1503
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1504
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1505
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1506
  \frametitle{Odds and ends}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1507
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1508
  \item \typ{dir([object])} function: attributes of given object
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1509
  \item \typ{type(object)}: returns type information
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1510
  \item \typ{str(), repr()}: convert object to string representation
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1511
  \item \typ{isinstance, issubclass}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1512
  \item \typ{assert} statements let you do debugging assertions in
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1513
    code
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1514
  \item \typ{csv} module: reading and writing CSV files
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1515
  \item \typ{pickle}: lets you save and load Python objects
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1516
    (\alert{serialization})
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1517
  \item \typ{sys.argv}: command line arguments
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1518
  \item \typ{os.path}: common path manipulations
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1519
  \item Check out the Python Library reference:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1520
    \url{http://docs.python.org/lib/lib.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1521
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1522
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1523
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1524
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1525
  \frametitle{Test driven development (TDD)}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1526
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1527
      \item Why?
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1528
          \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1529
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1530
            \item Forces you to write reusable code!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1531
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1532
            \item Think about the API
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1533
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1534
            \item More robust
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1535
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1536
            \item Makes refactoring very easy
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1537
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1538
          \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1539
      \item How?  Python offers three major ways of doing this
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1540
          \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1541
              \item doctest
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1542
              \item unittest
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1543
              \item nosetest (and similar like py.test)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1544
          \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1545
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1546
      \item Test every piece of functionality you offer
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1547
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1548
      \item This isn't a formal introduction but more a practical one
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1549
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1550
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1551
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1552
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1553
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1554
  \frametitle{Unit test}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1555
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1556
import unittest
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1557
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1558
class MyTestCase(unittest.TestCase):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1559
    def setUp(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1560
        # Called *before* each test_* 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1561
    def tearDown(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1562
        # Called *after* each test_* 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1563
    def test_something(self):
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1564
        "docstring"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1565
        #  Test code.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1566
        self.assertEqual(x, y)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1567
        self.assertRaises(ValueError, func, arg1, arg2 ...)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1568
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1569
if __name__ == '__main__':
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1570
    unittest.main() 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1571
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1572
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1573
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1574
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1575
    \frametitle{Nosetest}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1576
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1577
import particle
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1578
def test_particle():
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1579
    # Use asserts here.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1580
    p = particle.Particle(1.0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1581
    assert p.property[0] == 1.0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1582
    assert p.property[2] == 0.0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1583
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1584
if __name__ == '__main__':
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1585
    import nose
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1586
    nose.main()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1587
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1588
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1589
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1590
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1591
  \frametitle{Testing}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1592
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1593
      \item More details: see library reference and search for nosetest
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1594
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1595
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1596
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1597
\section{Numerics \& Plotting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1598
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1599
\subsection{NumPy Arrays}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1600
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1601
\subsection{Plotting: Matplotlib}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1602
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1603
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1604
  \frametitle{About \texttt{matplotlib}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1605
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1606
  \item Easy to use, scriptable, ``Matlab-like'' 2D plotting
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1607
  \item Publication quality figures and interactive capabilities
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1608
  \item Plots, histograms, power spectra, bar charts, errorcharts,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1609
    scatterplots, etc.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1610
  \item Also does polar plots, maps, contours
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1611
  \item Support for simple \TeX\ markup
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1612
  \item Multiple output backends (images, EPS, SVG, wx, Agg, Tk, GTK)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1613
  \item Cross-platform: Linux, Win32, Mac OS X
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1614
  \item Good idea to use via IPython:  \typ{ipython -pylab}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1615
  \item From scripts use: \typ{import pylab}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1616
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1617
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1618
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1619
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1620
  \frametitle{More information}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1621
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1622
  \item More information here: \url{http://matplotlib.sf.net}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1623
  \item \url{http://matplotlib.sf.net/tutorial.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1624
  \item \url{http://matplotlib.sf.net/screenshots.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1625
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1626
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1627
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1628
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1629
  \frametitle{Basic plotting with \texttt{matplotlib}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1630
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1631
>>> x = arange(0, 2*pi, 0.05)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1632
>>> plot(x, sin(x)) # Same as plot(x, sin(x), 'b-') 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1633
>>> plot(x, sin(x), 'ro')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1634
>>> axis([0,2*pi, -1,1])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1635
>>> xlabel(r'$\chi$', color='g')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1636
>>> ylabel(r'sin($\chi$)', color='r')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1637
>>> title('A simple figure', fontsize=20)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1638
>>> savefig('/tmp/test.eps')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1639
# Multiple plots in one figure
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1640
>>> t = arange(0.0, 5.2, 0.2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1641
# red dashes, blue squares and green triangles
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1642
>>> plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1643
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1644
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1645
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1646
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1647
  \frametitle{Basic plotting \ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1648
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1649
# Set properties of objects:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1650
>>> plot(x, sin(x), linewidth=2.0, color='r')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1651
>>> l, = plot(x, sin(x))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1652
>>> setp(l, linewidth=2.0, color='r')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1653
>>> l.set_linewidth(2.0); l.set_color('r')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1654
>>> draw() # Redraws current figure.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1655
>>> setp(l) # Prints available properties
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1656
>>> close() # Closes the figure.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1657
# Multiple figures:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1658
>>> figure(1); plot(x, sin(x))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1659
>>> figure(2); plot(x, tanh(x))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1660
>>> figure(1); title('Easy as 1,2,3')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1661
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1662
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1663
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1664
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1665
  \frametitle{Basic plotting \ldots}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1666
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1667
>>> figure(1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1668
>>> subplot(211) # Same as subplot(2, 1, 1)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1669
>>> plot(x, cos(5*x)*exp(-x))
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1670
>>> subplot(2, 1, 2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1671
>>> plot(x, cos(5*x), 'r--', label='cosine')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1672
>>> plot(x, sin(5*x), 'g--', label='sine')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1673
>>> legend() # Or legend(['cosine', 'sine'])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1674
>>> text(1,0, '(1,0)')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1675
>>> axes = gca() # Current axis
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1676
>>> fig = gcf() # Current figure
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1677
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1678
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1679
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1680
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1681
\subsection{SciPy}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1682
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1683
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1684
  \frametitle{Using \texttt{SciPy}}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1685
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1686
  \item SciPy is Open Source software for mathematics, science, and
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1687
    engineering
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1688
  \item \typ{import scipy}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1689
  \item Built on NumPy
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1690
  \item Provides modules for statistics, optimization, integration,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1691
    linear algebra, Fourier transforms, signal and image processing,
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1692
    genetic algorithms, ODE solvers, special functions, and more
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1693
  \item Used widely by scientists world over
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1694
  \item Details are beyond the scope of this tutorial
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1695
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1696
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1697
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1698
\section{Standard library}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1699
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1700
\subsection{Quick Tour}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1701
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1702
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1703
  \frametitle{Standard library}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1704
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1705
  \item Very powerful
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1706
  \item ``Batteries included''
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1707
  \item Example standard modules taken from the tutorial
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1708
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1709
    \item Operating system interface: \typ{os}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1710
    \item System, Command line arguments: \typ{sys}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1711
    \item Regular expressions: \typ{re}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1712
    \item Math: \typ{math}, \typ{random}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1713
    \item Internet access: \typ{urllib2}, \typ{smtplib}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1714
    \item Data compression: \typ{zlib}, \typ{gzip}, \typ{bz2},
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1715
      \typ{zipfile}, and \typ{tarfile}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1716
    \item Unit testing: \typ{doctest} and \typ{unittest}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1717
    \item And a whole lot more!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1718
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1719
  \item Check out the Python Library reference:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1720
    \url{http://docs.python.org/lib/lib.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1721
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1722
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1723
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1724
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1725
  \frametitle{Stdlib: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1726
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1727
>>> import os
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1728
>>> os.system('date')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1729
Fri Jun 10 22:13:09 IST 2005
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1730
0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1731
>>> os.getcwd()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1732
'/home/prabhu'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1733
>>> os.chdir('/tmp')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1734
>>> import os
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1735
>>> dir(os)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1736
<returns a list of all module functions>
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1737
>>> help(os)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1738
<extensive manual page from module's docstrings>
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1739
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1740
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1741
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1742
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1743
  \frametitle{Stdlib: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1744
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1745
>>> import sys
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1746
>>> # Print the list of command line args to Python
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1747
... print sys.argv 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1748
['']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1749
>>> import re # Regular expressions
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1750
>>> re.findall(r'\bf[a-z]*', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1751
... 'which foot or hand fell fastest')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1752
['foot', 'fell', 'fastest']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1753
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1754
... 'cat in the the hat')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1755
'cat in the hat'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1756
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1757
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1758
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1759
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1760
  \frametitle{Stdlib: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1761
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1762
>>> import math
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1763
>>> math.cos(math.pi / 4.0)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1764
0.70710678118654757
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1765
>>> math.log(1024, 2)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1766
10.0
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1767
>>> import random
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1768
>>> random.choice(['apple', 'pear', 'banana'])
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1769
'pear'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1770
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1771
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1772
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1773
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1774
  \frametitle{Stdlib: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1775
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1776
>>> import urllib2
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1777
>>> f = urllib2.urlopen('http://www.python.org/')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1778
>>> print f.read(100)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1779
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1780
<?xml-stylesheet href="./css/ht2html
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1781
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1782
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1783
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1784
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1785
  \frametitle{Stdlib: examples}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1786
\begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1787
>>> import zlib
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1788
>>> s = 'witch which has which witches wrist watch'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1789
>>> len(s)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1790
41
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1791
>>> t = zlib.compress(s)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1792
>>> len(t)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1793
37
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1794
>>> zlib.decompress(t)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1795
'witch which has which witches wrist watch'
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1796
>>> zlib.crc32(t)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1797
-1438085031
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1798
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1799
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1800
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1801
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1802
  \frametitle{Summary}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1803
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1804
  \item Introduced Python
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1805
  \item Basic syntax
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1806
  \item Basic types and data structures
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1807
  \item Control flow
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1808
  \item Functions
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1809
  \item Modules
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1810
  \item Exceptions
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1811
  \item Classes
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1812
  \item Standard library
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1813
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1814
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1815
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1816
\end{document}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1817
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1818
\subsection{Basic data structures}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1819
\begin{frame}{Lists}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1820
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1821
    \item \texttt{species = [ 'humans', 'orcs', 'elves', 'dwarves' ]}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1822
    \item \texttt{ ids = [ 107, 109, 124, 141, 142, 144 ]}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1823
    \item \texttt{ oneliners = [ 'I will be back', 'Do or do not! No try!!', 42 ] }
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1824
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1825
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1826
  \begin{block}{List operations}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1827
    ids + [ 100, 102 ]\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1828
    species.append( 'unicorns')\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1829
    print oneliners[ 1 ]\\
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1830
    look up \alert{docs.python.org/tutorial/datastructures.html}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1831
  \end{block}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1832
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1833
\end{document}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1834
\section{Python Tutorial}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1835
\subsection{Preliminaries}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1836
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1837
  \frametitle{Using the interpreter}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1838
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1839
  \item Starting up: \typ{python} or \typ{ipython}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1840
  \item Quitting: \typ{Control-D} or \typ{Control-Z} (on Win32)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1841
  \item Can use it like a calculator
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1842
  \item Can execute one-liners via the \typ{-c} option:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1843
    \typ{python -c "print 'hello world'"}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1844
  \item Other options via \typ{python -h}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1845
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1846
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1847
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1848
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1849
  \frametitle{IPython}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1850
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1851
  \item Recommended interpreter, IPython:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1852
    \url{http://ipython.scipy.org}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1853
  \item Better than the default Python shell
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1854
  \item Supports tab completion by default
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1855
  \item Easier object introspection
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1856
  \item Shell access!
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1857
  \item Command system to allow extending its own behavior
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1858
  \item Supports history (across sessions) and logging
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1859
  \item Can be embedded in your own Python code
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1860
  \item Support for macros
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1861
  \item A flexible framework for your own custom interpreter
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1862
  \item Other miscellaneous conveniences
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1863
  \item We'll get back to this later
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1864
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1865
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1866
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1867
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1868
  \frametitle{Basic IPython features}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1869
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1870
  \item Startup: \verb+ipython [options] files+
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1871
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1872
    \item \verb+ipython [-wthread|-gthread|-qthread]+:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1873
      Threading modes to support wxPython, pyGTK and Qt
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1874
    \item \verb+ipython -pylab+: Support for matplotlib
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1875
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1876
  \item TAB completion:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1877
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1878
    \item Type \verb+object_name.<TAB>+ to see list of options
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1879
    \item Also completes on file and directory names
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1880
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1881
  \item \verb+object?+ shows docstring/help for any Python object
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1882
  \item \verb+object??+ presents more docs (and source if possible)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1883
  \item Debugging with \verb+%pdb+ magic: pops up pdb on errors
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1884
  \item Access history (saved over earlier sessions also)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1885
    \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1886
    \item Use \texttt{<UpArrow>}: move up history
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1887
    \item Use \texttt{<Ctrl-r> string}: search history backwards
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1888
    \item Use \texttt{Esc >}: get back to end of history
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1889
    \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1890
  \item \verb+%run [options] file[.py]+ lets you run Python code
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1891
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1892
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1893
% LocalWords:  BDFL Guido Rossum PSF Nokia OO Zope CMS RedHat SciPy MayaVi spam
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1894
% LocalWords:  IPython ipython stdin TypeError dict int elif PYTHONPATH IOError
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1895
% LocalWords:  namespace Namespaces SyntaxError ZeroDivisionError NameError str
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1896
% LocalWords:  ValueError subclassed def
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1897
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1898
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1899
  \item Types are of two kinds: \alert{mutable} and \alert{immutable}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1900
  \item Immutable types: numbers, strings, \typ{None} and tuples
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1901
  \item Immutables cannot be changed ``in-place''
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1902
  \item Mutable types: lists, dictionaries, instances, etc.
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1903
  \item Mutable objects can be ``changed''
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1904
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1905
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1906
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1907
\begin{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1908
  \frametitle{Important!}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1909
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1910
    \item Assignment to an object is by reference
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1911
    \item Essentially, \alert{names are bound to objects}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1912
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1913
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1914
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1915
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1916
\end{document}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1917
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1918
  \frametitle{Dictionaries}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1919
  \begin{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1920
  \item Associative arrays/mappings
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1921
  \item Indexed by ``keys'' (keys must be immutable)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1922
  \item \typ{dict[key] = value}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1923
  \item \typ{keys()} returns all keys of the dict
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1924
  \item \typ{values()} returns the values of the dict
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1925
  \item \verb+has_key(key)+ returns if \typ{key} is in the dict
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1926
  \end{itemize}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1927
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1928
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1929
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1930
  \frametitle{Dictionaries: example}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1931
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1932
>>> tel = {'jack': 4098, 'sape': 4139}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1933
>>> tel['guido'] = 4127
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1934
>>> tel
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1935
{'sape': 4139, 'guido': 4127, 'jack': 4098}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1936
>>> tel['jack']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1937
4098
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1938
>>> del tel['sape']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1939
>>> tel['irv'] = 4127
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1940
>>> tel
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1941
{'guido': 4127, 'irv': 4127, 'jack': 4098}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1942
>>> tel.keys()
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1943
['guido', 'irv', 'jack']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1944
>>> tel.has_key('guido')
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1945
True
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1946
  \end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1947
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1948
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1949
\subsection{Control flow, functions}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1950
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1951
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1952
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1953
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1954
  \frametitle{\typ{If} example}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1955
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1956
>>> a = ['cat', 'window', 'defenestrate']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1957
>>> if 'cat' in a:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1958
...    print "meaw"
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1959
...
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1960
meaw
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1961
>>> pets = {'cat': 1, 'dog':2, 'croc': 10}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1962
>>> if 'croc' in pets:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1963
...    print pets['croc']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1964
...
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1965
10
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1966
  \end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1967
\end{frame}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1968
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1969
\begin{frame}[fragile]
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1970
  \frametitle{\typ{for} example}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1971
  \begin{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1972
>>> a = ['cat', 'window', 'defenestrate']
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1973
>>> for x in a:
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1974
...     print x, len(x)
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1975
...
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1976
cat 3
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1977
window 6
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1978
defenestrate 12
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1979
>>> knights = {'gallahad': 'the pure', 
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1980
... 'robin': 'the brave'}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1981
>>> for k, v in knights.iteritems():
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1982
...     print k, v
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1983
...
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1984
gallahad the pure
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1985
robin the brave
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1986
\end{lstlisting}
9243d75024cc Initial data added.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
  1987
\end{frame}