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