day1/Session-4.tex
changeset 86 f657495cf8b2
parent 85 8ca53181bee6
child 87 cd29428cd8f5
equal deleted inserted replaced
85:8ca53181bee6 86:f657495cf8b2
   110 
   110 
   111 \begin{frame}
   111 \begin{frame}
   112   \titlepage
   112   \titlepage
   113 \end{frame}
   113 \end{frame}
   114 
   114 
   115 \section{Advanced Data structures, Functions and Debugging}
   115 \section{Advanced Data structures}
   116 
   116 
   117 \subsection{Dictionary}
   117 \subsection{Dictionary}
   118 \begin{frame}{Dictionary}
   118 \begin{frame}{Dictionary}
   119   \begin{itemize}
   119   \begin{itemize}
   120     \item lists and tuples index: 0 \ldots n
   120     \item lists and tuples index: 0 \ldots n
   121     \item dictionaries index using strings
   121     \item dictionaries index using strings
   122     \item \typ{ d = \{ ``Hitchhiker's guide'' : 42, ``Terminator'' : ``I'll be back''\}}
   122     \item \typ{ d = \{ ``Hitchhiker's guide'' : 42, ``Terminator'' : ``I'll be back''\}}
   123     \item \typ{d[``Terminator'']\\``I'll be back''}
   123     \item \typ{d[``Terminator''] => ``I'll be back''}
   124     \item aka associative array, key-value pair, hashmap, hashtable \ldots    
   124     \item aka associative array, key-value pair, hashmap, hashtable \ldots    
   125     \item what can be keys?
   125     \item what can be keys?
   126   \end{itemize}
   126   \end{itemize}
   127 \end{frame}
   127 \end{frame}
   128 
   128 
   129 \begin{frame}{Dictionary \ldots }
   129 \begin{frame}{Dictionary \ldots }
   130   \begin{itemize}
   130   \begin{itemize}
   131     \item \alert{Unordered}
   131     \item \alert{Unordered}
   132       \begin{block}{Standard usage}
   132       \begin{block}{Standard usage}
   133         for key in dict:\\
   133         for key in dict:\\
   134             <use> dict[key] \# => value
   134         \ \ \ \ print dict[key]
   135       \end{block}
   135       \end{block}
   136     \item \typ{d.keys()} returns a list
   136     \item \typ{d.keys()} returns a list
   137     \item can we have duplicate keys?
   137     \item can we have duplicate keys?
   138   \end{itemize}
   138   \end{itemize}
   139   \inctime{5}
   139   \inctime{5}
   187 5
   187 5
   188 \end{lstlisting}
   188 \end{lstlisting}
   189 \inctime{5}
   189 \inctime{5}
   190 \end{frame}
   190 \end{frame}
   191 
   191 
   192 
       
   193 \begin{frame}
   192 \begin{frame}
   194   \frametitle{Problem set 6.2}
   193   \frametitle{Problem set 6.2}
   195   \begin{description}
   194   \begin{description}
   196     \item[6.2.1] Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these?
   195     \item[6.2.1] Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these?
   197     \item[6.2.2] Given a string of the form ``4-7, 9, 12, 15'' find the numbers missing in this list for a given range.
   196     \item[6.2.2] Given a string of the form ``4-7, 9, 12, 15'' find the numbers missing in this list for a given range.
   198 \end{description}
   197 \end{description}
   199 \inctime{10}
   198 \inctime{10}
   200 \end{frame}
   199 \end{frame}
   201 
   200 
   202 \subsection{Functions Reloaded!}
   201 
       
   202 \section{Functions Reloaded!}
   203 \begin{frame}[fragile]
   203 \begin{frame}[fragile]
   204     \frametitle{Advanced functions}
   204     \frametitle{Advanced functions}
   205     \begin{itemize}
   205     \begin{itemize}
   206         \item default args
   206         \item default args
   207         \item var args
   207         \item var args
   209         \item scope
   209         \item scope
   210         \item \typ{global}
   210         \item \typ{global}
   211       \end{itemize}
   211       \end{itemize}
   212 \end{frame}
   212 \end{frame}
   213 
   213 
       
   214 \subsection{Default arguments}
   214 \begin{frame}[fragile]
   215 \begin{frame}[fragile]
   215   \frametitle{Functions: default arguments}
   216   \frametitle{Functions: default arguments}
   216   \small
   217   \small
   217   \begin{lstlisting}
   218   \begin{lstlisting}
   218 def ask_ok(prompt, complaint='Yes or no!'):
   219 def ask_ok(prompt, complaint='Yes or no!'):
   228 ask_ok('?')
   229 ask_ok('?')
   229 ask_ok('?', '[Y/N]')
   230 ask_ok('?', '[Y/N]')
   230   \end{lstlisting}
   231   \end{lstlisting}
   231 \end{frame}
   232 \end{frame}
   232 
   233 
       
   234 \subsection{Keyword arguments}
   233 \begin{frame}[fragile]
   235 \begin{frame}[fragile]
   234   \frametitle{Functions: keyword arguments}
   236   \frametitle{Functions: keyword arguments}
   235   \small
   237   \small
   236   \begin{lstlisting}
   238   \begin{lstlisting}
   237 def ask_ok(prompt, complaint='Yes or no!'):
   239 def ask_ok(prompt, complaint='Yes or no!'):
   249 ask_ok(complaint='[y/n]', prompt='?')
   251 ask_ok(complaint='[y/n]', prompt='?')
   250 \end{lstlisting}
   252 \end{lstlisting}
   251 \inctime{15} 
   253 \inctime{15} 
   252 \end{frame}
   254 \end{frame}
   253 
   255 
   254 \subsection{Functional programming}
   256 \section{Functional programming}
   255 \begin{frame}[fragile]
   257 \begin{frame}[fragile]
   256     \frametitle{Functional programming}
   258     \frametitle{Functional programming}
   257     \begin{itemize}
   259     \begin{itemize}
   258       \item What is the basic idea?
   260       \item What is the basic idea?
   259       \item Why is it interesting?
   261       \item Why is it interesting?
   261       \item list comprehension
   263       \item list comprehension
   262       \item generators
   264       \item generators
   263     \end{itemize}
   265     \end{itemize}
   264 \end{frame}
   266 \end{frame}
   265 
   267 
       
   268 \subsection{List comprehensions}
   266 \begin{frame}[fragile]
   269 \begin{frame}[fragile]
   267     \frametitle{List Comprehensions}
   270     \frametitle{List Comprehensions}
   268 Lets say we want to squares of all the numbers from 1 to 100
   271 Lets say we want to squares of all the numbers from 1 to 100
   269     \begin{lstlisting}
   272     \begin{lstlisting}
   270 squares = []
   273 squares = []
   292 # list comprehension
   295 # list comprehension
   293 squares = [i*i for i in range(1, 100)
   296 squares = [i*i for i in range(1, 100)
   294            if i % 10 in [1, 2, 5, 7]]
   297            if i % 10 in [1, 2, 5, 7]]
   295      \end{lstlisting}
   298      \end{lstlisting}
   296 Which is more readable?
   299 Which is more readable?
       
   300 \end{frame}
       
   301 
       
   302 \begin{frame}[fragile]
       
   303     \frametitle{map() function}
       
   304     map() function accomplishes the same as list comprehensions
       
   305     \begin{lstlisting}
       
   306 >>> def square(x): return x*x
       
   307 ...
       
   308 >>> map(square, range(1, 100))
       
   309 [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
       
   310     \end{lstlisting}
   297 \inctime{15}
   311 \inctime{15}
   298 \end{frame}
   312 \end{frame}
   299 
   313 
   300 
   314 \section{Debugging}
   301 \subsection{Debugging}
   315 \subsection{Errors and Exceptions}
   302 \begin{frame}[fragile]
   316 \begin{frame}[fragile]
   303  \frametitle{Errors}
   317  \frametitle{Errors}
   304  \begin{lstlisting}
   318  \begin{lstlisting}
   305 >>> while True print 'Hello world'
   319 >>> while True print 'Hello world'
   306  \end{lstlisting}
   320  \end{lstlisting}
   338 ZeroDivisionError: integer division 
   352 ZeroDivisionError: integer division 
   339 or modulo by zero
   353 or modulo by zero
   340 \end{lstlisting}
   354 \end{lstlisting}
   341 \end{frame}
   355 \end{frame}
   342 
   356 
       
   357 \subsection{Strategy}
   343 \begin{frame}[fragile]
   358 \begin{frame}[fragile]
   344     \frametitle{Debugging effectively}
   359     \frametitle{Debugging effectively}
   345     \begin{itemize}
   360     \begin{itemize}
   346         \item \kwrd{print} based strategy
   361         \item \kwrd{print} based strategy
   347         \item Process:
   362         \item Process:
   350 \end{frame}
   365 \end{frame}
   351 
   366 
   352 \begin{frame}[fragile]
   367 \begin{frame}[fragile]
   353     \frametitle{Debugging effectively}
   368     \frametitle{Debugging effectively}
   354     \begin{itemize}
   369     \begin{itemize}
   355       \item Using \typ{\%debug} and \typ{\%pdb} in IPython
   370       \item Using \typ{\%debug} in IPython
   356     \end{itemize}
   371     \end{itemize}
   357 \end{frame}
   372 \end{frame}
   358 
   373 
   359 \begin{frame}[fragile]
   374 \begin{frame}[fragile]
   360 \frametitle{Debugging in IPython}
   375 \frametitle{Debugging in IPython}
   362 \begin{lstlisting}
   377 \begin{lstlisting}
   363 In [1]: import mymodule
   378 In [1]: import mymodule
   364 In [2]: mymodule.test()
   379 In [2]: mymodule.test()
   365 ---------------------------------------------
   380 ---------------------------------------------
   366 NameError   Traceback (most recent call last)
   381 NameError   Traceback (most recent call last)
   367 /media/python/iitb/workshops/day1/<ipython console> in <module>()
   382 <ipython console> in <module>()
   368 /media/python/iitb/workshops/day1/mymodule.py in test()
   383 mymodule.py in test()
   369       1 def test():
   384       1 def test():
   370 ----> 2     print spam
   385 ----> 2     print spam
   371 NameError: global name 'spam' is not defined
   386 NameError: global name 'spam' is not defined
       
   387 
   372 In [3]: %debug
   388 In [3]: %debug
   373 > /media/python/iitb/workshops/day1/mymodule.py(2)test()
   389 > mymodule.py(2)test()
   374       0     print spam
   390       0     print spam
   375 ipdb> 
   391 ipdb> 
   376 \end{lstlisting}
   392 \end{lstlisting}
   377 \inctime{15} 
   393 \inctime{15} 
   378 \end{frame}
   394 \end{frame}
   379 
   395 
       
   396 \subsection{Exercise}
   380 \begin{frame}[fragile]
   397 \begin{frame}[fragile]
   381 \frametitle{Debugging: Exercise}
   398 \frametitle{Debugging: Exercise}
       
   399 \small
       
   400 \begin{lstlisting}
       
   401 import keyword
       
   402 f = open('/path/to/file')
       
   403 
       
   404 freq = {}
       
   405 for line in f:
       
   406     words = line.split()
       
   407     for word in words:
       
   408         key = word.strip(',.!;?()[]: ')
       
   409         if keyword.iskeyword(key):
       
   410             value = freq[key]
       
   411             freq[key] = value + 1
       
   412 
       
   413 print freq
       
   414 \end{lstlisting}
   382 \inctime{10}
   415 \inctime{10}
   383 \end{frame}
   416 \end{frame}
   384 
   417 
   385 \begin{frame}
   418 \begin{frame}
   386   \frametitle{What did we learn?}
   419   \frametitle{What did we learn?}
   387   \begin{itemize}
   420   \begin{itemize}
   388     \item Creating and using Dictionaries
   421     \item Dictionaries
   389     \item Creating and using Sets
   422     \item Sets
   390     \item Advances Functions: default arguments, keyword arguments
   423     \item Default and keyword arguments
   391     \item Functional Programming, list comprehensions
   424     \item Functional Programming, list comprehensions
   392     \item Errors and Exceptions in Python
   425     \item Errors and Exceptions in Python
   393     \item Debugging: \%pdb and \%debug in IPython
   426     \item Debugging: \%debug in IPython
   394   \end{itemize}
   427   \end{itemize}
   395 \end{frame}
   428 \end{frame}
   396 \end{document}
   429 \end{document}