Merged branches.
authorSantosh G. Vattam <vattam.santosh@gmail.com>
Fri, 09 Oct 2009 16:59:30 +0530
changeset 91 aa271f590e24
parent 90 18687fadbc08 (current diff)
parent 89 98ebba820e91 (diff)
child 92 d9c31aacd835
Merged branches.
Binary file day1/DebugginDiagram.png has changed
--- a/day1/Session-2.tex	Fri Oct 09 16:59:02 2009 +0530
+++ b/day1/Session-2.tex	Fri Oct 09 16:59:30 2009 +0530
@@ -87,11 +87,11 @@
 
 %% Delete this, if you do not want the table of contents to pop up at
 %% the beginning of each subsection:
-\AtBeginSubsection[]
+\AtBeginSection[]
 {
   \begin{frame}<beamer>
     \frametitle{Outline}
-    \tableofcontents[currentsection,currentsubsection]
+    \tableofcontents[currentsection,subsections]
   \end{frame}
 }
 
@@ -110,9 +110,9 @@
   \titlepage
 \end{frame}
 
-\section{Functions and basic data structures}
+\section{Control Flow}
 
-\subsection{Exercises on Control flow}
+\subsection{Exercises}
 \begin{frame}
   \frametitle{Problem set 1}
   \begin{itemize}
@@ -123,7 +123,8 @@
 
 \begin{frame}{Problem 1.1}
   Write a program that displays all three digit numbers that are equal to the sum of the cubes of their digits. That is, print numbers $abc$ that have the property $abc = a^3 + b^3 + c^3$\\
-These are called $Armstrong$ numbers.
+\vspace*{0.2in}
+\emphbar{These are called $Armstrong$ numbers.}
 \end{frame}
   
 \begin{frame}{Problem 1.2 - Collatz sequence}
@@ -147,7 +148,7 @@
   \end{lstlisting}
 The number of lines must be obtained from the user as input.\\
 \pause
-When can your code fail?
+\emphbar{When can your code fail?}
 \only<2->{\inctime{20}}
 \end{frame}
 
@@ -155,7 +156,8 @@
 % TIME: 20 m, running 20m 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-\subsection{Functions}
+\section{Functions}
+\subsection{Defining}
 \begin{frame}[fragile]
 \frametitle{Functions: examples}
   \begin{lstlisting}
@@ -185,7 +187,7 @@
     return padSize * SPACE + s
   \end{lstlisting}
 \pause
-What about \%3d?
+\emphbar{What about \% formatting?}
 \end{frame}
 
 \begin{frame}[fragile]
@@ -229,10 +231,11 @@
   \end{lstlisting}
 \end{frame}
 
+\subsection{Built-in functions}
 \begin{frame}
   {Before writing a function}
   \begin{itemize}
-      \item Builtin functions for various and sundry
+      \item Variety of builtin functions are available
       \item \typ{abs, any, all, len, max, min}
       \item \typ{pow, range, sum, type}
       \item Refer here:
@@ -244,32 +247,29 @@
 % TIME: 10 m, running 30m 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-\begin{frame}{Problem set 2}
-  The focus is on writing functions and calling them.
-\end{frame}
-
-\begin{frame}{Problem 2.1}
+\subsection{Exercises}
+\begin{frame}{Problem set 2: Problem 2.1}
   Write a function to return the gcd of two numbers.
 \end{frame}
 
 \begin{frame}{Problem 2.2}
-A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and should not be displayed by our program. \\
-Write a program to print primitive pythagorean triads. The program should generate all triads with a, b values in the range 0---100
+Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\
+A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and is also not primitive.
 \end{frame}
 
 \begin{frame}{Problem 2.3}
-  Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\\For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).
+  Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).}
 \end{frame}
 
 \begin{frame}{Problem 2.4}
