# HG changeset patch # User Santosh G. Vattam # Date 1257922567 -19800 # Node ID 49bdffe4dca565fed8f7e8ee3899bde0fa2a6b60 # Parent f87f2a310abedf155ba88942712f042e7653f37e Updated session 2 slides of day 1 and added cheatsheets of day 2. diff -r f87f2a310abe -r 49bdffe4dca5 day1/session2.tex --- a/day1/session2.tex Tue Nov 10 17:22:07 2009 +0530 +++ b/day1/session2.tex Wed Nov 11 12:26:07 2009 +0530 @@ -126,7 +126,7 @@ \section{Plotting Points} \begin{frame}[fragile] \frametitle{Why would I plot f(x)?} -How often do we plot analytical functions?\\We plot experimental data more. +Do we plot analytical functions or experimental data? \begin{small} \begin{lstlisting} In []: x = [0, 1, 2, 3] @@ -204,8 +204,10 @@ \begin{frame}[fragile] \frametitle{Lists: Initializing \& accessing elements} \begin{lstlisting} -In []: mtlist = [] #Empty List - +In []: mtlist = [] +\end{lstlisting} +\emphbar{Empty List} +\begin{lstlisting} In []: a = [ 1, 2, 3, 4, 5] In []: a[0]+a[1]+a[-1] @@ -219,9 +221,11 @@ \kwrd{In []: a = [ 1, 2, 3, 4, 5]} \end{block} \begin{lstlisting} -In []: a[1:3] # A slice. +In []: a[1:3] Out[]: [2, 3] - +\end{lstlisting} +\emphbar{A slice} +\begin{lstlisting} In []: a[1:-1] Out[]: [2, 3, 4] \end{lstlisting} @@ -254,15 +258,15 @@ \begin{tabular}{| c | c | c |} \hline $L$ & $T$ & $T^2$ \\ \hline -0.1 & 0.6900 & \\ \hline -0.2 & 0.8989 & \\ \hline -0.3 & 1.1867 & \\ \hline -0.4 & 1.2991 & \\ \hline -0.5 & 1.4656 & \\ \hline -0.6 & 1.5843 & \\ \hline -0.7 & 1.7706 & \\ \hline -0.8 & 1.8296 & \\ \hline -0.9 & 1.9440 & \\ \hline +0.1 & 0.69 & \\ \hline +0.2 & 0.90 & \\ \hline +0.3 & 1.19 & \\ \hline +0.4 & 1.30 & \\ \hline +0.5 & 1.47 & \\ \hline +0.6 & 1.58 & \\ \hline +0.7 & 1.77 & \\ \hline +0.8 & 1.83 & \\ \hline +0.9 & 1.94 & \\ \hline \end{tabular} \end{small}\\ \alert{$L \alpha T^2$} @@ -275,9 +279,9 @@ In []: l = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] -In []: t = [0.69, 0.8989, 1.1867, - 1.2991, 1.4656, 1.5843, - 1.7706, 1.8296, 1.9440] +In []: t = [0.69, 0.90, 1.19, + 1.30, 1.47, 1.58, + 1.77, 1.83, 1.94] \end{lstlisting} \end{frame} @@ -305,15 +309,15 @@ \end{frame} \begin{frame}[fragile] - \frametitle{How to come out of the \textt{for} loop?} - Hit the ``enter'' key twice to come to the previous indentation level + \frametitle{How to come out of the \texttt{for} loop?} + Hit the ``ENTER'' key twice to come to the previous indentation level \begin{lstlisting} In []: for time in t: ....: tsq.append(time*time) ....: ....: - In []: + In []: print tsq \end{lstlisting} \end{frame} @@ -338,7 +342,7 @@ \end{lstlisting} \ldots \begin{block}{Windows users:} - > type pendulum.txt + C:> type pendulum.txt \end{block} \end{frame} @@ -358,9 +362,9 @@ l = [] t = [] for line in open('pendulum.txt'): - points = line.split() - l.append(float(points[0])) - t.append(float(points[1])) + point = line.split() + l.append(float(point[0])) + t.append(float(point[1])) tsq = [] for time in t: tsq.append(time*time) @@ -416,7 +420,9 @@ \begin{lstlisting} In []: line = '1.2000e-01 7.4252e-01' -In []: line.split() +In []: point = line.split() + +In []: point Out[]: ['1.2000e-01', '7.4252e-01'] \end{lstlisting} \end{frame} @@ -424,12 +430,12 @@ \begin{frame}[fragile] \frametitle{Getting floats from strings} \begin{lstlisting} -In []: type(points[0]) +In []: type(point[0]) Out[]: \end{lstlisting} But, we need floating point numbers \begin{lstlisting} -In []: t = float(points[0]) +In []: t = float(point[0]) In []: type(t) Out[]: @@ -442,9 +448,9 @@ l = [] t = [] for line in open('pendulum.txt'): - points = line.split() - l.append(float(points[0])) - t.append(float(points[1])) + point = line.split() + l.append(float(point[0])) + t.append(float(point[1])) tsq = [] for time in t: tsq.append(time*time) @@ -467,6 +473,7 @@ \item Lists \item \kwrd{for} \item Reading files + \item Tokenizing \item Strings \end{itemize} \end{frame} diff -r f87f2a310abe -r 49bdffe4dca5 day2/cheatsheet1.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day2/cheatsheet1.tex Wed Nov 11 12:26:07 2009 +0530 @@ -0,0 +1,136 @@ +\documentclass[12pt]{article} + + +\title{Python: Basics} +\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: Basics}\\ +\large{FOSSEE} +\end{center} +\section{Data types} +Complex Numbers +\begin{lstlisting} +In []: c = 3+4j +In []: abs(c) +Out[]: 5.0 +In []: c.imag +Out[]: 4.0 +In []: c.real +Out[]: 3.0 +\end{lstlisting} +Boolean +\begin{lstlisting} +In []: a = False +In []: b = True +In []: c = True +In []: (a and b) or c +Out[]: True +\end{lstlisting} +Strings + \begin{lstlisting} +In []: w = "hello" +In []: print w[0] + w[2] + w[-1] +Out[]: hlo +In []: len(w) +Out[]: 5 +In []: w[0] = 'H' # ERROR: Strings are immutable + \end{lstlisting} +String methods + \begin{lstlisting} +In []: a = 'Hello World' +In []: a.startswith('Hell') # 'a' starts with 'Hell' +In []: a.endswith('ld') # 'a' ends with 'ld' +In []: a.upper() # all characters to upper case +In []: a.lower() # all characters to lower case +In []: ''.join(['a', 'b', 'c']) +Out[]: 'abc' + \end{lstlisting} +String formatting + \begin{lstlisting} +In []: x, y = 1, 1.234 +In []: 'x is %s, y is %s' %(x, y) +Out[]: 'x is 1, y is 1.234' + \end{lstlisting} +Arithmetic Operators + \begin{lstlisting} +In []: 45 % 2 # Modulo operator +Out[]: 1 +In []: 1234567891234567890 ** 3 # Power +In []: a = 5 +In []: a += 1 +In []: a *= 2 + \end{lstlisting} +String Operations +\begin{lstlisting} +In []: s = 'Hello' +In []: p = 'World' +In []: s + p +Out[]: 'HelloWorld' +In []: s * 4 +Out[]: 'HelloHelloHelloHello' +\end{lstlisting} +Relational and Logical Operators +\begin{lstlisting} +In []: p, z, n = 1, 0, -1 +In []: p == n +Out[]: False +In []: p >= n +Out[]: True +In []: n < z < p +Out[]: True +In []: p + n != z +Out[]: False +\end{lstlisting} +Built-ins +\begin{lstlisting} +In []: int(17 / 2.0) +Out[]: 8 +In []: float(17 / 2) +Out[]: 8.0 +In []: str(17 / 2.0) +Out[]: '8.5' +In []: round( 7.5 ) +Out[]: 8.0 +\end{lstlisting} +Console Input +\begin{lstlisting} +In []: a = raw_input('Enter a value: ') +Enter a value: 5 +\end{lstlisting} +\section{Conditionals} +\typ{if} +\begin{lstlisting} +In []: x = int(raw_input("Enter an integer:")) +In []: if x < 0: + ...: print 'Be positive!' + ...: elif x == 0: + ...: print 'Zero' + ...: elif x == 1: + ...: print 'Single' + ...: else: + ...: print 'More' +\end{lstlisting} +Ternary Operator +\begin{lstlisting} +In []: a = raw_input('Enter number(Q to quit):') +In []: num = int(a) if a != 'Q' else 0 +\end{lstlisting} +\end{document} diff -r f87f2a310abe -r 49bdffe4dca5 day2/cheatsheet2.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day2/cheatsheet2.tex Wed Nov 11 12:26:07 2009 +0530 @@ -0,0 +1,89 @@ +\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: Data Structures}\\ +\large{FOSSEE} +\end{center} +\section{Basic Looping} +\typ{while} + \begin{lstlisting} +In []: a, b = 0, 1 +In []: while b < 10: + ...: print b, + ...: a, b = b, a + b # Fibonacci Sequence +\end{lstlisting} +\typ{for} and \typ{range}\\ +\typ{range([start,] stop[, step])} +\begin{lstlisting} +In []: for i in range(3, 10, 2): + ....: print i, i * i +3 9 +5 25 +7 49 +9 81 +\end{lstlisting} +List methods (Contd.) +\begin{lstlisting} +In []: num = [1, 2, 3, 4] +In []: num.append([9, 10, 11]) +In []: num +Out[]: [1, 2, 3, 4, [9, 10, 11]] +In []: num = [1, 2, 3, 4] +In []: num.extend([5, 6, 7]) +In []: num +Out[]: [1, 2, 3, 4, 5, 6, 7] +In []: num.reverse() +In []: num +Out[]: [7, 6, 5, 4, 3, 2, 1] +In []: num.remove(6) +In []: num +\end{lstlisting} +Slicing: \typ{list[initial:final:step]} +\begin{lstlisting} +In []: a[1:-1:2] +Out[]: [2, 4] +In []: a[::2] +Out[]: [1, 3, 5] +In []: a[-1::-1] +Out[]: [5, 4, 3, 2, 1] +\end{lstlisting} +Tuples(Immutable lists) +\begin{lstlisting} +In []: t = (1, 2, 3, 4, 5, 6, 7, 8) +In []: t[0] + t[3] + t[-1] +Out[]: 13 +In []: t[4] = 7 # ERROR: tuples are immutable +\end{lstlisting} +Sets +\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. +\end{lstlisting} +\end{document} + diff -r f87f2a310abe -r 49bdffe4dca5 day2/cheatsheet3.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day2/cheatsheet3.tex Wed Nov 11 12:26:07 2009 +0530 @@ -0,0 +1,65 @@ +\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: Functions and Objects}\\ +\large{FOSSEE} +\end{center} +\section{Functions} +Function definition + \begin{lstlisting} +def signum( r ): + if r < 0: + return -1 + elif r > 0: + return 1 + else: + return 0 + \end{lstlisting} +Default Arguments +\begin{lstlisting} +def welcome(greet, name='world!'): + print greet, name +\end{lstlisting} +Keyword Arguments +\begin{lstlisting} +In []: plot(y, sin(y), 'g', linewidth=2) +\end{lstlisting} +Self contained python script + \begin{lstlisting} +from scipy import linspace, pi, sin +from pylab import plot, legend, annotate +from pylab import xlim, ylim + +x = linspace(-5*pi, 5*pi, 500) +plot(x, x, 'b') +plot(x, -x, 'b') +plot(x, sin(x), 'g', linewidth=2) +plot(x, x*sin(x), 'r', linewidth=3) +legend(['x', '-x', 'sin(x)', 'xsin(x)']) +annotate('origin', xy = (0, 0)) +xlim(-5*pi, 5*pi) +ylim(-5*pi, 5*pi) + \end{lstlisting} + +\end{document}