# HG changeset patch # User Puneeth Chaganti # Date 1257308581 -19800 # Node ID 28d4714a97021026bab967b5c165cdcfca34e7d1 # Parent ac31e2f3754e77a10d8619bf5cd50ddbfbdec0eb# Parent 8a4a1e5aec857cc567aaedcdcc2ae25140bf11ae Merged with mainline. diff -r ac31e2f3754e -r 28d4714a9702 day1/data/interpolate.png Binary file day1/data/interpolate.png has changed diff -r ac31e2f3754e -r 28d4714a9702 day1/data/missing_points.png Binary file day1/data/missing_points.png has changed diff -r ac31e2f3754e -r 28d4714a9702 day1/links.tex --- a/day1/links.tex Wed Nov 04 09:33:37 2009 +0530 +++ b/day1/links.tex Wed Nov 04 09:53:01 2009 +0530 @@ -1,6 +1,7 @@ \documentclass[12pt]{article} \title{Links and References} -\author{Asokan Pichai\\Prabhu Ramachandran} +\author{FOSSEE} +\date{} \begin{document} \maketitle \begin{itemize} @@ -8,7 +9,9 @@ \item ``may be one of the thinnest programming language books on my shelf, but it's also one of the best.'' -- \emph{Slashdot, AccordianGuy, September 8, 2004}- available at \url{http://diveintopython.org/} \item How to Think Like a Computer Scientist: Learning with Python available at \url{http://www.openbookproject.net/thinkcs/python/english/}\\``The concepts covered here apply to all programming languages and to problem solving in general.'' -- \emph{Guido van Rossum, creator of Python} \item Some assorted articles related to Python \url{http://effbot.org/zone/index.htm} + \item Reference manual to describe the standard libraries that are distributed with Python is available at \url{http://docs.python.org/library/} \item To read more on strings refer to: \\ \url{http://docs.python.org/library/stdtypes.html#string-methods} + \item Some coding conventions for using Python language are available at \\ \url{http://www.python.org/dev/peps/pep-0008/} \item For documentation on IPython refer: \\ \url{http://ipython.scipy.org/moin/Documentation} \item Documentation for Numpy and Scipy is available at: \url{http://docs.scipy.org/doc/} \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \url{http://www.scipy.org/Cookbook/} diff -r ac31e2f3754e -r 28d4714a9702 day1/session2.tex --- a/day1/session2.tex Wed Nov 04 09:33:37 2009 +0530 +++ b/day1/session2.tex Wed Nov 04 09:53:01 2009 +0530 @@ -260,6 +260,7 @@ \end{lstlisting} \end{frame} +%% more on list slicing \begin{frame}[fragile] \frametitle{List operations} \begin{lstlisting} diff -r ac31e2f3754e -r 28d4714a9702 day1/session3.tex --- a/day1/session3.tex Wed Nov 04 09:33:37 2009 +0530 +++ b/day1/session3.tex Wed Nov 04 09:53:01 2009 +0530 @@ -581,6 +581,7 @@ \end{itemize} \end{frame} +%%making vander without vander, simple matrix \subsection{Van der Monde matrix generation} \begin{frame}[fragile] \frametitle{Van der Monde Matrix} diff -r ac31e2f3754e -r 28d4714a9702 day1/session4.tex --- a/day1/session4.tex Wed Nov 04 09:33:37 2009 +0530 +++ b/day1/session4.tex Wed Nov 04 09:53:01 2009 +0530 @@ -128,45 +128,68 @@ \begin{frame} \frametitle{Matrices: Introduction} -We looked at the Van der Monde matrix in the previous session,\\ -let us now look at matrices in a little more detail. +Let us now look at matrices in detail.\\ +\alert{All matrix operations are done using \kwrd{arrays}} \end{frame} \subsection{Initializing} \begin{frame}[fragile] \frametitle{Matrices: Initializing} \begin{lstlisting} -In []: A = matrix([[ 1, 1, 2, -1], - [ 2, 5, -1, -9], - [ 2, 1, -1, 3], - [ 1, -3, 2, 7]]) +In []: A = array([[ 1, 1, 2, -1], + [ 2, 5, -1, -9], + [ 2, 1, -1, 3], + [ 1, -3, 2, 7]]) In []: A Out[]: -matrix([[ 1, 1, 2, -1], - [ 2, 5, -1, -9], - [ 2, 1, -1, 3], - [ 1, -3, 2, 7]]) +array([[ 1, 1, 2, -1], + [ 2, 5, -1, -9], + [ 2, 1, -1, 3], + [ 1, -3, 2, 7]]) \end{lstlisting} \end{frame} +\begin{frame}[fragile] + \frametitle{Accessing elements of matrices} +\begin{small} + \begin{lstlisting} +In []: C = array([[1,1,2], + [2,4,1], + [-1,3,7]]) +In []: C[1,2] +Out[]: 1 + +In []: C[1] +Out[]: array([2, 4, 1]) + +In []: C[1,1] = -2 +In []: C +Out[]: +array([[ 1, 1, 2], + [ 2, -2, 1], + [-1, 3, 7]]) + \end{lstlisting} +\end{small} +\end{frame} + \subsection{Basic Operations} \begin{frame}[fragile] \frametitle{Transpose of a Matrix} \begin{lstlisting} -In []: linalg.transpose(A) +In []: A.T Out[]: -matrix([[ 1, 2, 2, 1], - [ 1, 5, 1, -3], - [ 2, -1, -1, 2], - [-1, -9, 3, 7]]) +array([[ 1, 2, 2, 1], + [ 1, 5, 1, -3], + [ 2, -1, -1, 2], + [-1, -9, 3, 7]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Sum of all elements} \begin{lstlisting} -In []: linalg.sum(A) +In []: sum(A) Out[]: 12 \end{lstlisting} \end{frame} @@ -174,41 +197,56 @@ \begin{frame}[fragile] \frametitle{Matrix Addition} \begin{lstlisting} -In []: B = matrix([[3,2,-1,5], - [2,-2,4,9], - [-1,0.5,-1,-7], - [9,-5,7,3]]) -In []: linalg.add(A,B) +In []: B = array([[3,2,-1,5], + [2,-2,4,9], + [-1,0.5,-1,-7], + [9,-5,7,3]]) +In []: A + B Out[]: -matrix([[ 4. , 3. , 1. , 4. ], - [ 4. , 3. , 3. , 0. ], - [ 1. , 1.5, -2. , -4. ], - [ 10. , -8. , 9. , 10. ]]) +array([[ 4. , 3. , 1. , 4. ], + [ 4. , 3. , 3. , 0. ], + [ 1. , 1.5, -2. , -4. ], + [ 10. , -8. , 9. , 10. ]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] +\frametitle{Elementwise Multiplication} +\begin{lstlisting} +In []: A*B +Out[]: +array([[ 3. , 2. , -2. , -5. ], + [ 4. , -10. , -4. , -81. ], + [ -2. , 0.5, 1. , -21. ], + [ 9. , 15. , 14. , 21. ]]) + +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] \frametitle{Matrix Multiplication} \begin{lstlisting} -In []: linalg.multiply(A, B) +In []: dot(A,B) Out[]: -matrix([[ 3. , 2. , -2. , -5. ], - [ 4. , -10. , -4. , -81. ], - [ -2. , 0.5, 1. , -21. ], - [ 9. , 15. , 14. , 21. ]]) +array([[ -6. , 6. , -6. , -3. ], + [-64. , 38.5, -44. , 35. ], + [ 36. , -13.5, 24. , 35. ], + [ 58. , -26. , 34. , -15. ]]) \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Inverse of a Matrix} +\begin{lstlisting} +In []: inv(A) +\end{lstlisting} \begin{small} \begin{lstlisting} -In []: linalg.inv(A) Out[]: -matrix([[-0.5 , 0.55, -0.15, 0.7 ], - [ 0.75, -0.5 , 0.5 , -0.75], - [ 0.5 , -0.15, -0.05, -0.1 ], - [ 0.25, -0.25, 0.25, -0.25]]) +array([[-0.5 , 0.55, -0.15, 0.7 ], + [ 0.75, -0.5 , 0.5 , -0.75], + [ 0.5 , -0.15, -0.05, -0.1 ], + [ 0.25, -0.25, 0.25, -0.25]]) \end{lstlisting} \end{small} \end{frame} @@ -217,24 +255,25 @@ \frametitle{Determinant} \begin{lstlisting} In []: det(A) -Out[66]: 80.0 +Out[]: 80.0 \end{lstlisting} \end{frame} +%%use S=array(X,Y) \begin{frame}[fragile] -\frametitle{Eigen Values and Eigen Matrix} +\frametitle{Eigenvalues and Eigen Vectors} \begin{small} \begin{lstlisting} -In []: E = matrix([[3,2,4],[2,0,2],[4,2,3]]) +In []: E = array([[3,2,4],[2,0,2],[4,2,3]]) -In []: linalg.eig(E) +In []: eig(E) Out[]: (array([-1., 8., -1.]), - matrix([[-0.74535599, 0.66666667, -0.1931126 ], + array([[-0.74535599, 0.66666667, -0.1931126 ], [ 0.2981424 , 0.33333333, -0.78664085], [ 0.59628479, 0.66666667, 0.58643303]])) -In []: linalg.eigvals(E) +In []: eigvals(E) Out[]: array([-1., 8., -1.]) \end{lstlisting} \end{small} @@ -243,23 +282,23 @@ \begin{frame}[fragile] \frametitle{Computing Norms} \begin{lstlisting} -In []: linalg.norm(E) +In []: norm(E) Out[]: 8.1240384046359608 \end{lstlisting} \end{frame} \begin{frame}[fragile] - \frametitle{Single Value Decomposition} + \frametitle{Singular Value Decomposition} \begin{small} \begin{lstlisting} -In [76]: linalg.svd(E) -Out[76]: -(matrix( +In []: svd(E) +Out[]: +(array( [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], [ -3.33333333e-01, -8.94427191e-01, -2.98142397e-01], [ -6.66666667e-01, 4.47213595e-01, -5.96284794e-01]]), array([ 8., 1., 1.]), - matrix([[-0.66666667, -0.33333333, -0.66666667], + array([[-0.66666667, -0.33333333, -0.66666667], [-0. , 0.89442719, -0.4472136 ], [-0.74535599, 0.2981424 , 0.59628479]])) \end{lstlisting} @@ -289,12 +328,12 @@ \frametitle{Solving using Matrices} Let us now look at how to solve this using \kwrd{matrices} \begin{lstlisting} - In []: A = matrix([[3,2,-1], - [2,-2,4], - [-1, 0.5, -1]]) - In []: b = matrix([[1], [-2], [0]]) - In []: x = linalg.solve(A, b) - In []: Ax = dot(A, x) + In []: A = array([[3,2,-1], + [2,-2,4], + [-1, 0.5, -1]]) + In []: b = array([[1], [-2], [0]]) + In []: x = solve(A, b) + In []: Ax = dot(A,x) \end{lstlisting} \end{frame} @@ -314,9 +353,9 @@ \begin{lstlisting} In []: Ax Out[]: -matrix([[ 1.00000000e+00], - [ -2.00000000e+00], - [ 2.22044605e-16]]) +array([[ 1.00000000e+00], + [ -2.00000000e+00], + [ 2.22044605e-16]]) \end{lstlisting} \begin{block}{} The last term in the matrix is actually \alert{0}!\\ @@ -332,7 +371,74 @@ \subsection{Exercises} \begin{frame}[fragile] -\frametitle{Problem Set 4: Problem 4.1} +\frametitle{Problem 1} +Given the matrix:\\ +\begin{center} +$\begin{bmatrix} +-2 & 2 & 3\\ + 2 & 1 & 6\\ +-1 &-2 & 0\\ +\end{bmatrix}$ +\end{center} +Find: +\begin{itemize} + \item[i] Transpose + \item[ii]Inverse + \item[iii]Determinant + \item[iv] Eigenvalues and Eigen vectors + \item[v] Singular Value decomposition +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Problem 2} +Given +\begin{center} +A = +$\begin{bmatrix} +-3 & 1 & 5 \\ +1 & 0 & -2 \\ +5 & -2 & 4 \\ +\end{bmatrix}$ +, B = +$\begin{bmatrix} +0 & 9 & -12 \\ +-9 & 0 & 20 \\ +12 & -20 & 0 \\ +\end{bmatrix}$ +\end{center} +Find: +\begin{itemize} + \item[i] Sum of A and B + \item[ii]Elementwise Product of A and B + \item[iii] Matrix product of A and B +\end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Solution} +Sum: +$\begin{bmatrix} +-3 & 10 & 7 \\ +-8 & 0 & 18 \\ +17 & -22 & 4 \\ +\end{bmatrix}$ +,\\ Elementwise Product: +$\begin{bmatrix} +0 & 9 & -60 \\ +-9 & 0 & -40 \\ +60 & 40 & 0 \\ +\end{bmatrix}$ +,\\ Matrix product: +$\begin{bmatrix} +51 & -127 & 56 \\ +-24 & 49 & -12 \\ +66 & -35 & -100 \\ +\end{bmatrix}$ +\end{frame} + +\begin{frame}[fragile] +\frametitle{Problem 3} Solve the set of equations: \begin{align*} x + y + 2z -w & = 3\\ @@ -345,37 +451,30 @@ \begin{frame}[fragile] \frametitle{Solution} -Solution: -\begin{lstlisting} +Use \kwrd{solve()} \begin{align*} x & = -5\\ y & = 2\\ z & = 3\\ w & = 0\\ \end{align*} -\end{lstlisting} -\end{frame} - -\begin{frame}[fragile] -\frametitle{Problem 4.2} - \end{frame} \section{Summary} \begin{frame} - \frametitle{Summary} -So what did we learn?? + \frametitle{What did we learn??} \begin{itemize} \item Matrices \begin{itemize} + \item Accessing elements \item Transpose \item Addition \item Multiplication \item Inverse of a matrix \item Determinant - \item Eigen values and Eigen matrix + \item Eigenvalues and Eigen vector \item Norms - \item Single Value Decomposition + \item Singular Value Decomposition \end{itemize} \item Solving linear equations \end{itemize} diff -r ac31e2f3754e -r 28d4714a9702 day1/session5.tex diff -r ac31e2f3754e -r 28d4714a9702 day1/session6.tex diff -r ac31e2f3754e -r 28d4714a9702 day1quiz1.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day1quiz1.tex Wed Nov 04 09:53:01 2009 +0530 @@ -0,0 +1,160 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2005-2009, FOSSEE Team +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\documentclass[14pt,compress]{beamer} + +\mode +{ + \useoutertheme{split} + \setbeamercovered{transparent} +} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros + +\newcounter{qno} +\setcounter{qno}{0} +\newcommand{\incqno}{\addtocounter{qno}{1}{Question \theqno}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Basic Python]{Python: Quiz} + +\author[FOSSEE Team] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {31, October 2009\\Day 1, Quiz 1} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Write your details...} +On the top right hand corner please write down the following: + \begin{itemize} + \item Name: + \item Affliation: + \item Occupation: + \end{itemize} +\end{frame} + +\begin{frame} +\frametitle{\incqno } + A sample line from a Comma Separated Values (CSV) file:\\ + \vspace*{0.2in} + \emph{Rossum, Guido, 42, 56, 34, 54}\\ + \vspace*{0.2in} + What method would you use to separate the line into fields? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } + \begin{lstlisting} + In [1]: a = [1, 2, 5, 9] + In [2]: a[:-1] + \end{lstlisting} + What is the output? +\end{frame} + +\begin{frame} +\frametitle{\incqno } + How do you combine the two lists \emph{a} and \emph{b}? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } + \begin{lstlisting} + In [1]: d = { + 'a': 1, + 'b': 2 + } + In [2]: print d['c'] + \end{lstlisting} + What is the output? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } + \begin{lstlisting} + for x in "abcd": + print x + + a + b + c + d + \end{lstlisting} + How do you get the following output? + \begin{lstlisting} + 0 a + 1 b + 2 c + 3 d + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + In [1]: x + array([[0, 1, 2], + [3, 4, 5], + [6, 7, 8]]) + In [2]: x[::-1,:] +\end{lstlisting} +\end{frame} + +\begin{frame} +\frametitle{\incqno } +How to read and print each line of a file. +\end{frame} + +\begin{frame} +\frametitle{\incqno } +How to get list of third column of a data file. +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What is the output of: +\begin{lstlisting} +In []: x=linspace(0 , 2 * pi) +In []: plot(x, cos(x),'go') +\end{lstlisting} +\end{frame} + +\begin{frame} +\frametitle{\incqno } +Draw a plot with line width 3. +\end{frame} + +\begin{frame} +\frametitle{\incqno } +Setting x and y axis limits. +\end{frame} + +\end{document} + diff -r ac31e2f3754e -r 28d4714a9702 day1quiz2.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day1quiz2.tex Wed Nov 04 09:53:01 2009 +0530 @@ -0,0 +1,178 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2005-2009, FOSSEE Team +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\documentclass[14pt,compress]{beamer} + +\mode +{ + \useoutertheme{split} + \setbeamercovered{transparent} +} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros + +\newcounter{qno} +\setcounter{qno}{0} +\newcommand{\incqno}{\addtocounter{qno}{1}{Question \theqno}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Basic Python]{Python: Quiz} + +\author[FOSSEE Team] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {11, October 2009\\Day 2} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Write your details...} +On the top right hand corner please write down the following: + \begin{itemize} + \item Name: + \item Affliation: + \item Occupation: + \end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([[1,2,3,4],[3,4,2,5]]) + >>> x.shape + (2, 4) +\end{lstlisting} +Change the shape of \lstinline+x+ to (4,2) +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([[1,2,3,4]]) +\end{lstlisting} +How to change the third element of \lstinline+x+ to 0? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + >>> y = arange(3) + >>> x = linspace(0,3,3) + >>> x-y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([0, 1, 2, 3]) + >>> x.shape = 2,2 + >>> x + array([[0, 1], + [2, 3]]) + >>> x[::2,::2] +\end{lstlisting} +What is the output? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + >>> x + array([[0, 1, 2], + [3, 4, 5], + [6, 7, 8]]) + >>> x[::-1,:] +\end{lstlisting} +Hint: +\begin{lstlisting} + >>> x = arange(9) + >>> x[::-1] + array([8, 7, 6, 5, 4, 3, 2, 1, 0]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x + array([[ 0, 1, 2, 3], + [ 4, 5, 6, 7], + [ 8, 9, 10, 11], + [12, 13, 14, 15]]) +\end{lstlisting} +How will you get the following \lstinline+x+? +\begin{lstlisting} + array([[ 5, 7], + [ 9, 11]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array(([1,2,3,4],[2,3,4,5])) + >>> x[-2][-3] = 4 + >>> x +\end{lstlisting} +What will be printed? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the output? +\begin{lstlisting} + >>> y = arange(4) + >>> x = array(([1,2,3,2],[1,3,6,0])) + >>> x + y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> line = plot(x, sin(x)) +\end{lstlisting} +Use the \lstinline+set_linewidth+ method to set width of \lstinline+line+ to 2. +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the output? +\begin{lstlisting} + >>> x = arange(9) + >>> y = arange(9.) + >>> x == y +\end{lstlisting} +\end{frame} + + +\end{document} + diff -r ac31e2f3754e -r 28d4714a9702 day2/session3.tex --- a/day2/session3.tex Wed Nov 04 09:33:37 2009 +0530 +++ b/day2/session3.tex Wed Nov 04 09:53:01 2009 +0530 @@ -95,12 +95,12 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[]{3D data Visualization} +\title[3D Plotting]{3D data Visualization} \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {1, November 2009\\Day 2, Session 3} +\date[] {1 November, 2009\\Day 2, Session 5} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -279,10 +279,9 @@ \end{columns} \begin{lstlisting} -In []: from numpy import * In []: t = linspace(0, 2*pi, 50) In []: u = cos(t) * pi -in []: x, y, z = sin(u), cos(u), sin(t) +In []: x, y, z = sin(u), cos(u), sin(t) \end{lstlisting} \emphbar{\PythonCode{In []: mlab.points3d(x, y, z)}} \end{frame} @@ -319,6 +318,40 @@ \end{frame} \begin{frame}[fragile] + \frametitle{mgrid} + \begin{lstlisting} +In []: mgrid[0:3,0:3] +Out[]: +array([[[0, 0, 0], + [1, 1, 1], + [2, 2, 2]], + + [[0, 1, 2], + [0, 1, 2], + [0, 1, 2]]]) + +In []: mgrid[-1:1:5j] +Out[]: array([-1., -0.5, 0., 0.5, 1.]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Example} + \begin{lstlisting} +In []: x, y = mgrid[-1:1:5j, -1:1:5j] +In []: z = x*x + y*y + +In []: z +Out[]: +array([[ 2. , 1.25, 1. , 1.25, 2. ], + [ 1.25, 0.5 , 0.25, 0.5 , 1.25], + [ 1. , 0.25, 0. , 0.25, 1. ], + [ 1.25, 0.5 , 0.25, 0.5 , 1.25], + [ 2. , 1.25, 1. , 1.25, 2. ]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] \myemph{\Large 2D data: \texttt{mlab.mesh}} \vspace*{0.25in} @@ -328,7 +361,7 @@ \vspace*{0.25in} \begin{lstlisting} -In []: phi, theta = numpy.mgrid[0:pi:20j, +In []: phi, theta = mgrid[0:pi:20j, ... 0:2*pi:20j] In []: x = sin(phi)*cos(theta) In []: y = sin(phi)*sin(theta) @@ -349,7 +382,7 @@ \pgfimage[width=1.5in]{MEDIA/m2/mlab/contour3d}\\ \end{columns} \begin{lstlisting} -In []: x, y, z = ogrid[-5:5:64j, +In []: x, y, z = mgrid[-5:5:64j, ... -5:5:64j, ... -5:5:64j] In []: mlab.contour3d(x*x*0.5 + y*y + diff -r ac31e2f3754e -r 28d4714a9702 day2/session4.tex --- a/day2/session4.tex Wed Nov 04 09:33:37 2009 +0530 +++ b/day2/session4.tex Wed Nov 04 09:53:01 2009 +0530 @@ -95,14 +95,12 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[]{Debugging and \\Test Driven Approach} +\title[Python Development]{Python Development} \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {11, October 2009} -\date[] % (optional) - +\date[] {1 November, 2009\\Day 2, Session 3} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo} @@ -142,13 +140,147 @@ \maketitle \end{frame} +\section{Tests: Getting started} +\begin{frame}[fragile] + \frametitle{gcd revisited!} + \begin{itemize} + \item Open gcd.py + \end{itemize} +\begin{lstlisting} + def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a%b) + + print gcd(15, 65) + print gcd(16, 76) +\end{lstlisting} + \begin{itemize} + \item python gcd.py + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Find lcm using our gcd module} + \begin{itemize} + \item Open lcm.py + \item $lcm = \frac{a*b}{gcd(a,b)}$ + \end{itemize} +\begin{lstlisting} + from gcd import gcd + def lcm(a, b): + return (a * b) / gcd(a, b) + + print lcm(14, 56) +\end{lstlisting} + \begin{itemize} + \item python lcm.py + \end{itemize} + \begin{lstlisting} +5 +4 +56 + \end{lstlisting} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Writing stand-alone module} +Edit gcd.py file to: +\begin{lstlisting} + def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a%b) + + if __name__ == "__main__": + print gcd(15, 65) + print gcd(16, 76) +\end{lstlisting} + \begin{itemize} + \item python gcd.py + \item python lcm.py + \end{itemize} +\end{frame} + +\begin{frame}[fragile] + \frametitle{More use of main} + For automating tests. + \begin{lstlisting} +if __name__ == '__main__': + for line in open('numbers.txt'): + numbers = line.split() + x = int(numbers[0]) + y = int(numbers[1]) + result = (int(numbers[2])) + assert gcd(x, y) == result + \end{lstlisting} +\end{frame} + +\section{Coding Style} +\begin{frame}{Readability and Consistency} + \begin{itemize} + \item Readability Counts!\\Code is read more often than its written. + \item Consistency! + \item Know when to be inconsistent. + \end{itemize} +\end{frame} + +\begin{frame}[fragile] \frametitle{A question of good style} + \begin{lstlisting} + amount = 12.68 + denom = 0.05 + nCoins = round(amount/denom) + rAmount = nCoins * denom + \end{lstlisting} + \pause + \begin{block}{Style Rule \#1} + Naming is 80\% of programming + \end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitle{Code Layout} + \begin{itemize} + \item Indentation + \item Tabs or Spaces?? + \item Maximum Line Length + \item Blank Lines + \item Encodings + \end{itemize} +\end{frame} + +\begin{frame}{Whitespaces in Expressions} + \begin{itemize} + \item When to use extraneous whitespaces?? + \item When to avoid extra whitespaces?? + \item Use one statement per line + \end{itemize} +\end{frame} + +\begin{frame}{Comments} + \begin{itemize} + \item No comments better than contradicting comments + \item Block comments + \item Inline comments + \end{itemize} +\end{frame} + +\begin{frame}{Docstrings} + \begin{itemize} + \item When to write docstrings? + \item Ending the docstrings + \item One liner docstrings + \end{itemize} +More information at PEP8: http://www.python.org/dev/peps/pep-0008/ +\inctime{5} +\end{frame} \section{Debugging} \subsection{Errors and Exceptions} \begin{frame}[fragile] \frametitle{Errors} \begin{lstlisting} ->>> while True print 'Hello world' +In []: while True print 'Hello world' \end{lstlisting} \pause \begin{lstlisting} @@ -162,7 +294,7 @@ \begin{frame}[fragile] \frametitle{Exceptions} \begin{lstlisting} ->>> print spam +In []: print spam \end{lstlisting} \pause \begin{lstlisting} @@ -175,7 +307,7 @@ \begin{frame}[fragile] \frametitle{Exceptions} \begin{lstlisting} ->>> 1 / 0 +In []: 1 / 0 \end{lstlisting} \pause \begin{lstlisting} @@ -186,6 +318,44 @@ \end{lstlisting} \end{frame} +\begin{frame}[fragile] + \frametitle{Handling Exceptions} + Python uses \typ{try} and \typ{except} clause. + %%Revisiting the raw\_input + \begin{lstlisting} +a = raw_input('Enter number(Q to quit):') +try: + num = int(a) + print num +except: + if a == 'Q': + print 'Exiting...' + else: + print 'Wrong input!' + \end{lstlisting} + + +\end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{Solving it with \typ{try} and \typ{except}} +%% \vspace{-0.2in} +%% \begin{lstlisting} +%% highest = 0 +%% for record in open('sslc1.txt'): +%% fields = record.split(';') +%% try: +%% total = 0 +%% for score_str in fields[3:8]: +%% score = int(score_str) +%% total += score +%% if total > highest: +%% highest = total +%% except: +%% pass +%% print highest +%% \end{lstlisting} +%% \end{frame} \subsection{Strategy} \begin{frame}[fragile] \frametitle{Debugging effectively} @@ -209,8 +379,8 @@ \frametitle{Debugging in IPython} \small \begin{lstlisting} -In [1]: import mymodule -In [2]: mymodule.test() +In []: import mymodule +In []: mymodule.test() --------------------------------------------- NameError Traceback (most recent call last) in () @@ -219,7 +389,7 @@ ----> 2 print spam NameError: global name 'spam' is not defined -In [3]: %debug +In []: %debug > mymodule.py(2)test() 0 print spam ipdb> @@ -232,19 +402,21 @@ \frametitle{Debugging: Exercise} \small \begin{lstlisting} -import keyword -f = open('/path/to/file') +science = {} + +for record in open('sslc1.txt'): + fields = record.split(';') + region_code = fields[0].strip() -freq = {} -for line in f: - words = line.split() - for word in words: - key = word.strip(',.!;?()[]: ') - if keyword.iskeyword(key): - value = freq[key] - freq[key] = value + 1 + score_str = fields[6].strip() + score = int(score_str) if score_str != 'AA' + else 0 -print freq + if score > 90: + science[region_code] += 1 + +pie(science.values(), labels=science.keys()) +savefig('science.png') \end{lstlisting} \inctime{10} \end{frame} @@ -263,7 +435,7 @@ \section{Test Driven Approach} \begin{frame} - \frametitle{Need of Testing!} + \frametitle{Need for Testing!} \begin{itemize} \item Quality @@ -293,17 +465,19 @@ \begin{frame}[fragile] \frametitle{Test for the palindrome: palindrome.py} \begin{lstlisting} -from plaindrome import is_palindrome def test_function_normal_words(): input = "noon" assert is_palindrome(input) == True + +if __name__ == "main'': + test_function_normal_words() \end{lstlisting} \end{frame} \begin{frame}[fragile] \frametitle{Running the tests.} \begin{lstlisting} -$ nosetests test.py +$ nosetests palindrome.py . ---------------------------------------------- Ran 1 test in 0.001s @@ -321,7 +495,7 @@ \end{lstlisting} \vspace*{0.25in} Check\\ - \PythonCode{$ nosetests test.py} \\ + \PythonCode{$ nosetests palindrome.py} \\ \begin{block}{Task} Tweak the code to pass this test. \end{block} @@ -347,37 +521,39 @@ %\end{frame} % -\begin{frame}[fragile] - \frametitle{Exercise} - Based on Euclid's algorithm: - \begin{center} - $gcd(a,b)=gcd(b,b\%a)$ - \end{center} - gcd function can be written as: - \begin{lstlisting} - def gcd(a, b): - if a%b == 0: return b - return gcd(b, a%b) - \end{lstlisting} - \vspace*{-0.15in} - \begin{block}{Task} - \begin{itemize} - \item Write at least - two tests for above mentioned function. - \item Write a non recursive implementation - of gcd(), and test it using already - written tests. - \end{itemize} - \end{block} +%% \begin{frame}[fragile] +%% \frametitle{Exercise} +%% Based on Euclid's algorithm: +%% \begin{center} +%% $gcd(a,b)=gcd(b,b\%a)$ +%% \end{center} +%% gcd function can be written as: +%% \begin{lstlisting} +%% def gcd(a, b): +%% if a%b == 0: return b +%% return gcd(b, a%b) +%% \end{lstlisting} +%% \vspace*{-0.15in} +%% \begin{block}{Task} +%% \begin{itemize} +%% \item Write at least +%% two tests for above mentioned function. +%% \item Write a non recursive implementation +%% of gcd(), and test it using already +%% written tests. +%% \end{itemize} +%% \end{block} -\inctime{15} -\end{frame} +%% \inctime{15} +%% \end{frame} \begin{frame} - \frametitle{We have learned} + \frametitle{Summary} +We have coverd: \begin{itemize} \item Following and Resolving Error Messages. \item Exceptions. + \item Handling exceptions \item Approach for Debugging. \item Writting and running tests. \end{itemize} diff -r ac31e2f3754e -r 28d4714a9702 day2/session5.tex --- a/day2/session5.tex Wed Nov 04 09:33:37 2009 +0530 +++ b/day2/session5.tex Wed Nov 04 09:53:01 2009 +0530 @@ -78,7 +78,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {1, November 2009\\Day 2} +\date[] {1 November, 2009\\Day 2, Session 4} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -111,12 +111,13 @@ \end{frame} \begin{frame}{Problem 1.1} - The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\ + The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. \\For example: +\center{aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.}\\ Write a function that returns the aliquot number of a given number. \end{frame} \begin{frame}{Problem 1.2} - 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.\\ + Pair of numbers (a, b) is said to be \alert{amicable} if aliquot number of a is b and aliquot number of b is a.\\ Example: \texttt{220, 284}\\ Write a program that prints all four digit amicable pairs. diff -r ac31e2f3754e -r 28d4714a9702 day2quiz2.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day2quiz2.tex Wed Nov 04 09:53:01 2009 +0530 @@ -0,0 +1,178 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Tutorial slides on Python. +% +% Author: FOSSEE +% Copyright (c) 2005-2009, FOSSEE Team +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\documentclass[14pt,compress]{beamer} + +\mode +{ + \useoutertheme{split} + \setbeamercovered{transparent} +} + +\definecolor{darkgreen}{rgb}{0,0.5,0} + +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily\bfseries, + commentstyle=\color{red}\itshape, + stringstyle=\color{darkgreen}, + showstringspaces=false, + keywordstyle=\color{blue}\bfseries} + +\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} } + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Macros + +\newcounter{qno} +\setcounter{qno}{0} +\newcommand{\incqno}{\addtocounter{qno}{1}{Question \theqno}} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Title page +\title[Basic Python]{Python: Quiz} + +\author[FOSSEE Team] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date[] {11, October 2009\\Day 2} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\begin{document} + +\begin{frame} + \titlepage +\end{frame} + +\begin{frame} + \frametitle{Write your details...} +On the top right hand corner please write down the following: + \begin{itemize} + \item Name: + \item Affliation: + \item Occupation: + \end{itemize} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([[1,2,3,4],[3,4,2,5]]) + >>> x.shape + (2, 4) +\end{lstlisting} +Change the shape of \lstinline+x+ to (4,2) +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([[1,2,3,4]]) +\end{lstlisting} +How to change the third element of \lstinline+x+ to 0? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + >>> y = arange(3) + >>> x = linspace(0,3,3) + >>> x-y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array([0, 1, 2, 3]) + >>> x.shape = 2,2 + >>> x + array([[0, 1], + [2, 3]]) + >>> x[::2,::2] +\end{lstlisting} +What is the output? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the result? +\begin{lstlisting} + >>> x + array([[0, 1, 2], + [3, 4, 5], + [6, 7, 8]]) + >>> x[::-1,:] +\end{lstlisting} +Hint: +\begin{lstlisting} + >>> x = arange(9) + >>> x[::-1] + array([8, 7, 6, 5, 4, 3, 2, 1, 0]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x + array([[ 0, 1, 2, 3], + [ 4, 5, 6, 7], + [ 8, 9, 10, 11], + [12, 13, 14, 15]]) +\end{lstlisting} +How will you get the following \lstinline+x+? +\begin{lstlisting} + array([[ 5, 7], + [ 9, 11]]) +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> x = array(([1,2,3,4],[2,3,4,5])) + >>> x[-2][-3] = 4 + >>> x +\end{lstlisting} +What will be printed? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the output? +\begin{lstlisting} + >>> y = arange(4) + >>> x = array(([1,2,3,2],[1,3,6,0])) + >>> x + y +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + >>> line = plot(x, sin(x)) +\end{lstlisting} +Use the \lstinline+set_linewidth+ method to set width of \lstinline+line+ to 2. +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } +What would be the output? +\begin{lstlisting} + >>> x = arange(9) + >>> y = arange(9.) + >>> x == y +\end{lstlisting} +\end{frame} + + +\end{document} +