-  The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, the aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\
+  The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\
   Write a function that returns the aliquot number of a given number. 
 \end{frame}
 
 \begin{frame}{Problem 2.5}
   A pair of numbers (a, b) is said to be \alert{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\
   Example: \texttt{220, 284}\\
-  Write a program that prints all five digit amicable pairs.
+  Write a program that prints all four digit amicable pairs.
   \inctime{25}
 \end{frame}
 
@@ -277,8 +277,9 @@
 % TIME: 25 m, running 55m 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-\subsection{Lists}
+\section{Lists}
 
+\subsection{Manipulating}
 \begin{frame}[fragile]
   \frametitle{List creation and indexing}
 \begin{lstlisting}
@@ -286,8 +287,8 @@
 >>> a = [1, 2, 3, 4] # More useful.
 >>> len(a) 
 4
->>> a[0] + a[1] + a[2] + a[-1]
-10
+>>> a[0] + a[1] + a[-1]
+7
 \end{lstlisting}
   \begin{itemize}
   \item Indices start with ?
@@ -323,7 +324,7 @@
 >>> a[-1::-1]
 \end{lstlisting}
 What do you think the last one will do?
-  \emphbar{Note: Strings also use same indexing and slicing.}
+  \emphbar{Strings also use same indexing and slicing.}
 \end{frame}
 
 \begin{frame}[fragile]
@@ -366,6 +367,7 @@
 % TIME: 10 m, running 65m 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+\subsection{Methods}
 \begin{frame}[fragile]
   \frametitle{List methods}
 \begin{lstlisting}
@@ -403,6 +405,7 @@
 % TIME: 5 m, running 70m 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+\section{Tuples}
 \begin{frame}[fragile]
   \frametitle{Tuples: immutable}
 \begin{lstlisting}
@@ -412,7 +415,8 @@
 >>> t[0] = 1
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
-TypeError: object does not support item assignment
+TypeError: object does not support
+item assignment
 \end{lstlisting}  
 \begin{itemize}
     \item Multiple return values are actually a tuple.
@@ -425,6 +429,7 @@
 % TIME: 5 m, running 75m 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+\section{for and range()}
 \begin{frame}[fragile]
   \frametitle{\typ{range()} function}
   \begin{lstlisting}
@@ -494,12 +499,13 @@
 \begin{frame}
   \frametitle{What did we learn?}
   \begin{itemize}
-    \item Defining functions and calling them
-    \item Lists: Creating, Indexing, Slicing and List methods
+    \item Control flow in action
+    \item Functions
+    \item Manipulating Lists 
     \item Tuples
     \item range() function
     \item for loops
-    \item iterating lists with for, for...range()
+    \item for...range() idiom
   \end{itemize}
 \end{frame}
 \end{document}
--- a/day1/Session-4.tex	Fri Oct 09 16:59:02 2009 +0530
+++ b/day1/Session-4.tex	Fri Oct 09 16:59:30 2009 +0530
@@ -89,11 +89,11 @@
 
 %% Delete this, if you do not want the table of contents to pop up at
 %% the beginning of each subsection:
-\AtBeginSubsection[]
+\AtBeginSection[]
 {
   \begin{frame}<beamer>
     \frametitle{Outline}
-    \tableofcontents[currentsection,currentsubsection]
+    \tableofcontents[currentsection,subsections]
   \end{frame}
 }
 
@@ -112,7 +112,7 @@
   \titlepage
 \end{frame}
 
-\section{Advanced Data structures, Functions and Debugging}
+\section{Advanced Data structures}
 
 \subsection{Dictionary}
 \begin{frame}{Dictionary}
@@ -120,7 +120,7 @@
     \item lists and tuples index: 0 \ldots n
     \item dictionaries index using strings
     \item \typ{ d = \{ ``Hitchhiker's guide'' : 42, ``Terminator'' : ``I'll be back''\}}
-    \item \typ{d[``Terminator'']\\``I'll be back''}
+    \item \typ{d[``Terminator''] => ``I'll be back''}
     \item aka associative array, key-value pair, hashmap, hashtable \ldots    
     \item what can be keys?
   \end{itemize}
@@ -131,7 +131,7 @@
     \item \alert{Unordered}
       \begin{block}{Standard usage}
         for key in dict:\\
-            <use> dict[key] \# => value
+        \ \ \ \ print dict[key]
       \end{block}
     \item \typ{d.keys()} returns a list
     \item can we have duplicate keys?
@@ -189,7 +189,6 @@
 \inctime{5}
 \end{frame}
 
-
 \begin{frame}
   \frametitle{Problem set 6.2}
   \begin{description}
@@ -199,7 +198,8 @@
 \inctime{10}
 \end{frame}
 
-\subsection{Functions Reloaded!}
+
+\section{Functions Reloaded!}
 \begin{frame}[fragile]
     \frametitle{Advanced functions}
     \begin{itemize}
@@ -211,6 +211,7 @@
       \end{itemize}
 \end{frame}
 
+\subsection{Default arguments}
 \begin{frame}[fragile]
   \frametitle{Functions: default arguments}
   \small
@@ -230,6 +231,7 @@
   \end{lstlisting}
 \end{frame}
 
+\subsection{Keyword arguments}
 \begin{frame}[fragile]
   \frametitle{Functions: keyword arguments}
   \small
@@ -251,16 +253,19 @@
 \inctime{15} 
 \end{frame}
 
-\subsection{Functional programming}
+\section{Functional programming}
 \begin{frame}[fragile]
     \frametitle{Functional programming}
-What is the basic idea?\\
-Why is it interesting?\\
-\typ{map, reduce, filter}\\
-list comprehension\\
-generators
+    \begin{itemize}
+      \item What is the basic idea?
+      \item Why is it interesting?
+      \item \typ{map, reduce, filter}
+      \item list comprehension
+      \item generators
+    \end{itemize}
 \end{frame}
 
+\subsection{List comprehensions}
 \begin{frame}[fragile]
     \frametitle{List Comprehensions}
 Lets say we want to squares of all the numbers from 1 to 100
@@ -295,8 +300,8 @@
 \inctime{15}
 \end{frame}
 
-
-\subsection{Debugging}
+\section{Debugging}
+\subsection{Errors and Exceptions}
 \begin{frame}[fragile]
  \frametitle{Errors}
  \begin{lstlisting}
@@ -338,6 +343,7 @@
 \end{lstlisting}
 \end{frame}
 
+\subsection{Strategy}
 \begin{frame}[fragile]
     \frametitle{Debugging effectively}
     \begin{itemize}
@@ -350,7 +356,7 @@
 \begin{frame}[fragile]
     \frametitle{Debugging effectively}
     \begin{itemize}
-      \item Using \typ{\%debug} and \typ{\%pdb} in IPython
+      \item Using \typ{\%debug} in IPython
     \end{itemize}
 \end{frame}
 
@@ -362,33 +368,51 @@
 In [2]: mymodule.test()
 ---------------------------------------------
 NameError   Traceback (most recent call last)
-/media/python/iitb/workshops/day1/<ipython console> in <module>()
-/media/python/iitb/workshops/day1/mymodule.py in test()
+<ipython console> in <module>()
+mymodule.py in test()
       1 def test():
 ----> 2     print spam
 NameError: global name 'spam' is not defined
+
 In [3]: %debug
-> /media/python/iitb/workshops/day1/mymodule.py(2)test()
+> mymodule.py(2)test()
       0     print spam
 ipdb> 
 \end{lstlisting}
 \inctime{15} 
 \end{frame}
 
+\subsection{Exercise}
 \begin{frame}[fragile]
 \frametitle{Debugging: Exercise}
+\small
+\begin{lstlisting}
+import keyword
+f = open('/path/to/file')
+
+freq = {}
+for line in f:
+    words = line.split()
+    for word in words:
+        key = word.strip(',.!;?()[]: ')
+        if keyword.iskeyword(key):
+            value = freq[key]
+            freq[key] = value + 1
+
+print freq
+\end{lstlisting}
 \inctime{10}
 \end{frame}
 
 \begin{frame}
   \frametitle{What did we learn?}
   \begin{itemize}
-    \item Creating and using Dictionaries
-    \item Creating and using Sets
-    \item Advances Functions: default arguments, keyword arguments
+    \item Dictionaries
+    \item Sets
+    \item Default and keyword arguments
     \item Functional Programming, list comprehensions
     \item Errors and Exceptions in Python
-    \item Debugging: \%pdb and \%debug in IPython
+    \item Debugging: \%debug in IPython
   \end{itemize}
 \end{frame}
 \end{document}
--- a/day1/debug_exercise.py	Fri Oct 09 16:59:02 2009 +0530
+++ b/day1/debug_exercise.py	Fri Oct 09 16:59:30 2009 +0530
@@ -1,3 +1,13 @@
-def no_bug():
-    for i in range(10):
-        if 
+import keyword
+f = open('/path/to/file')
+
+freq = {}
+for line in f:
+    words = line.split()
+    for word in words:
+        key = word.strip(',.!;?()[]: ')
+        if keyword.iskeyword(key):
+            value = freq[key]
+            freq[key] = value + 1
+
+print freq
--- a/day1/exercise/kwfreq.py	Fri Oct 09 16:59:02 2009 +0530
+++ b/day1/exercise/kwfreq.py	Fri Oct 09 16:59:30 2009 +0530
@@ -1,5 +1,5 @@
 import keyword
-f = open('/home/madhu/pyprogs/pytriads.py')
+f = open('/path/to/file')
 
 freq = {}
 for line in f:
@@ -7,7 +7,7 @@
     for word in words:
         key = word.strip(',.!;?()[]: ')
         if keyword.iskeyword(key):
-            value = freq.get(key, 1)
+            value = freq[key]
             freq[key] = value + 1
 
 print freq
--- a/day2/session2.tex	Fri Oct 09 16:59:02 2009 +0530
+++ b/day2/session2.tex	Fri Oct 09 16:59:30 2009 +0530
@@ -338,12 +338,12 @@
   Numerically solve ODEs\\
   \begin{align*}
   \frac{dx}{dt}&=-e^{-t}x^2\\ 
-           x(0)&=2    
+           x&=2 \quad at \ t=0
   \end{align*}
   \begin{lstlisting}
 >>> def dx_dt(x,t):
         return -np.exp(-t)*x**2
-
+>>> t=np.linspace(0,2,100)
 >>> x=integrate.odeint(dx_dt, 2, t)
 >>> plt.plot(x,t)
   \end{lstlisting}
@@ -372,8 +372,6 @@
   \frametitle{Interpolation - Splines}
   Plot the Cubic Spline of $sin(x)$
   \begin{lstlisting}
->>> x = np.arange(0,2*np.pi,np.pi/4)
->>> y = np.sin(x)
 >>> tck = interpolate.splrep(x,y)
 >>> X = np.arange(0,2*np.pi,np.pi/50)
 >>> Y = interpolate.splev(X,tck,der=0)
--- a/quiz.tex	Fri Oct 09 16:59:02 2009 +0530
+++ b/quiz.tex	Fri Oct 09 16:59:30 2009 +0530
@@ -1,11 +1,209 @@
-\documentclass[a4paper,10pt]{book}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Tutorial slides on Python.
+%
+% Author: FOSSEE <info at fossee  dot in>
+% Copyright (c) 2005-2009, FOSSEE Team
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+\documentclass[14pt,compress]{beamer}
+
+\mode<presentation>
+{
+  \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}}}  }
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% 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, Session 0}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 
 \begin{document}
