# HG changeset patch # User Madhusudan.C.S # Date 1262094911 -19800 # Node ID 2214b5dba4d4ee98f1d7083fee6f34b10111c536 # Parent 25b18b51be4174fbfbc27d589a5027e0f211776d# Parent c1bfec797af67d7212eedeaaa571edf7aa6afeb9 Merged the branches. diff -r 25b18b51be41 -r 2214b5dba4d4 day1/cheatsheet1.tex --- a/day1/cheatsheet1.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/cheatsheet1.tex Tue Dec 29 19:25:11 2009 +0530 @@ -6,7 +6,8 @@ \usepackage{listings} \lstset{language=Python, basicstyle=\ttfamily, -commentstyle=\itshape\bfseries +commentstyle=\itshape\bfseries, +showstringspaces=false, } \newcommand{\typ}[1]{\lstinline{#1}} \usepackage[english]{babel} @@ -33,49 +34,118 @@ \begin{lstlisting} In [2]: (Ctrl-D)^D Do you really want to exit ([y]/n)? y -\end{lstlisting} +\end{lstlisting} %$ \section{Plotting} +\subsection{linspace} +\typ{In []: x = linspace(start, stop, num)}\\ +\typ{linspace} returns array of length \typ{num}, for which \typ{x[0] = start} and \typ{x[num-1] = stop} \\ +\emph{Please note indices of array starts from zero(0)} - \begin{lstlisting} -In [1]: x = linspace(0, 2*pi, 50) -In [2]: plot(x, sin(x)) -In [3]: xlabel('x') -In [4]: ylabel('sin(x)') -In [5]: title('Sinusoids') -In [6]: legend(['sin(y)']) -In [7]: legend(['sin(2y)'], loc = 'center') -# loc = 'upper right', 'upper left', 'lower left, 'lower right', 'center left', -# 'center right', 'lower center', 'upper center', 'best', 'right', 'center' +\subsection{plot} +\typ{In []: plot(X, Y)}\\ +For given arrays of equal length(above case X and Y), \typ{plot} plots the corresponding *x* and *y* pairs taken from X and Y. -In [8]: legend(['sin(2y)'], loc = (.8, .1)) +\subsection{Colors of plots} +\typ{In []: plot(y, sin(y), 'g')}\\ +Plots graph with green color. Other options available are: +\begin{lstlisting} + 'r' ---> Red + 'b' ---> Blue + 'r' ---> Red + 'c' ---> Cyan + 'm' ---> Magenta + 'y' ---> Yellow + 'k' ---> Black + 'w' ---> White +\end{lstlisting} +One can set the width of the plotline using optional argument \typ{linewidth}. For example:\\ +\typ{In []: plot(x, cos(x), 'r', linewidth=2)}\\ +Plots the line with linewidth = 2 +\subsection{label and title} +\typ{In []: xlabel('Length') #sets *x* axis label to Length}\\ +\typ{In []: ylabel('Time') #sets *y* axis label to Time.}\\ +\typ{In []: title('Sinusoid') #sets title of plot}\\ +\\ +\textbf{Additionally}\\ +Pylab accepts TeX equation expressions in any text expression. To get something like:\\ +$\sigma_i=15$ \\ +on title of figure use: +\begin{lstlisting} +In []: title('$\sigma_i=15$') +\end{lstlisting} +Same way one can have TeX expression on xlabel, ylabel etc. -In [9]: savefig('sin.png') # Save figure -In [10]: close() # Closes the figure +\subsection{legends} +\typ{In []: legend('sin(x)',loc=center)} \\ +Place a legend on the current plot at location *loc*.\\ +Apart from \typ{center}, some other \typ{loc} which can be specified are: +\begin{lstlisting} +'best' +'right' +'upper right' +'upper left' +'lower left' +'lower right' +'center left' +'center right' +'lower center' +'upper center' +\end{lstlisting} +\newpage +One can also mention explicit co-ordinates for placement of legend. +\begin{lstlisting} +In []: legend(['sin(2y)'], loc=(.8,.1)) +\end{lstlisting} +\typ{loc = 0, 1} (top left position of graph)\\ +\typ{loc = 0.5, 0.5} (center of graph). -In [11]: clf() # Clears the Plot area +\subsection{Annotate} +\typ{In []: annotate('local max', xy=(1.5, 1))}\\ +Annotates current plot with text, 'local max', at position specified to \typ{xy}. -In [12]: plot(y, sin(y), 'g') -# Colors can be: 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w' +\subsection{Saving figures} +\typ{In []: savefig('sinusoids.png')}\\ +Saves the current figure with file name 'sinusoids.png' in current working directory. One can save figure in any of these formats: png, pdf, ps, eps and svg. -In [13]: plot(y, cos(y), 'r', linewidth=2) - -In [14]: legend(['x', '-x']) -In [15]: annotate('origin', xy=(0, 0)) +\subsection{Miscellaneous} +\typ{In []: clf() #Clears the current plot area}\\ +\typ{In []: close() #Closes the figure} +\section{Saving and running scripts} +\begin{itemize} + \item \typ{\%hist}\\ + It returns the logs of all commands(including mistakes) used in IPython interpreter. + \item \typ{\%hist -n}\\ +It disables the line number representation of logs. + \item \typ{\%save four\_plot.py 16 18-27}\\ +For creating a script named four\_plot which includes line 16 and line 18 to 27 of logs. + \item \typ{\%run -i four\_plot.py}\\ +Running the python script inside IPython interpreter. +\end{itemize} -In [16]: xmin, xman = xlim() # Without arguments gets -In [17]: ymin, ymax = ylim() # values +\section{Example} + \begin{lstlisting} +In []: x = linspace(0, 2*pi, 50) +In []: plot(x, sin(x), 'g') +In []: plot(x, cos(x), 'r', linewidth=2) +In []: xlabel('x') +In []: title('Sinusoidal Waves') +In []: legend(['sin(x)', 'cos(x)']) +In []: annotate('origin', xy=(0, 0)) +In []: xmin, xman = xlim() # returns current X axis limits. +In []: ymin, ymax = ylim() +In []: xlim(0, 2 * pi) # sets the X axis limits to passed values +In []: ylim(ymin - 0.2, ymax + 0.2) -In [18]: xlim(0, 2 * pi) # With values, sets the -In [19]: ylim(ymin - 0.2, ymax + 0.2) # specified values +In []: savefig('sin.png') # Save figure +In []: close() \end{lstlisting} -\section{Saving and running scripts} +\section{References} \begin{itemize} - \item \typ{\%hist} - \item \typ{\%save four\_plot.py 16 18-27} - \item \typ{\%run -i four\_plot.py} + \item For documentation on IPython refer: \\ http://ipython.scipy.org/moin/Documentation + \item Plotting(matplotlib) related documentation are available at:\\ http://matplotlib.sourceforge.net/contents.html + \item Explore examples and plots based on matplotlib at \\ http://matplotlib.sourceforge.net/examples/index.html \end{itemize} - \end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/cheatsheet2.tex --- a/day1/cheatsheet2.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/cheatsheet2.tex Tue Dec 29 19:25:11 2009 +0530 @@ -25,87 +25,135 @@ \LARGE{Plotting Points}\\ \large{FOSSEE} \end{center} -\section{Plotting from Data files} -\begin{verbatim} -l = [] #Empty List -t = [] -for line in open('pendulum.txt'): # Opening & Reading files - points = line.split() # Splitting a string - l.append(float(points[0])) # Appending to a list - t.append(float(points[1])) -tsq = [] -for time in t: #Iterating through lists - tsq.append(t*t) -plot(l, tsq, '.') # Plotting points -\end{verbatim} + \section{Plotting Points with Lists} \begin{lstlisting} -In [1]: x = [0, 1, 2, 3] -In [2]: y = [7, 11, 15, 19] -In [3]: plot(x, y) -In [4]: clf() -In [5]: plot(x, y, 'o') # Plotting Circles -#Dots - '.', #Dashed lines - '--' #Lines - '-' +In []: x = [0, 1, 2, 3] # Creating a list +In []: y = [7, 11, 15, 19] +In []: plot(x, y) +In []: clf() +In []: plot(x, y, 'o') # Plotting Circles \end{lstlisting} +\subsection{Line style/marker} +\begin{lstlisting} +The following format string characters are accepted +to control the line style or marker: + + ================ =============================== + character description + ================ =============================== + '-' solid line style + '--' dashed line style + '-.' dash-dot line style + ':' dotted line style + '.' point marker + ',' pixel marker + 'o' circle marker + 'v' triangle_down marker + '^' triangle_up marker + '<' triangle_left marker + '>' triangle_right marker + '1' tri_down marker + '2' tri_up marker + '3' tri_left marker + '4' tri_right marker + 's' square marker + 'p' pentagon marker + '*' star marker + 'h' hexagon1 marker + 'H' hexagon2 marker + '+' plus marker + 'x' x marker + 'D' diamond marker + 'd' thin_diamond marker + '|' vline marker + '_' hline marker + ================ =============================== + +\end{lstlisting} + +\subsection{Marker combinations} +\typ{In []: plot(x, y, 'ro')} \\ +This plots figure with red colored filled circles.\\ +Similarly other combination of colors and marker can be used. \section{Lists} Initializing \begin{lstlisting} -In [10]: mtlist = [] # Empty List -In [11]: lst = [ 1, 2, 3, 4, 5] +In []: mtlist = [] # Empty List +In []: lst = [ 1, 2, 3, 4, 5] \end{lstlisting} Slicing \begin{lstlisting} -In [12]: lst[1:3] # A slice. -Out[12]: [2, 3] +In []: lst[1:3] # A slice. +Out[]: [2, 3] -In [13]: lst[1:-1] -Out[13]: [2, 3, 4] +In []: lst[1:-1] +Out[]: [2, 3, 4] \end{lstlisting} -Appending to lists +\subsection{Appending to lists} \begin{lstlisting} -In [14]: a = [ 6, 7, 8, 9] -In [15]: b = lst + a -In [16]: b -Out[16]: [1, 2, 3, 4, 5, 6, 7, 8, 9] +In []: a = [ 6, 7, 8, 9] +In []: b = lst + a +In []: b +Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9] -In [17]: lst.append(6) -In [18]: lst -Out[18]: [ 1, 2, 3, 4, 5, 6] +In []: lst.append(6) +In []: lst +Out[]: [ 1, 2, 3, 4, 5, 6] \end{lstlisting} - -Iterating over a List +\subsection{Iterating over a List} \begin{lstlisting} -In [19]: for each in b: # Iterating over the list, element-wise - ....: print b # Print each element +In []: for element in b: # Iterating over the list, element-wise + ....: print element # Print each element ....: \end{lstlisting} -Splitting Strings +\section{Strings} +\subsection{Splitting Strings} \begin{lstlisting} -In [20]: line = '1.2000e-01 7.4252e-01' -In [21]: point = line.split() # Splits the string at the space -Out[21]: ['1.2000e-01', '7.4252e-01'] +In []: greet = ``hello world'' +In []: print greet.split() +Out[]: ['hello', 'world'] +In []: greet = ``hello, world'' +In []: print greet.split(',') +Out[]: ['hello', ' world'] # Note the whitespace before 'world' +\end{lstlisting} +A string can be split based on the delimiter specified within quotes. A combination of more than one delimiter can also be used.\\ +\typ{In []: greet.split(', ')}\\ +\typ{Out[]: ['hello', 'world']}\\Note the whitespace is not there anymore. +\newpage +\section{Plotting from Files} +\subsection{Opening files} + +\typ{In []: f = open('datafile.txt')}\\By default opens in read mode. \\If file does not exist then it throws an exception\\ +\typ{In []: f = open('datafile.txt','r')}\\Specifying the read mode\\ +\typ{In []: f = open('datafile.txt', 'w')}\\Opens the file in write mode. \\If the file already exists, then it deletes all the previous content and opens. + +\subsection{Reading from files} +Just like lists files are iterable as well. + +\begin{lstlisting} + In []: for line in f: + ...: print line + ...: + ...: \end{lstlisting} -Plotting from Files +\subsection{Plotting} \begin{lstlisting} -In [22]: L = [] -In [23]: T = [] - -#Open a file & operate on each line -In [24]: for line in open('pendulum.txt'): - .... point = line.split() - .... L.append(float(point[0])) - .... T.append(float(point[1])) -In [25]: TSq = [] -In [26]: for t in T: - ....: TSq.append(t*t) - ....: - ....: -In [27]: plot(L, TSq, '.') +l = [] +t = [] +for line in open('pendulum.txt'): + point = line.split() + l.append(float(point[0])) + t.append(float(point[1])) +tsq = [] +for time in t: + tsq.append(time*time) +plot(l, tsq, '.') \end{lstlisting} \end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/cheatsheet3.tex --- a/day1/cheatsheet3.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/cheatsheet3.tex Tue Dec 29 19:25:11 2009 +0530 @@ -69,7 +69,7 @@ \begin{lstlisting} In []: pie(science.values(), labels=science.keys()) \end{lstlisting} -Numpy Arrays +Arrays \begin{lstlisting} In []: a = array([1, 2, 3]) #Creating In []: b = array([4, 5, 6]) @@ -77,7 +77,7 @@ \end{lstlisting} Numpy statistical operations \begin{lstlisting} -In []: mean(math_scores) +In []: mean(math_scores) In []: median(math_scores) In []: std(math_scores) \end{lstlisting} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/cheatsheet4.tex --- a/day1/cheatsheet4.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/cheatsheet4.tex Tue Dec 29 19:25:11 2009 +0530 @@ -24,25 +24,42 @@ \large{FOSSEE} \end{center} \section{Matrices} -Inputting a Matrix +\subsection{Basics} +Matrix Creation\\ +\typ{In []: C = array([[1,1,2], [2,4,1], [-1,3,7]])}\\ +It creates C matrix of shape 3x3\\ +Shape is dimensions of given array. \begin{lstlisting} -In []: C = array([[1,1,2], - [2,4,1], - [-1,3,7]]) -In []: B = ones_like(C) -In []: A = ones((3,2)) -In []: I = identity(3) +In []: C.shape +Out[]: (3, 3) +In []: shape([[1,2],[4,5],[3,0]]) +Out[]: (3, 2) \end{lstlisting} -Accessing Elements +\typ{In []: B = ones_like(C)} \\ +B would be array of ones with the same shape and type as C.\\ +\typ{In []: A = ones((3,2))} \\ +A would be new matrix of given shape(arguments), filled with ones.\\ +\typ{In []: I = identity(3)}\\ +I would be identity matrix of shape 3x3 + +\subsection{Accessing Elements} \begin{lstlisting} +In []: C +Out[]: +array([[ 1, 1, 2], + [ 2, 4, 1], + [-1, 3, 7]]) In []: C[1,2] Out[]: 1 - +\end{lstlisting} +Two indexes seperated by \typ{','} specifies [row, column]. So \typ{C[1,2]} gets third element of second row(indices starts from 0). +\newpage +\begin{lstlisting} In []: C[1] Out[]: array([2, 4, 1]) \end{lstlisting} - -Changing elements +Single index implies complete row. +\subsection{Changing elements} \begin{lstlisting} In []: C[1,1] = -2 In []: C @@ -59,19 +76,27 @@ [-1, 3, 7]]) \end{lstlisting} -Slicing +\subsection{Slicing} +Accessing rows with Matrices is straightforward. But If one wants to access particular Column, or want a sub-matrix, Slicing is the way to go. \begin{lstlisting} In []: C[:,1] Out[]: array([1, 0, 3]) - +\end{lstlisting} +First index(:) specifies row(':' implies all the rows) and second index(1) specifies column(second column). +\begin{lstlisting} In []: C[1,:] Out[]: array([0, 0, 0]) - +\end{lstlisting} +Here we get second row(1), all columns(':') of C matrix. +\newpage +\begin{lstlisting} In []: C[0:2,:] Out[]: array([[1, 1, 2], [0, 0, 0]]) - +\end{lstlisting} +Result is sub-matrix with first and second row(endpoint is excluded), and all columns from C. +\begin{lstlisting} In []: C[1:3,:] Out[]: array([[ 0, 0, 0], @@ -81,7 +106,9 @@ Out[]: array([[1, 1, 2], [0, 0, 0]]) - +\end{lstlisting} +\typ{':2'} => start from first row, till and excluding third row. +\begin{lstlisting} In []: C[1:,:] Out[]: array([[ 0, 0, 0], @@ -92,36 +119,45 @@ array([[ 0, 0], [-1, 3]]) \end{lstlisting} - -Striding +\typ{'1:'} => Start from second row, till last row\\ +\typ{':2'} => Start from first column, till and excluding third column. +\newpage +\subsection{Striding} +Often apart from submatrix, one needs to get some mechanism to jump a step. For example, how can we have all alternate rows of a Matrix. \\ +Following method will return Matrix with alternate rows. \begin{lstlisting} In []: C[::2,:] Out[]: array([[ 1, 1, 2], [-1, 3, 7]]) - +\end{lstlisting} +\typ{C[startR:stopR:stepR,startC:stopC:stepC]} => Syntax of mentioning starting index, ending index, and step to jump.\\ +In above mentioned case, \typ{'::2'} means, start from first row, till last row(both are blank), with step of 2, that is, skipping alternate row. After first row, C[startR], next row would be C[startR+stepR] and so on. +\begin{lstlisting} In []: C[:,::2] Out[]: xarray([[ 1, 2], [ 0, 0], [-1, 7]]) - +\end{lstlisting} +Same as above, just that here we get matrix with each alternate column and all rows. +\begin{lstlisting} In []: C[::2,::2] Out[]: array([[ 1, 2], [-1, 7]]) \end{lstlisting} - -Matrix Operations +\section{Matrix Operations} +For a Matrix A and B of equal shapes. \begin{lstlisting} In []: A.T # Transpose In []: sum(A) # Sum of all elements In []: A+B # Addition -In []: A*B # Product +In []: A*B # Element wise product +In []: dot(A,b) #Matrix multiplication In []: inv(A) # Inverse In []: det(A) # Determinant \end{lstlisting} - Eigen Values and Eigen Vectors \begin{lstlisting} In []: eig(A) #Eigen Values and Vectors @@ -135,14 +171,33 @@ %% \begin{lstlisting} %% In []: svd(A) %% \end{lstlisting} -Least Square Fit Line +\section{Least Square Fit Line} \begin{lstlisting} -In []: A = array([L, ones_like(L)]) -In []: A = A.T +L = [] +T = [] +for line in open('pendulum.txt'): + point = line.split() + L.append(float(point[0])) + T.append(float(point[1])) +Tsq = [] +for time in T: + Tsq.append(time*time) +plot(L, Tsq, '.') +\end{lstlisting} +This is exact curve we get from L Vs Tsq from data.This relation among L and Tsq is not of straight line. For getting Least Square Fit line, we have to solve the relations:\\ +$L=m*Tsq+c$ (something similar to $y=m*x+c$)\\ +For present scenario, we have L and corresponding Tsq values. For finding m and c at given points we use \typ{lstlq} function provided by pylab. It returns the least-squares solution to an equation. \\ +For finding Least Square Fit line for this particular data we have to do following steps:\\ +\typ{In []: A = array([L, ones\_like(L)])}\\ +A is 2x(Length of array L) array. +\begin{lstlisting} +In []: A = A.T #now A.shape = (Length of array L)x2 In []: result = lstsq(A,TSq) In []: coef = result[0] In []: Tline = coef[0]*L + coef[1] -In []: plot(L, Tline) \end{lstlisting} +\typ{coef[0]} is array with all $m$ values, and \typ{coef[1]} contains $c$.\\ +To get the final plot.\\ +\typ{In []: plot(L, Tline)} \end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/cheatsheet6.tex --- a/day1/cheatsheet6.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/cheatsheet6.tex Tue Dec 29 19:25:11 2009 +0530 @@ -1,6 +1,21 @@ \documentclass[12pt]{article} \title{Solving Equations \& ODEs} \author{FOSSEE} +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily, +commentstyle=\itshape\bfseries, +showstringspaces=false, +} +\newcommand{\typ}[1]{\lstinline{#1}} +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} +\usepackage{amsmath} \begin{document} \date{} \vspace{-1in} @@ -9,36 +24,112 @@ \large{FOSSEE} \end{center} \section{Solving linear equations} -\begin{verbatim} - 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) - In []: allclose(Ax, b) - Out[]: True -\end{verbatim} +Condier following sets of equations:\\ + \begin{align*} + 3x + 2y - z & = 1 \\ + 2x - 2y + 4z & = -2 \\ + -x + \frac{1}{2}y -z & = 0 + \end{align*}\\ +The matrix representation is:\\ +\begin{center} +$A*x = B$ +\end{center} +Where A is coefficient matrix(in this case 3x3)\\ +B is constant matrix(1x3)\\ +x is the required solution.\\ +\begin{lstlisting} +In []: A = array([[3,2,-1], [2,-2,4], [-1, 0.5, -1]]) +In []: B = array([[1], [-2], [0]]) +In []: x = solve(A, B) +\end{lstlisting} +Solve the equation $A x = B$ for $x$.\\ +To check whether solution is correct try this: +\begin{lstlisting} +In []: Ax = dot(A,x) #Matrix multiplication of A and x(LHS) +In []: allclose(Ax, B) +Out[]: True +\end{lstlisting} +\typ{allclose} Returns \typ{True} if two arrays(in above case Ax and B) are element-wise equal within a tolerance. +\newpage \section{Finding roots} -\begin{verbatim} - In []: coeffs = [1, 6, 13] - In []: roots(coeffs) -\end{verbatim} -Finding the roots of a function -\begin{verbatim} -In []: fsolve(sin(x)+cos(x)**2, 0) -\end{verbatim} +\subsection{Polynomials} +\begin{center} + $x^2+6x+13=0$ +\end{center} +to find roots, pylab provides \typ{roots} function. +\begin{lstlisting} +In []: coeffs = [1, 6, 13] #list of all coefficients +In []: roots(coeffs) +\end{lstlisting} +\subsection{functions} +Functions can be defined and used by following syntax: +\begin{lstlisting} +def func_name(arg1, arg2): + #function code + return ret_value +\end{lstlisting} +A simple example can be +\begin{lstlisting} +def expression(x): + y = x*sin(x) + return y +\end{lstlisting} +Above function when called with a argument, will return $xsin(x)$ value for that argument. +\begin{lstlisting} +In [95]: expression(pi/2) +Out[95]: 1.5707963267948966 + +In [96]: expression(pi/3) +Out[96]: 0.90689968211710881 +\end{lstlisting} +\subsection{Roots of non-linear equations} +For Finding the roots of a non linear equation(defined as $f(x)=0$), around a starting estimate we use \typ{fsolve}:\\ +\typ{In []: from scipy.optimize import fsolve}\\ +\typ{fsolve} is not a part of \typ{pylab}, instead is a function in the \textbf{optimize} module of \textbf{scipy}, and hence we \textbf{import} it.\\ +%\typ{fsolve} takes first argument as name of function, which evaluates $f(x)$, whose roots one wants to find. And second argument is starting estimate, around which roots are found. +For illustration, we want to find roots of equation: +\begin{center} + $f(x)=sin(x)+cos(x)^2$ +\end{center} +So just like we did above, we define a function: +\begin{lstlisting} +In []: def f(x): + ....: return sin(x)+cos(x)**2 + ....: +\end{lstlisting} +Now to find roots of this non linear equation, around a initial estimate value, say 0, we use \typ{fsolve} in following way: +\begin{lstlisting} +In []: fsolve(f, 0) #arguments are function name and estimate +Out[]: -0.66623943249251527 +\end{lstlisting} + \section{ODE} -\begin{verbatim} - In []: def epid(y, t): + +We wish to solve an (a system of) Ordinary Differential Equation. For this purpose, we shall use \typ{odeint}.\\ +\typ{In []: from scipy.integrate import odeint}\\ +\typ{odeint} is a function in the \textbf{integrate} module of \textbf{scipy}.\\ +As an illustration, let us solve the ODE below:\\ +$\frac{dy}{dt} = ky(L-y)$, L = 25000, k = 0.00003, y(0) = 250\\ +We define a function (as below) that takes $y$ and time as arguments and returns the right hand side of the ODE. +\begin{lstlisting} +In []: def f(y, t): .... k, L = 0.00003, 25000 .... return k*y*(L-y) .... - - In []: t = arange(0, 12, 0.2) - - In []: y = odeint(epid, 250, t) - - In []: plot(t, y) -\end{verbatim} +\end{lstlisting} +Next we define the time over which we wish to solve the ODE. We also note the initial conditions given to us. +\begin{lstlisting} +In []: t = linspace(0, 12, 61) +In []: y0 = 250 +\end{lstlisting} +To solve the ODE, we call the \typ{odeint} function with three arguments - the function \typ{f}, initial conditions and the time vector. +\begin{lstlisting} +In []: y = odeint(f, y0, t) +\end{lstlisting} +Note: To solve a system of ODEs, we need to change the function to return the right hand side of all the equations and the system and the pass the required number of initial conditions to the \typ{odeint} function. +\section{Links and References} +\begin{itemize} +\item Documentation for Numpy and Scipy is available at:\\ http://docs.scipy.org/doc/ + \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \\ http://www.scipy.org/Cookbook/ +\end{itemize} \end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/data/image.png Binary file day1/data/image.png has changed diff -r 25b18b51be41 -r 2214b5dba4d4 day1/data/ode.png Binary file day1/data/ode.png has changed diff -r 25b18b51be41 -r 2214b5dba4d4 day1/exercises.tex --- a/day1/exercises.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/exercises.tex Tue Dec 29 19:25:11 2009 +0530 @@ -78,7 +78,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {7 November, 2009\\Day 1, Session 5} +\date[] {14 December, 2009\\Day 1, Session 5} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/session1.tex --- a/day1/session1.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/session1.tex Tue Dec 29 19:25:11 2009 +0530 @@ -77,7 +77,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {7 November, 2009\\Day 1, Session 1} +\date[] {14 December, 2009\\Day 1, Session 1} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -125,28 +125,28 @@ \begin{frame} \frametitle{Workshop Schedule: Day 1} \begin{description} - \item[Session 1] Sat 09:00--10:00 - \item[Session 2] Sat 10:05--11:05 - \item[Session 3] Sat 11:20--12:20 - \item[Session 4] Sat 12:25--13:25 - \item[Quiz 1] Sat 14:25--14:40 - \item[Session 5] Sat 14:40--15:25 - \item[Session 6] Sat 15:40--16:40 - \item[Quiz 2] Sat 16:45--17:00 + \item[Session 1] Mon 09:00--10:00 + \item[Session 2] Mon 10:05--11:05 + \item[Session 3] Mon 11:20--12:20 + \item[Session 4] Mon 12:25--13:25 + \item[Quiz 1] Mon 14:25--14:40 + \item[Exercises] Mon 14:40--15:25 + \item[Session 5] Mon 15:40--16:40 + \item[Quiz 2] Mon 16:45--17:00 \end{description} \end{frame} \begin{frame} \frametitle{Workshop Schedule: Day 2} \begin{description} - \item[Session 1] Sun 09:00--10:00 - \item[Session 2] Sun 10:05--11:05 - \item[Session 3] Sun 11:20--12:20 - \item[Session 4] Sun 12:25--13:25 - \item[Quiz 1] Sun 14:25--14:40 - \item[Session 5] Sun 14:40--15:25 - \item[Session 6] Sun 15:40--16:40 - \item[Quiz 2] Sun 16:45--17:00 + \item[Session 1] Tue 09:00--10:00 + \item[Session 2] Tue 10:05--11:05 + \item[Session 3] Tue 11:20--12:20 + \item[Session 4] Tue 12:25--13:25 + \item[Quiz 1] Tue 14:25--14:40 + \item[Exercises] Tue 14:40--15:25 + \item[Session 5] Tue 15:40--16:40 + \item[Quiz 2] Tue 16:45--17:00 \end{description} \end{frame} @@ -162,7 +162,7 @@ \begin{block}{Goal: Successful participants will be able to} \begin{itemize} \item Use Python as plotting, computational tool - \item Understand how Python can be used as a scripting and problem solving language. + \item Understand how to use Python as a scripting and problem solving language. \item Train students for the same \end{itemize} \end{block} @@ -186,15 +186,19 @@ \item \typ{sslc_allreg.py} \item \typ{sslc_science.py} \end{itemize} + \item Images + \begin{itemize} + \item \typ{lena.png} + \end{itemize} \end{enumerate} \end{frame} \begin{frame}[fragile] \frametitle{Starting up \ldots} \begin{block}{} -\begin{verbatim} +\begin{lstlisting} $ ipython -pylab -\end{verbatim} +\end{lstlisting} %$ \end{block} \begin{lstlisting} In []: print "Hello, World!" @@ -290,7 +294,9 @@ \frametitle{Another example} \begin{lstlisting} In []: clf() -#Clears the plot area. + \end{lstlisting} +\emphbar{Clears the plot area.} + \begin{lstlisting} In []: y = linspace(0, 2*pi, 50) In []: plot(y, sin(2*y)) In []: xlabel('y') @@ -306,7 +312,6 @@ % \small \begin{lstlisting} In []: title('Sinusoids') -#Sets the title of the figure In []: legend(['sin(2y)']) \end{lstlisting} % \small @@ -329,37 +334,29 @@ \begin{columns} \column{0.6\textwidth} \includegraphics[height=2in, interpolate=true]{data/position} -\begin{lstlisting} -'best', 'right', 'center' -\end{lstlisting} \column{0.45\textwidth} \vspace{-0.2in} \begin{lstlisting} -'upper right' -'upper left' -'lower left' -'lower right' -'center left' -'center right' -'lower center' -'upper center' +'best' +'right' +'center' \end{lstlisting} \end{columns} \end{frame} -\begin{frame}[fragile] - \frametitle{For arbitrary location} -\vspace*{-0.1in} -\begin{lstlisting} -In []: legend(['sin(2y)'], loc=(.8,.1)) -# Specify south-east corner position -\end{lstlisting} -%\vspace*{-0.2in} -\begin{center} - \includegraphics[height=2in, interpolate=true]{data/loc} -\end{center} -%\inctime{10} -\end{frame} +%% \begin{frame}[fragile] +%% \frametitle{For arbitrary location} +%% \vspace*{-0.1in} +%% \begin{lstlisting} +%% In []: legend(['sin(2y)'], loc=(.8,.1)) +%% \end{lstlisting} +%% \emphbar{Specify south-east corner position} +%% %\vspace*{-0.2in} +%% \begin{center} +%% \includegraphics[height=2in, interpolate=true]{data/loc} +%% \end{center} +%% %\inctime{10} +%% \end{frame} \begin{frame}[fragile] \frametitle{Saving \& Closing} @@ -406,7 +403,7 @@ In []: plot(y, sin(y), 'g') In []: clf() -In []: plot(y, sin(y), 'g', linewidth=2) +In []: plot(y, cos(y), 'r', linewidth=2) \end{lstlisting} \vspace*{-0.2in} \begin{center} @@ -429,13 +426,14 @@ \begin{frame}[fragile] \frametitle{Axes lengths} +\emphbar{Get the axes limits} \begin{lstlisting} -#Get the axes limits In []: xmin, xmax = xlim() In []: ymin, ymax = ylim() - + \end{lstlisting} +\emphbar{Set the axes limits} + \begin{lstlisting} In []: xmax = 2*pi -#Set the axes limits In []: xlim(xmin, xmax) In []: ylim(ymin-0.2, ymax+0.2) \end{lstlisting} @@ -447,7 +445,7 @@ \item Plot x, -x, sin(x), xsin(x) in range $-5\pi$ to $5\pi$ \item Add a legend \item Annotate the origin -\item Set axis limits to the range of x +\item Set axes limits to the range of x \end{enumerate} \begin{lstlisting} In []: x=linspace(-5*pi, 5*pi, 500) @@ -481,23 +479,28 @@ \item Identify the required line numbers \item Then, use \typ{\%save} command of IPython \end{itemize} -\begin{lstlisting} - In []: %hist - In []: %save four_plot.py 16 18-27 -\end{lstlisting} +\typ{In []: \%hist}\\ +\typ{In []: \%save four_plot.py} \alert{\typ{16 18-27}} +\begin{block}{Careful about errors!} + \kwrd{\%hist} will contain the errors as well,\\ + so be careful while selecting line numbers. +\end{block} \end{frame} \begin{frame} \frametitle{Python Scripts\ldots} This is called a Python Script. \begin{itemize} - \item run the script in IPython using \typ{\%run -i sine_plot.py}\\ + \item run the script in IPython using \typ{\%run -i four_plot.py}\\ \end{itemize} \end{frame} -\begin{frame} +\begin{frame}[fragile] \frametitle{What did we learn?} \begin{itemize} + \item \kwrd{\%hist} + \item Saving commands to a script + \item Running a script using \kwrd{\%run -i} \item Creating simple plots. \item Adding labels and legends. \item Annotating plots. diff -r 25b18b51be41 -r 2214b5dba4d4 day1/session2.tex --- a/day1/session2.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/session2.tex Tue Dec 29 19:25:11 2009 +0530 @@ -78,7 +78,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {7 November, 2009\\Day 1, Session 2} +\date[] {14 December, 2009\\Day 1, Session 2} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -331,7 +331,7 @@ \frametitle{What about larger data sets?} \alert{Data is usually present in a file!} \\ Lets look at the \typ{pendulum.txt} file. -\begin{lstlisting} +\begin{lstlisting} $ cat pendulum.txt 1.0000e-01 6.9004e-01 1.1000e-01 6.9497e-01 @@ -339,7 +339,7 @@ 1.3000e-01 7.5360e-01 1.4000e-01 8.3568e-01 1.5000e-01 8.6789e-01 -\end{lstlisting} +\end{lstlisting} %$ \ldots \begin{block}{Windows users:} C:> type pendulum.txt @@ -418,12 +418,12 @@ \end{lstlisting} This is what happens with \typ{line} \begin{lstlisting} -In []: line = '1.2000e-01 7.4252e-01' +In []: line = '1.20 7.42' In []: point = line.split() In []: point -Out[]: ['1.2000e-01', '7.4252e-01'] +Out[]: ['1.20', '7.42'] \end{lstlisting} \end{frame} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/session3.tex --- a/day1/session3.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/session3.tex Tue Dec 29 19:25:11 2009 +0530 @@ -79,7 +79,7 @@ \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {7 November, 2009\\Day 1, Session 3} +\date[] {14 December, 2009\\Day 1, Session 3} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/session4.tex --- a/day1/session4.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/session4.tex Tue Dec 29 19:25:11 2009 +0530 @@ -79,7 +79,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {7 November, 2009\\Day 1, Session 4} +\date[] {14 December, 2009\\Day 1, Session 4} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -134,16 +134,15 @@ \begin{frame}[fragile] \frametitle{Matrices: Initializing} \begin{lstlisting} -In []: A = array([[ 1, 1, 2, -1], - [ 2, 5, -1, -9], - [ 2, 1, -1, 3], - [ 1, -3, 2, 7]]) -In []: A +In []: c = array([[1,1,2], + [2,4,1], + [-1,3,7]]) + +In []: c Out[]: -array([[ 1, 1, 2, -1], - [ 2, 5, -1, -9], - [ 2, 1, -1, 3], - [ 1, -3, 2, 7]]) +array([[1,1,2], + [2,4,1], + [-1,3,7]]) \end{lstlisting} \end{frame} @@ -157,8 +156,8 @@ [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]]) -In []: ones_like([1, 2, 3, 4, 5]) -Out[]: array([1, 1, 1, 1, 1]) +In []: ones_like([1, 2, 3, 4]) +Out[]: array([1, 1, 1, 1]) In []: identity(2) Out[]: @@ -173,17 +172,17 @@ \begin{frame}[fragile] \frametitle{Accessing elements} \begin{lstlisting} -In []: C = array([[1,1,2], - [2,4,1], - [-1,3,7]]) +In []: c +Out[]: +array([[1,1,2], + [2,4,1], + [-1,3,7]]) -In []: C[1][2] +In []: c[1][2] Out[]: 1 - -In []: C[1,2] +In []: c[1,2] Out[]: 1 - -In []: C[1] +In []: c[1] Out[]: array([2, 4, 1]) \end{lstlisting} \end{frame} @@ -192,15 +191,15 @@ \frametitle{Changing elements} \begin{small} \begin{lstlisting} -In []: C[1,1] = -2 -In []: C +In []: c[1,1] = -2 +In []: c Out[]: array([[ 1, 1, 2], [ 2, -2, 1], [-1, 3, 7]]) -In []: C[1] = [0,0,0] -In []: C +In []: c[1] = [0,0,0] +In []: c Out[]: array([[ 1, 1, 2], [ 0, 0, 0], @@ -214,18 +213,18 @@ \frametitle{Slicing} \begin{small} \begin{lstlisting} -In []: C[:,1] +In []: c[:,1] Out[]: array([1, 0, 3]) -In []: C[1,:] +In []: c[1,:] Out[]: array([0, 0, 0]) -In []: C[0:2,:] +In []: c[0:2,:] Out[]: array([[1, 1, 2], [0, 0, 0]]) -In []: C[1:3,:] +In []: c[1:3,:] Out[]: array([[ 0, 0, 0], [-1, 3, 7]]) @@ -237,17 +236,17 @@ \frametitle{Slicing \ldots} \begin{small} \begin{lstlisting} -In []: C[:2,:] +In []: c[:2,:] Out[]: array([[1, 1, 2], [0, 0, 0]]) -In []: C[1:,:] +In []: c[1:,:] Out[]: array([[ 0, 0, 0], [-1, 3, 7]]) -In []: C[1:,:2] +In []: c[1:,:2] Out[]: array([[ 0, 0], [-1, 3]]) @@ -260,18 +259,18 @@ \frametitle{Striding} \begin{small} \begin{lstlisting} -In []: C[::2,:] +In []: c[::2,:] Out[]: array([[ 1, 1, 2], [-1, 3, 7]]) -In []: C[:,::2] +In []: c[:,::2] Out[]: xarray([[ 1, 2], [ 0, 0], [-1, 7]]) -In []: C[::2,::2] +In []: c[::2,::2] Out[]: array([[ 1, 2], [-1, 7]]) @@ -283,13 +282,11 @@ \frametitle{Slicing \& Striding Exercises} \begin{small} \begin{lstlisting} -In []: A = imread('lena.png') +In []: a = imread('lena.png') -In []: imshow(A) +In []: imshow(a) Out[]: -In []: A.shape -Out[]: (512, 512, 4) \end{lstlisting} \end{small} \begin{itemize} @@ -303,13 +300,13 @@ \frametitle{Solutions} \begin{small} \begin{lstlisting} -In []: imshow(A[:256,:256]) +In []: imshow(a[:256,:256]) Out[]: -In []: imshow(A[200:400,200:400]) +In []: imshow(a[200:400,200:400]) Out[]: -In []: imshow(A[::2,::2]) +In []: imshow(a[::2,::2]) Out[]: \end{lstlisting} \end{small} @@ -318,7 +315,12 @@ \begin{frame}[fragile] \frametitle{Transpose of a Matrix} \begin{lstlisting} -In []: A.T +In []: a = array([[ 1, 1, 2, -1], + ...: [ 2, 5, -1, -9], + ...: [ 2, 1, -1, 3], + ...: [ 1, -3, 2, 7]]) + +In []: a.T Out[]: array([[ 1, 2, 2, 1], [ 1, 5, 1, -3], @@ -330,7 +332,7 @@ \begin{frame}[fragile] \frametitle{Sum of all elements} \begin{lstlisting} -In []: sum(A) +In []: sum(a) Out[]: 12 \end{lstlisting} \end{frame} @@ -338,11 +340,11 @@ \begin{frame}[fragile] \frametitle{Matrix Addition} \begin{lstlisting} -In []: B = array([[3,2,-1,5], +In []: b = array([[3,2,-1,5], [2,-2,4,9], [-1,0.5,-1,-7], [9,-5,7,3]]) -In []: A + B +In []: a + b Out[]: array([[ 4. , 3. , 1. , 4. ], [ 4. , 3. , 3. , 0. ], @@ -354,7 +356,7 @@ \begin{frame}[fragile] \frametitle{Elementwise Multiplication} \begin{lstlisting} -In []: A*B +In []: a*b Out[]: array([[ 3. , 2. , -2. , -5. ], [ 4. , -10. , -4. , -81. ], @@ -367,7 +369,7 @@ \begin{frame}[fragile] \frametitle{Matrix Multiplication} \begin{lstlisting} -In []: dot(A,B) +In []: dot(a, b) Out[]: array([[ -6. , 6. , -6. , -3. ], [-64. , 38.5, -44. , 35. ], @@ -379,7 +381,7 @@ \begin{frame}[fragile] \frametitle{Inverse of a Matrix} \begin{lstlisting} -In []: inv(A) +In []: inv(a) \end{lstlisting} \begin{small} \begin{lstlisting} @@ -395,7 +397,7 @@ \begin{frame}[fragile] \frametitle{Determinant} \begin{lstlisting} -In []: det(A) +In []: det(a) Out[]: 80.0 \end{lstlisting} \end{frame} @@ -405,16 +407,16 @@ \frametitle{Eigenvalues and Eigen Vectors} \begin{small} \begin{lstlisting} -In []: E = array([[3,2,4],[2,0,2],[4,2,3]]) +In []: e = array([[3,2,4],[2,0,2],[4,2,3]]) -In []: eig(E) +In []: eig(e) Out[]: (array([-1., 8., -1.]), array([[-0.74535599, 0.66666667, -0.1931126 ], [ 0.2981424 , 0.33333333, -0.78664085], [ 0.59628479, 0.66666667, 0.58643303]])) -In []: eigvals(E) +In []: eigvals(e) Out[]: array([-1., 8., -1.]) \end{lstlisting} \end{small} @@ -423,7 +425,7 @@ %% \begin{frame}[fragile] %% \frametitle{Computing Norms} %% \begin{lstlisting} -%% In []: norm(E) +%% In []: norm(e) %% Out[]: 8.1240384046359608 %% \end{lstlisting} %% \end{frame} @@ -432,7 +434,7 @@ %% \frametitle{Singular Value Decomposition} %% \begin{small} %% \begin{lstlisting} -%% In []: svd(E) +%% In []: svd(e) %% Out[]: %% (array( %% [[ -6.66666667e-01, -1.23702565e-16, 7.45355992e-01], @@ -503,9 +505,33 @@ \end{frame} \begin{frame}[fragile] +\frametitle{Getting $L$ and $T^2$} +If you \alert{closed} IPython after session 2 +\begin{lstlisting} +In []: l = [] +In []: t = [] +In []: for line in open('pendulum.txt'): + .... point = line.split() + .... l.append(float(point[0])) + .... t.append(float(point[1])) + .... + .... +\end{lstlisting} +\end{frame} + +\begin{frame}[fragile] +\frametitle{Getting $L$ and $T^2$ \dots} +\begin{lstlisting} +In []: l = array(l) +In []: t = array(t) +\end{lstlisting} +\alert{\typ{In []: tsq = t*t}} +\end{frame} + +\begin{frame}[fragile] \frametitle{Generating $A$} \begin{lstlisting} -In []: A = array([L, ones_like(L)]) +In []: A = array([l, ones_like(l)]) In []: A = A.T \end{lstlisting} %% \begin{itemize} @@ -524,7 +550,7 @@ \item Along with a lot of things, it returns the least squares solution \end{itemize} \begin{lstlisting} -In []: result = lstsq(A,TSq) +In []: result = lstsq(A,tsq) In []: coef = result[0] \end{lstlisting} \end{frame} @@ -533,13 +559,13 @@ \frametitle{Least Square Fit Line \ldots} We get the points of the line from \typ{coef} \begin{lstlisting} -In []: Tline = coef[0]*L + coef[1] +In []: Tline = coef[0]*l + coef[1] \end{lstlisting} \begin{itemize} -\item Now plot Tline vs. L, to get the Least squares fit line. +\item Now plot \typ{Tline} vs. \typ{l}, to get the Least squares fit line. \end{itemize} \begin{lstlisting} -In []: plot(L, Tline) +In []: plot(l, Tline) \end{lstlisting} \end{frame} diff -r 25b18b51be41 -r 2214b5dba4d4 day1/session6.tex --- a/day1/session6.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1/session6.tex Tue Dec 29 19:25:11 2009 +0530 @@ -78,7 +78,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {7 November, 2009\\Day 1, Session 6} +\date[] {14 December, 2009\\Day 1, Session 6} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -150,7 +150,6 @@ [-1, 0.5, -1]]) In []: b = array([[1], [-2], [0]]) In []: x = solve(A, b) - In []: Ax = dot(A,x) \end{lstlisting} \end{frame} @@ -168,6 +167,7 @@ \begin{frame}[fragile] \frametitle{Let's check!} \begin{lstlisting} +In []: Ax = dot(A,x) In []: Ax Out[]: array([[ 1.00000000e+00], @@ -318,6 +318,7 @@ \item Define a function as below \end{itemize} \begin{lstlisting} +In []: from scipy.integrate import odeint In []: def epid(y, t): .... k, L = 0.00003, 25000 .... return k*y*(L-y) @@ -328,7 +329,7 @@ \begin{frame}[fragile] \frametitle{Solving ODEs using SciPy \ldots} \begin{lstlisting} -In []: t = arange(0, 12, 0.2) +In []: t = linspace(0, 12, 61) In []: y = odeint(epid, 250, t) @@ -338,6 +339,14 @@ \end{frame} \begin{frame}[fragile] +\frametitle{Result} +\begin{center} +\includegraphics[height=2in, interpolate=true]{data/image} +\end{center} +\end{frame} + + +\begin{frame}[fragile] \frametitle{ODEs - Simple Pendulum} We shall use the simple ODE of a simple pendulum. \begin{equation*} @@ -392,6 +401,12 @@ \end{lstlisting} \end{frame} +\begin{frame}[fragile] +\frametitle{Result} +\begin{center} +\includegraphics[height=2in, interpolate=true]{data/ode} +\end{center} +\end{frame} \begin{frame} \frametitle{Things we have learned} diff -r 25b18b51be41 -r 2214b5dba4d4 day1quiz1.tex --- a/day1quiz1.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1quiz1.tex Tue Dec 29 19:25:11 2009 +0530 @@ -1,5 +1,5 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Tutorial slides on Python. +% Quiz slides day 1 quiz 1 % % Author: FOSSEE % Copyright (c) 2005-2009, FOSSEE Team @@ -35,12 +35,12 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[Basic Python]{Python: Quiz} +\title[Basic Python]{Python for science and engineering: Day 1, Quiz 1} \author[FOSSEE Team] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {14, December 2009\\Day 1, Quiz 1} +\date[] {\today \\Day 1, Quiz 1} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -54,33 +54,72 @@ \frametitle{Write your details...} On the top right hand corner please write down the following: \begin{itemize} - \item Name: - \item Affliation: - \item Occupation: + \item Name: + \item University/College/Company: + \item Student/Teacher/Professional: \end{itemize} \end{frame} +\begin{frame}[fragile] +\frametitle{\incqno } +Describe the plot produced by the following: + +\begin{lstlisting} +In []: x = linspace(0, 2*pi) +In []: plot(x, cos(x), 'go') +\end{lstlisting} +\end{frame} + +\begin{frame} +\frametitle{\incqno } +How will you plot the previous graph with line width set to 3? How will +you set the $x$ and $y$ labels of the plot? +\end{frame} + +\begin{frame} +\frametitle{\incqno } +How will you set the x and y axis limits so that the region of interest +is in the rectangle $(0, -1.5)$ (left bottom coordinate) and $(2\pi, +1.5)$ (right top coordinate)? +\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? + What code 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] + In []: a = [1, 2, 5, 9] + In []: a[0:-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}? + How do you combine two lists \emph{a} and \emph{b} to produce one list? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } + \begin{lstlisting} + In []: a = [1, 2, 5, 9] + \end{lstlisting} + How do you add the value 10 to the end of this list? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } + \begin{lstlisting} + In []: a = [1, 2, 5, 9] + \end{lstlisting} + How do you find the length of this list? \end{frame} \begin{frame}[fragile] @@ -95,25 +134,25 @@ What is the output? \end{frame} -\begin{frame}[fragile] -\frametitle{\incqno } - \begin{lstlisting} - for x in "abcd": - print x +%% \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} +%% 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} \frametitle{\incqno } @@ -121,8 +160,82 @@ \end{frame} \begin{frame} +\begin{lstlisting} +In []: sc = {'A': 10, 'B': 20, + 'C': 70} +\end{lstlisting} +Given the above dictionary, what command will you give to plot a +pie-chart? +\end{frame} + + +\begin{frame}[fragile] \frametitle{\incqno } -How to read and print each line of a file? +\begin{lstlisting} +In []: marks = [10, 20, 30, 50, 55, + 75, 83] +\end{lstlisting} + +Given the above marks, how will you calculate the \alert{mean} and +\alert{standard deviation}? +\end{frame} + +%\begin{frame}[fragile] +%\frametitle{\incqno } +%\begin{lstlisting} +%In []: marks = [10, 20, 30, 50, 55, +% 75, 83] +%\end{lstlisting} +%How will you convert the list \texttt{marks} to an \alert{array}? +%\end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% In []: a = array([[1, 2], +%% [3, 4]]) +%% In []: a[1,0] = 0 +%% \end{lstlisting} +%% What is the resulting matrix? +%% \end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% In []: a = array([[1, 2], +%% [3, 4]]) +%% \end{lstlisting} +%% How do you get the transpose of this array? +%% \end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% In []: a = array([[1, 2], +%% [3, 4]]) +%% In []: b = array([[1, 1], +%% [2, 2]]) +%% In []: a*b +%% \end{lstlisting} +%% What does this produce? +%% \end{frame} + +%% \begin{frame} +%% \frametitle{\incqno } +%% What command do you use to find the inverse of a matrix and its +%% eigenvalues? +%% \end{frame} + +%% \begin{frame} +%% \frametitle{\incqno } +%% Given a 4x4 matrix \texttt{A} and a 4-vector \texttt{b}, what command do +%% you use to solve for the equation \\ +%% \texttt{Ax = b}? +%% \end{frame} + +\begin{frame} +\frametitle{\incqno } +Write the code to read a file \texttt{data.txt} and print each line of it? \end{frame} \begin{frame}[fragile] diff -r 25b18b51be41 -r 2214b5dba4d4 day1quiz2.tex --- a/day1quiz2.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day1quiz2.tex Tue Dec 29 19:25:11 2009 +0530 @@ -35,12 +35,13 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Title page -\title[Basic Python]{Python: Quiz} +\title[Basic Python]{Python for science and engineering: Day 1, Quiz 2} \author[FOSSEE Team] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {11, October 2009\\Day 2} +\date[] {\today \\ +Day 1, quiz 2} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -54,125 +55,243 @@ \frametitle{Write your details...} On the top right hand corner please write down the following: \begin{itemize} - \item Name: - \item Affliation: - \item Occupation: + \item Name: + \item University/College/Company: + \item Student/Teacher/Professional: \end{itemize} \end{frame} +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% In []: x = array([[1,2,3,4], +%% [3,4,2,5]]) +%% \end{lstlisting} +%% What is the \lstinline+shape+ of this array? +%% \end{frame} + + +\begin{frame}[fragile] +\frametitle{\incqno } +\begin{lstlisting} + In []: x = array([[1,2,3,4]]) +\end{lstlisting} +How to change the third element (i.e.\ 3) of \lstinline+x+ to 0? +\end{frame} + \begin{frame}[fragile] \frametitle{\incqno } \begin{lstlisting} - >>> x = array([[1,2,3,4],[3,4,2,5]]) - >>> x.shape - (2, 4) + In []: x = array([[1,2,3,4], + [3,4,2,5]]) +\end{lstlisting} +How do you get the following slice of \lstinline+x+? +\begin{lstlisting} +array([[2,3], + [4,2]]) \end{lstlisting} -Change the shape of \lstinline+x+ to (4,2) \end{frame} + +%% \begin{frame} +%% \frametitle{\incqno } +%% The file \lstinline+datafile.txt+ contains 3 columns of data. What +%% command will you use to read the entire data file into an array? +%% \end{frame} + +%% \begin{frame} +%% \frametitle{\incqno } + +%% If the contents of the file \lstinline+datafile.txt+ is read into an +%% $N\times3$ array called \lstinline+data+, how would you obtain the third +%% column of this data? + +%% \end{frame} + \begin{frame}[fragile] \frametitle{\incqno } \begin{lstlisting} - >>> x = array([[1,2,3,4]]) + In []: x = array(([1,2,3,4], + [2,3,4,5])) + In []: x[-2][-3] = 4 + In []: print x \end{lstlisting} -How to change the third element of \lstinline+x+ to 0? +What will be printed? \end{frame} -\begin{frame}[fragile] + +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% In []: x = arange(0, 1, 0.25) +%% In []: print x +%% \end{lstlisting} +%% What will be printed? +%% \end{frame} + + +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% from scipy.integrate import quad +%% def f(x): +%% res = x*cos(x) + +%% quad(f, 0, 1) +%% \end{lstlisting} + +%% What changes will you make to the above code to make it work? +%% \end{frame} + +%% \begin{frame} +%% \frametitle{\incqno } +%% What two commands will you use to create and evaluate a spline given +%% some data? +%% \end{frame} + +\begin{frame} \frametitle{\incqno } -What would be the result? -\begin{lstlisting} - >>> y = arange(3) - >>> x = linspace(0,3,3) - >>> x-y -\end{lstlisting} +What command will you use if you wish to integrate a system of ODEs? +\end{frame} + +\begin{frame} +\frametitle{\incqno } +How do you calculate the roots of the polynomial, $y = 1 + 6x + 8x^2 + +x^3$? +\end{frame} + +\begin{frame} +\frametitle{\incqno } + +Two arrays \lstinline+a+ and \lstinline+b+ are numerically almost equal, what command +do you use to check if this is true? + \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] +In []: marks = [10, 20, 30, 50, 55, + 75, 83] \end{lstlisting} -What is the output? +How will you convert the list \texttt{marks} to an \alert{array}? \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,:] +In []: a = array([[1, 2], + [3, 4]]) +In []: a[1,0] = 0 \end{lstlisting} -Hint: +What is the resulting matrix? +\end{frame} + +\begin{frame}[fragile] +\frametitle{\incqno } \begin{lstlisting} - >>> x = arange(9) - >>> x[::-1] - array([8, 7, 6, 5, 4, 3, 2, 1, 0]) +In []: a = array([[1, 2], + [3, 4]]) \end{lstlisting} +How do you get the transpose of this array? \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]]) +In []: a = array([[1, 2], + [3, 4]]) +In []: b = array([[1, 1], + [2, 2]]) +In []: a*b \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? +What does this produce? \end{frame} -\begin{frame}[fragile] +\begin{frame} \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} +What command do you use to find the inverse of a matrix and its +eigenvalues? \end{frame} -\begin{frame}[fragile] +\begin{frame} \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. +Given a 4x4 matrix \texttt{A} and a 4-vector \texttt{b}, what command do +you use to solve for the equation \\ +\texttt{Ax = b}? \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} +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% What would be the result? +%% \begin{lstlisting} +%% In []: x +%% array([[0, 1, 2], +%% [3, 4, 5], +%% [6, 7, 8]]) +%% In []: x[::-1,:] +%% \end{lstlisting} +%% Hint: +%% \begin{lstlisting} +%% In []: x = arange(9) +%% In []: x[::-1] +%% array([8, 7, 6, 5, 4, 3, 2, 1, 0]) +%% \end{lstlisting} +%% \end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% What would be the result? +%% \begin{lstlisting} +%% In []: y = arange(3) +%% In []: x = linspace(0,3,3) +%% In []: x-y +%% \end{lstlisting} +%% \end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% In []: 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 } +%% What would be the output? +%% \begin{lstlisting} +%% In []: y = arange(4) +%% In []: x = array(([1,2,3,2],[1,3,6,0])) +%% In []: x + y +%% \end{lstlisting} +%% \end{frame} + +%% \begin{frame}[fragile] +%% \frametitle{\incqno } +%% \begin{lstlisting} +%% In []: 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} +%% In []: x = arange(9) +%% In []: y = arange(9.) +%% In []: x == y +%% \end{lstlisting} +%% \end{frame} + diff -r 25b18b51be41 -r 2214b5dba4d4 day2/cheatsheet1.tex --- a/day2/cheatsheet1.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/cheatsheet1.tex Tue Dec 29 19:25:11 2009 +0530 @@ -26,95 +26,152 @@ \large{FOSSEE} \end{center} \section{Data types} -Complex Numbers +\subsection{int and float} +A whole number is a \typ{int} variable. \begin{lstlisting} -In []: c = 3+4j -In []: abs(c) +In []: a = 13 +In []: type(a) +Out[]: +In []: b = -2 +In []: type(b) +Out[]: +In []: c = 500000000 +In []: type(c) +Out[]: +\end{lstlisting} +A number with decimal is a \typ{float}. +\begin{lstlisting} +In []: p = 3.141592 +In []: type(p) +Out[]: +\end{lstlisting} +\subsection{Complex Numbers} +\begin{lstlisting} +In []: c = 3+4j #coeff of j specifies imaginary part +In []: abs(c) #absolute value of complex number Out[]: 5.0 -In []: c.imag +In []: c.imag #accessing imaginary part of c Out[]: 4.0 -In []: c.real +In []: c.real #accessing real part of c Out[]: 3.0 \end{lstlisting} -Boolean +\newpage +\subsection{Boolean} \begin{lstlisting} In []: a = False In []: b = True In []: c = True -In []: (a and b) or c +In []: (a and b) or c #Boolean operations Out[]: True \end{lstlisting} -Strings +\textbf{Note:} Python is case sensitive language, \typ{True} is \typ{bool} type, but \typ{true} would be a variable. and hence following assignment fails:\\ +\typ{In []: a = true}\\ +\subsection{Strings} \begin{lstlisting} -In []: w = "hello" -In []: print w[0] + w[2] + w[-1] -Out[]: hlo -In []: len(w) +In []: w = "hello" #w is string variable +In []: print w[1] +Out[]: e +In []: print w[-1] #last character of string +Out[]: o + \end{lstlisting} +\textbf{Note:} For a string variable, individual elements can be accessed using indices.\\ +\textbf{Note:} All slicing and striding operations works with strings as well. + \begin{lstlisting} +In []: len(w) #function to calculate length of string Out[]: 5 In []: w[0] = 'H' # ERROR: Strings are immutable \end{lstlisting} -String methods +\subsection{String methods} \begin{lstlisting} In []: a = 'Hello World' In []: a.startswith('Hell') # 'a' starts with 'Hell' +Out[]: True In []: a.endswith('ld') # 'a' ends with 'ld' +Out[]: True In []: a.upper() # all characters to upper case +Out[]: 'HELLO WORLD' In []: a.lower() # all characters to lower case +Out[]: 'hello world' In []: ''.join(['a', 'b', 'c']) Out[]: 'abc' \end{lstlisting} -String formatting +\typ{join} function joins all the list member passed as argument with the string it is called upon. In above case it is \typ{empty string}. +\begin{lstlisting} +In []: ' '.join(['a','b','c']) +Out[]: 'a b c' +In []: ','.join(['a','b','c']) +Out[]: 'a,b,c' +\end{lstlisting} +\subsection{String formatting} \begin{lstlisting} -In []: x, y = 1, 1.234 +In []: x, y = 1, 1.234 #initializing two variables In []: 'x is %s, y is %s' %(x, y) Out[]: 'x is 1, y is 1.234' \end{lstlisting} -Arithmetic Operators +\textbf{Note:} \typ{\%s} used in above formatting specifies \typ{'str'} representation of variables. One can also try:\\ +\typ{\%d} for \typ{int} representation\\ +\typ{\%f} for \typ{float} representation +\begin{lstlisting} +In []: 'x is %f, y is %f' %(x, y) +Out[]: 'x is 1.000000, y is 1.234000' + +In []: 'x is %d, y is %d' %(x, y) +Out[]: 'x is 1, y is 1' +\end{lstlisting} +\subsection{Arithmetic Operators} \begin{lstlisting} In []: 45 % 2 # Modulo operator Out[]: 1 -In []: 1234567891234567890 ** 3 # Power +In []: 5 ** 3 # Power +Out[]: 125 In []: a = 5 -In []: a += 1 +In []: a += 1 #increment by 1, translates to a = a + 1 In []: a *= 2 \end{lstlisting} -String Operations +\subsection{String Operations} \begin{lstlisting} In []: s = 'Hello' In []: p = 'World' -In []: s + p +In []: s + p #concatenating two strings Out[]: 'HelloWorld' -In []: s * 4 +In []: s * 4 #repeat string for given number of times Out[]: 'HelloHelloHelloHello' \end{lstlisting} -Relational and Logical Operators +\subsection{Relational and Logical Operators} \begin{lstlisting} -In []: p, z, n = 1, 0, -1 -In []: p == n +In []: p, z, n = 1, 0, -1 #initializing three variables +In []: p == n #equivalency check Out[]: False -In []: p >= n +In []: p >= n Out[]: True -In []: n < z < p +In []: n < z < p #finding largest number among three Out[]: True In []: p + n != z Out[]: False \end{lstlisting} -Built-ins +\subsection{Built-ins} \begin{lstlisting} -In []: int(17 / 2.0) +In []: int(17 / 2.0) #converts arguments to integer Out[]: 8 -In []: float(17 / 2) +In []: float(17 / 2) #argument is already integer(17 / 2 = 8) Out[]: 8.0 -In []: str(17 / 2.0) +In []: str(17 / 2.0) #converts to string Out[]: '8.5' -In []: round( 7.5 ) +In []: round( 7.5 ) Out[]: 8.0 \end{lstlisting} -Console Input +\subsection{Console Input} \begin{lstlisting} In []: a = raw_input('Enter a value: ') Enter a value: 5 \end{lstlisting} +\textbf{Note:} \typ{raw_input} always returns string representation of user input and hence: +\begin{lstlisting} +In []: type(a) +Out[]: +\end{lstlisting} +To get integer or floating point of this input, one has to perform type conversion:\\ +\typ{In []: b = int(a)} \section{Conditionals} \typ{if} \begin{lstlisting} @@ -133,4 +190,10 @@ In []: a = raw_input('Enter number(Q to quit):') In []: num = int(a) if a != 'Q' else 0 \end{lstlisting} +Above statement can be read as ``num is int of a, if a is not equal to 'Q', otherwise 0 `` +\section{Links and References} +\begin{itemize} + \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} +\end{itemize} \end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day2/cheatsheet2.tex --- a/day2/cheatsheet2.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/cheatsheet2.tex Tue Dec 29 19:25:11 2009 +0530 @@ -26,64 +26,256 @@ \large{FOSSEE} \end{center} \section{Basic Looping} -\typ{while} +\subsection{\typ{while}} \begin{lstlisting} In []: a, b = 0, 1 -In []: while b < 10: +In []: while b < 10: ...: print b, ...: a, b = b, a + b # Fibonacci Sequence + ...: \end{lstlisting} +Basic syntax of \typ{while} loop is: +\begin{lstlisting} +while condition: + statement1 + statement2 +\end{lstlisting} +All statements are executed, till the condition statement evaluates to True. +\subsection{\typ{for} and \typ{range}} +\typ{range(start, stop, step)}\\ +returns a list containing an arithmetic progression of integers.\\ +Of the arguments mentioned above, both start and step are optional.\\ +For example, if we skip third argument, i.e \typ{step}, default is taken as 1. So: +\begin{lstlisting} +In []: range(1,10) +Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9] +\end{lstlisting} +\textbf{Note:} stop value is not included in the list.\\ +Similarly if we don't pass \typ{first} argument (in this case \typ{start}), default is taken to be 0. +\begin{lstlisting} +In []: range(10) +Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +\end{lstlisting} +In case third argument is mentioned(\typ{step}), the jump between consecutive members of the list would be equal to that. +\begin{lstlisting} +In []: range(1,10,2) +Out[]: [1, 3, 5, 7, 9] +\end{lstlisting} +%Notice the jump between two consecutive elements is 2, i.e step.\\ \typ{for} and \typ{range}\\ -\typ{range([start,] stop[, step])} +As mentioned previously \typ{for} in Python is used to iterate through the list members. So \typ{for} and \typ{range} can be used together to iterate through required series. For example to get square of all numbers less then 5 and greater then equal to 0, code can be written as: \begin{lstlisting} -In []: for i in range(3, 10, 2): +In []: for i in range(5): ....: print i, i * i + ....: + ....: +0 0 +1 1 +2 4 3 9 -5 25 -7 49 -9 81 +4 16 +\end{lstlisting} + +\section{list} +\begin{lstlisting} +In []: num = [1, 2, 3, 4] # Initializing a list +In []: num +Out[]: [1, 2, 3, 4] +\end{lstlisting} +\subsection{Accessing individual elements} +\begin{lstlisting} +In []: num[1] +Out[]: 2 +\end{lstlisting} +\textbf{Note:} Index of list starts from 0. +\begin{lstlisting} +In []: num[5] # ERROR: throws a index error +IndexError: list index out of range +In []: num[-1] +Out[]: 4 \end{lstlisting} -List methods (Contd.) +\textbf{Note: }\typ{-1} points to last element in a list. Similarly to access third last element of a list one can use: +\begin{lstlisting} +In []: num[-3] +Out[]: 2 +\end{lstlisting} +\subsection{\typ{list} operations} +\begin{lstlisting} +In []: num += [9, 10, 11] # Concatenating two lists +In []: num +Out[]: [1, 2, 3, 4, 9, 10, 11] +\end{lstlisting} +\typ{list} provides \typ{append} function to append objects at the end. +\begin{lstlisting} +In []: num = [1, 2, 3, 4] +In []: num.append(-2) +In []: num +Out[]: [1, 2, 3, 4, -2] +\end{lstlisting} +%% In []: num += [-5] +%% In []: num +%% Out[]: [1, 2, 3, 4, -2, -5] +Working of \typ{append} is different from \typ{+} operator on list. Till here both will behave as same. But in following case: \begin{lstlisting} In []: num = [1, 2, 3, 4] -In []: num.append([9, 10, 11]) + +In []: num + [9, 10, 11] +Out[]: [1, 2, 3, 4, 9, 10, 11] + +In []: num.append([9, 10, 11]) # appending a list to a list + In []: num -Out[]: [1, 2, 3, 4, [9, 10, 11]] +Out[]: [1, 2, 3, 4, [9, 10, 11]] # last element is a list +\end{lstlisting} +when one attempts to append a list(in above case [9, 10, 11]) to a list(num) it adds list as a single element. So the resulting list will have a element which itself is a list. But \typ{+} operator would simply add the elements of second list.\\ +\subsection{Miscellaneous} +\begin{lstlisting} In []: num = [1, 2, 3, 4] -In []: num.extend([5, 6, 7]) +In []: num.extend([5, 6, 7]) # extend list by adding elements In []: num Out[]: [1, 2, 3, 4, 5, 6, 7] -In []: num.reverse() +In []: num.reverse() # reverse the current list In []: num Out[]: [7, 6, 5, 4, 3, 2, 1] -In []: num.remove(6) +In []: num.remove(6) # removing first occurrence of 6 In []: num +Out[]: [7, 5, 4, 3, 2, 1] +In []: len(num) # returns the length of list +Out[]: 6 +In []: a = [1, 5, 3, 7, -2, 4] +In []: min(a) # returns smallest item in a list. +Out[]: -2 +In []: max(a) # returns largest item in a list. +Out[]: 7 \end{lstlisting} -Slicing: \typ{list[initial:final:step]} + +\subsection{Slicing} +General syntax for getting slice out of a list is \\ +\typ{list[initial:final:step]} \begin{lstlisting} -In []: a[1:-1:2] +In []: a = [1, 2, 3, 4, 5] +In []: a[1:-1:2] Out[]: [2, 4] +\end{lstlisting} +Start slice from second element(1), till the last element(-1) with step size of 2. +\begin{lstlisting} In []: a[::2] Out[]: [1, 3, 5] +\end{lstlisting} +Start from beginning(since \typ{initial} is blank), till last(this time last element is included, as \typ{final} is blank), with step size of 2.\\ +Apart from using \typ{reverse} command on list, one can also use slicing in special way to get reverse of a list. +\begin{lstlisting} +In []: a[-1:-4:-1] +Out[]: [5, 4, 3] +\end{lstlisting} +Above syntax of slice can be expressed as, ``start from last element(\typ{-1}), go till fourth last element(\typ{-4}), with step size \typ{-1}, which implies, go in reverse direction. That is, first element would be \typ{a[-1]}, second element would be \typ{a[-2]} and so on and so forth.''\\ +So to get reverse of whole list one can write following slice syntax: +\begin{lstlisting} In []: a[-1::-1] Out[]: [5, 4, 3, 2, 1] \end{lstlisting} -Tuples(Immutable lists) +Since \typ{final} is left blank, it will traverse through whole list in reverse manner.\\ +\textbf{Note:} While \typ{reverse} reverses the original list, slicing will just result in a instance list with reverse of original, which can be used and worked upon independently. +%%Should we include list copy concept here? +\subsection{Containership} +\typ{in} keyword is used to check for containership of any element in a given list. \begin{lstlisting} -In []: t = (1, 2, 3, 4, 5, 6, 7, 8) -In []: t[0] + t[3] + t[-1] +In []: a = [2, 5, 4, 6, 9] +In []: 4 in a +Out[]: True + +In []: b = 15 +In []: b in a +Out[]: False +\end{lstlisting} +\section{Tuples} +Tuples are sequences just like Lists, but they are \textbf{immutable}, or items/elements cannot be changed in any way. +\begin{lstlisting} +In []: t = (1, 2, 3, 4, 5, 6, 7, 8) +\end{lstlisting} +\textbf{Note:} For tuples we use parentheses in place of square brackets, rest is same as lists. +\begin{lstlisting} +In []: t[0] + t[3] + t[-1] # elements are accessed via indices Out[]: 13 In []: t[4] = 7 # ERROR: tuples are immutable \end{lstlisting} -Sets +\textbf{Note:} elements cant be changed! +\section{Dictionaries} +Dictionaries are data structures that provide key-value mappings. They are similar to lists except that instead of the values having integer indexes, they have keys or strings as indexes.\\ +A simple dictionary can be created by: +\begin{lstlisting} +In []: player = {'Mat': 134,'Inn': 233, + 'Runs': 10823, 'Avg': 52.53} +\end{lstlisting} +For above case, value on left of ':' is key and value on right is corresponding value. To retrieve value related to key 'Avg' +\begin{lstlisting} +In []: player['Avg'] +Out[]: 52.530000000000001 +\end{lstlisting} +\subsection{Element operations} +\begin{lstlisting} +In []: player['Name'] = 'Rahul Dravid' #Adds new key-value pair. +In []: player +Out[]: +{'Avg': 52.530000000000001, + 'Inn': 233, + 'Mat': 134, + 'Name': 'Rahul Dravid', + 'Runs': 10823} +In []: player.pop('Mat') # removing particular key-value pair +Out[]: 134 +In [21]: player +Out[21]: {'Avg': 52.530000000000001, 'Inn': 233, + 'Name': 'Rahul Dravid', 'Runs': 10823} +\end{lstlisting} +\begin{lstlisting} +In []: player['Name'] = 'Dravid' +In []: player +Out[23]: {'Avg': 52.530000000000001, 'Inn': 233, + 'Name': 'Dravid', 'Runs': 10823} +\end{lstlisting} +\textbf{Note:} Duplicate keys are overwritten! +\subsection{containership} +\begin{lstlisting} +In []: 'Inn' in player +Out[]: True +In []: 'Econ' in player +Out[]: False +\end{lstlisting} +\textbf{Note:} Containership is always checked on 'keys' of dictionary, never on 'values'.\\ +\subsection{Methods} +\begin{lstlisting} +In []: player.keys() # returns list of all keys +Out[]: ['Runs', 'Inn', 'Avg', 'Mat'] + +In []: player.values() # returns list of all values. +Out[]: [10823, 233, + 52.530000000000001, 134] +\end{lstlisting} +\section{Sets} +are an unordered collection of unique elements.\\ +Creation: +\begin{lstlisting} +In []: s = set([2,4,7,8,5]) # creating a basic set +In []: s +Out[]: set([2, 4, 5, 7, 8]) +In []: g = set([2, 4, 5, 7, 4, 0, 5]) +In []: g +Out[]: set([0, 2, 4, 5, 7]) # No repetition allowed. +\end{lstlisting} +Some other operations which can be performed on sets are: \begin{lstlisting} In []: f = set([1,2,3,5,8]) In []: p = set([2,3,5,7]) In []: f | p # Union of two sets Out[]: set([1, 2, 3, 5, 7, 8]) -In []: g = set([2, 4, 5, 7, 4, 0, 5]) -In []: g -Out[]: set([2, 4, 5, 7, 0]) # No repetition allowed. +In []: f & p # Intersection of two sets +Out[]: set([2, 3, 5]) +In []: f - p # Elements in f not is p +Out[]: set([1, 8]) +In []: f ^ p # (f - p) | (p - f) +Out[]: set([1, 7, 8])) +In []: set([2,3]) < p # Test for subset +Out[]: True \end{lstlisting} \end{document} - diff -r 25b18b51be41 -r 2214b5dba4d4 day2/cheatsheet3.tex --- a/day2/cheatsheet3.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/cheatsheet3.tex Tue Dec 29 19:25:11 2009 +0530 @@ -25,27 +25,84 @@ \LARGE{Python: Functions and Objects}\\ \large{FOSSEE} \end{center} -\section{Functions} -Function definition +\section{Function} +They allows us to enclose a set of statements and call them again and again instead of repeating the group of statements every-time. +\subsection{Function definition} \begin{lstlisting} -def signum( r ): - if r < 0: - return -1 - elif r > 0: - return 1 - else: - return 0 +In []: def signum(r): + ...: if r < 0: + ...: return -1 + ...: elif r > 0: + ...: return 1 + ...: else: + ...: return 0 + ...: + ...: \end{lstlisting} -Default Arguments +%\typ{def} is a keyword, which is used to define a function with given name. +\subsection{Usage} +\begin{lstlisting} +In []: signum(4) +Out[]: 1 +In []: signum(0) +Out[]: 0 +In []: signum(-4) +Out[]: -1 +In []: signum() # ERROR signum() takes exactly 1 argument(0 given) +\end{lstlisting} +\textbf{Note:} Arguments passed to a function are passed by-value \textbf{only if} they are basic Python data type(int, float). In case one passes immutable types(String, tuples), they cant be modified in the function, but objects like \typ{list} and dictionary can be manipulated. +\subsection{Default Arguments} +This feature allow the functions to take the arguments optionally. For example: +\begin{lstlisting} +In []: greet = 'hello world' + +In []: greet.split() +Out[]: ['hello', 'world'] +\end{lstlisting} +In above case, default argument which \typ{split} function uses is a blank space. One can pass argument also to split the string for a different delimiter. +\begin{lstlisting} +In []: line = 'Rossum, Guido, 54, 46, 55' + +In []: line.split(',') #split with ',' +Out[]: ['Rossum', ' Guido', ' 54', + ' 46', ' 55'] +\end{lstlisting} +Function to work with default argument can be defined as: \begin{lstlisting} def welcome(greet, name='world!'): - print greet, name + print greet, name \end{lstlisting} -Keyword Arguments +above function can be used as: +\begin{lstlisting} +In []: welcome("Hello") #using default argument +Hello World! +In []: welcome("Hi", "Guido") #taking name via argument +Hi Guido +\end{lstlisting} +\subsection{Keyword Arguments} +This feature provides the facility of passing arguments by specifying the name of the parameter as defined in the function definition. You don't have to remember the order of the parameters in function definition. For example: \begin{lstlisting} In []: plot(y, sin(y), 'g', linewidth=2) +In []: plot(y, cos(y), linewidth=1, color='g') \end{lstlisting} -Self contained python script +Both call to \typ{plot} function will work and parameters are set accordingly.\\ +One can define a function such that keyword arguments can be used in following way: +\begin{lstlisting} +def wish(name='World', greetings='Hello'): + print greetings, name +\end{lstlisting} +This function can be called as: +\begin{lstlisting} +In [13]: wish() #default arguments will work +Hello World +In [14]: wish(greetings='hey', name='madhu') +hey madhu +In [15]: wish(name='vattam', greetings = 'bye bye') +bye bye vattam +\end{lstlisting} +% sorry Vattam just a joke :P +\section{Self contained python script} +Functions like \typ{plot}, \typ{linspace} etc are not inbuilt functions. One have to import them to use them. \begin{lstlisting} from scipy import linspace, pi, sin from pylab import plot, legend, annotate @@ -61,5 +118,25 @@ xlim(-5*pi, 5*pi) ylim(-5*pi, 5*pi) \end{lstlisting} - +These import statements are necessary to make program self contained. After importing, we can run script via:\\ +\typ{$ python sine_plot.py} \\ %$ +We no longer need: +\begin{lstlisting} +$ ipython -pylab +In []: %run -i sine_plot.py +\end{lstlisting} %$ +\section{objects} +In Python everything is a object! All variables, lists, tuples, dictionaries and even functions are objects. +\begin{lstlisting} +In []: a = str() # initializing a string object. +In []: b = "Hello World" +In []: b.split() # calling function on object 'b' +Out[]: ['Hello', 'World'] +\end{lstlisting} +``.'' is a operator used to call functions defined for given object. +\section{Links and References} +\begin{itemize} +\item Some of inbuilt functions available with Python are listed at\\ \url{http://docs.python.org/library/functions.html} +\item Reference manual to describe the standard libraries that are distributed with Python is available at \url{http://docs.python.org/library/} +\end{itemize} \end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day2/cheatsheet4.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day2/cheatsheet4.tex Tue Dec 29 19:25:11 2009 +0530 @@ -0,0 +1,75 @@ +\documentclass[12pt]{article} + + +\title{Python: Data Structures} +\author{FOSSEE} +\usepackage{listings} +\lstset{language=Python, + basicstyle=\ttfamily, +commentstyle=\itshape\bfseries, +showstringspaces=false +} +\newcommand{\typ}[1]{\lstinline{#1}} +\usepackage[english]{babel} +\usepackage[latin1]{inputenc} +\usepackage{times} +\usepackage[T1]{fontenc} +\usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} +\usepackage[scaled=.95]{helvet} + +\begin{document} +\date{} +\vspace{-1in} +\begin{center} +\LARGE{Python Development}\\ +\large{FOSSEE} +\end{center} +\section{Module} +Packages like \typ{scipy}, \typ{pylab} etc we used for functions like \typ{plot}, \typ{linspace} are \textbf{Modules}. They are Python script, which have various functions and objects, which can be imported and reused. +\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} +Save above mentioned python script with name 'gcd.py'. Now we can \typ{import} \typ{gcd} function. For example, in same directory create 'lcm.py' with following content: +\begin{lstlisting} +from gcd import gcd + +def lcm(a, b): + return (a * b) / gcd(a, b) + +print lcm(14, 56) +\end{lstlisting} +Here since both gcd.py and lcm.py are in same directory, import statement imports \typ{gcd} function from gcd.py.\\ +When you try to run lcm.py it prints three results, two from gcd.py and third from lcm.py. +\begin{lstlisting} +$ python lcm.py +5 +4 +56 +\end{lstlisting} %$ +\newpage +We have print statements to make sure \typ{gcd} and \typ{lcm} are working properly. So to suppress output of \typ{gcd} module when imported in lcm.py we use \typ{'__main__'} \ +\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} +\typ{__main__()} helps to create standalone scripts. Code inside it is only executed when we run gcd.py. Hence +\begin{lstlisting} +$ python gcd.py +5 +4 +$ python lcm.py +56 +\end{lstlisting} +\end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day2/exercises.tex --- a/day2/exercises.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/exercises.tex Tue Dec 29 19:25:11 2009 +0530 @@ -78,7 +78,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {8 November, 2009\\Day 2} +\date[] {15 December, 2009\\Day 2} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} diff -r 25b18b51be41 -r 2214b5dba4d4 day2/session1.tex --- a/day2/session1.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/session1.tex Tue Dec 29 19:25:11 2009 +0530 @@ -79,7 +79,7 @@ \author[FOSSEE Team] {The FOSSEE Group} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {8 November, 2009\\Day 2, Session 1} +\date[] {15 December, 2009\\Day 2, Session 1} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} diff -r 25b18b51be41 -r 2214b5dba4d4 day2/session2.tex --- a/day2/session2.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/session2.tex Tue Dec 29 19:25:11 2009 +0530 @@ -78,7 +78,7 @@ \author[FOSSEE Team] {The FOSSEE Group} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {8 November, 2009\\Day 2, Session 2} +\date[] {15 December, 2009\\Day 2, Session 2} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -535,4 +535,4 @@ \end{itemize} \end{frame} -\end{document} \ No newline at end of file +\end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day2/session3.tex --- a/day2/session3.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/session3.tex Tue Dec 29 19:25:11 2009 +0530 @@ -78,7 +78,7 @@ \author[FOSSEE Team] {The FOSSEE Group} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {8 November, 2009\\Day 2, Session 3} +\date[] {15 December, 2009\\Day 2, Session 3} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo} @@ -431,4 +431,4 @@ \end{itemize} \end{frame} -\end{document} \ No newline at end of file +\end{document} diff -r 25b18b51be41 -r 2214b5dba4d4 day2/session4.tex --- a/day2/session4.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/session4.tex Tue Dec 29 19:25:11 2009 +0530 @@ -100,7 +100,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {8 November, 2009\\Day 2, Session 4} +\date[] {15 December, 2009\\Day 2, Session 4} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %\pgfdeclareimage[height=0.75cm]{iitblogo}{iitblogo} @@ -553,4 +553,4 @@ % %% \end{block} % % %% \inctime{15} -% %% \end{frame} \ No newline at end of file +% %% \end{frame} diff -r 25b18b51be41 -r 2214b5dba4d4 day2/session5.tex --- a/day2/session5.tex Tue Dec 29 19:02:01 2009 +0530 +++ b/day2/session5.tex Tue Dec 29 19:25:11 2009 +0530 @@ -100,7 +100,7 @@ \author[FOSSEE] {FOSSEE} \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} -\date[] {8 November, 2009\\Day 2, Session 5} +\date[] {15 December, 2009\\Day 2, Session 5} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff -r 25b18b51be41 -r 2214b5dba4d4 day2quiz.tex diff -r 25b18b51be41 -r 2214b5dba4d4 day2quiz2.tex --- a/day2quiz2.tex Tue Dec 29 19:02:01 2009 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,178 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% 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 25b18b51be41 -r 2214b5dba4d4 quiz-day2.tex --- a/quiz-day2.tex Tue Dec 29 19:02:01 2009 +0530 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,178 +0,0 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% 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} -