-Which version of Python were you using? 
-List some key differences between IPython and Vanilla Python
-What is the biggest integer number that can be represented by Python?
-What is the result of 17.0 / 2?
-What does '*' * 40 produce?
- 
+
+\begin{frame}
+  \titlepage
+\end{frame}
+
+\begin{frame}{}
+  What is the largest integer value that can be represented by Python?
+\end{frame}
+
+\begin{frame}{}
+  What is the result of 17.0 / 2?
+\end{frame}
+
+\begin{frame}{}
+  What does '*' * 40 produce?
+\end{frame}
+
+\begin{frame}{}
+  Which of the following is not a type in Python?
+  \begin{enumerate}
+    \item int
+    \item float
+    \item char
+    \item string
+  \end{enumerate}
+\end{frame}
+
+\begin{frame}[fragile]{}
+  What happens when we run this code?
+  \begin{lstlisting}
+a = False
+b = True
+c = True
+if a and b or c:
+    print "You are correct!"
+  \end{lstlisting}
+\end{frame}
+
+\begin{frame}{}
+  What is the difference between \kwrd{print} \emph{x} and \kwrd{print} \emph{x,} ?
+\end{frame}
+
+\begin{frame}{}
+  A sample line from a Comma Separated Values (CSV) file:\\
+  \vspace*{0.2in}
+  \emph{Rossum, Guido, 42, 56, 34, 54}\\
+  \vspace*{0.2in}
+  What method would you use to separate the line into fields?
+\end{frame}
+
+\begin{frame}{}
+  How many items can a function return?
+\end{frame}
+
+\begin{frame}[fragile]{}
+  \begin{lstlisting}
+  >>> a = [1, 2, 5, 9]
+  >>> a[:-1]
+  \end{lstlisting}
+  What is the output?
+\end{frame}
+
+\begin{frame}{}
+  How do you get the alternate elements of a list?
+\end{frame}
+
+\begin{frame}{}
+  How do you combine the two lists \emph{a} and \emph{b}?
+\end{frame}
+
+\begin{frame}{}
+  How do you find the presence of an element \emph{x} in the list \emph{a}?
+\end{frame}
+
+\begin{frame}[fragile]{}
+  \begin{lstlisting}
+  >>> a = (1, 2, 5, 7)
+  >>> a[1]
+  \end{lstlisting}
+  What is the output?
+\end{frame}
+
+\begin{frame}[fragile]{}
+  \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}
+  by doing a small modification in the same two lines of code above?
+\end{frame}
+
+\begin{frame}{}
+  What can you use as the keys of a dictionary?
+\end{frame}
+
+\begin{frame}[fragile]{}
+  \begin{lstlisting}
+  >>> d = {
+      'a': 1,
+      'b': 2
+      }
+  >>> print d['c']
+  \end{lstlisting}
+  What is the output?
+\end{frame}
+
+\begin{frame}{}
+  How do you obtain all the keys of the dictionary?
+  \pause
+  \\all the values?
+\end{frame}
+
+\begin{frame}[fragile]{}
+  \begin{lstlisting}
+  >>> set([1, 2, 8, 2, 13, 8, 9])
+  \end{lstlisting}
+  What is the output?
+\end{frame}
+
 \end{document}
+
+\begin{enumerate}
+  \item Which version of Python were you using?
+  \item List some key differences between IPython and Vanilla Python
+  \item What is the biggest integer number that can be represented by Python?
+  \item What is the result of 17.0 / 2?
+  \item What does '*' * 40 produce?
+  \item List all the basic types available in Python.
+  \item What happens when we run this code?
+  a = False
+  b = True
+  c = True
+  if a and b or c:
+      print ``You are correct!''
+  \item Select last 3 alternative elements in any given list.
+  \item Give the difference between print x and print x,
+  \item A single line of CSV file should be separated into fields. What method would you use to achieve this?
+  \item How many items can a function return?
+  \item If function returns more than one item/object what is the return type of the function?
+  \item How do you document a function?
+  \item Given a list l, what will its slice l[:-1] evaluate to?
+  \item How do you get a slice of the list where the slice has only alternate elements?
+  \item How do you add another list at the end of a given list?
+  \item How do you find if a given element is present in the list or not?
+  \item You are given a tuple a = (1, 2, 5, 7). What happens when you do a[1] = 3?
+  \item We use for to loop through the list elements. What do we have to do if we want to iterate through the elements of the list as well as get the index of the elements of the list as we iterate through?
+  \item What is the difference between import math and from math import *?
+  \item List at least 5 Standard Library Modules.
+  \item How do you create a Python module of your own?
+  \item What can be the keys of a dictionary?
+  \item What happens when you try to access a key in the dictionary that does not exist?
+  \item How do you avoid such an exception?
+  \item How do you obtain all the keys of the dictionary?
+  \item How do you obtain all the values of the dictionary?
+  \item What will the set contain when you create a set from a list containing duplicate elements?
+  \item Name any 2 types of Exception.
+  \item Whats are the 2 IPython command you use for debugging?
+\end{enumerate}
\ No newline at end of file