day1/Session-4.tex
changeset 53 fc74987a475d
parent 50 aabaf8ec0a08
child 54 c3fe152b3539
equal deleted inserted replaced
52:fccb3551c7d5 53:fc74987a475d
    76 \title[Basic Python]{Python:\\Advanced Python data structures, Functions and Debugging}
    76 \title[Basic Python]{Python:\\Advanced Python data structures, Functions and Debugging}
    77 
    77 
    78 \author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}
    78 \author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}
    79 
    79 
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \date[] {10, October 2009}
    81 \date[] {10, October 2009\\Day 1, Session 4}
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    83 
    83 
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    85 %\logo{\pgfuseimage{iitmlogo}}
    85 %\logo{\pgfuseimage{iitmlogo}}
    86 
    86 
   211 
   211 
   212 \begin{frame}[fragile]
   212 \begin{frame}[fragile]
   213   \frametitle{Functions: default arguments}
   213   \frametitle{Functions: default arguments}
   214   \small
   214   \small
   215   \begin{lstlisting}
   215   \begin{lstlisting}
   216 def ask_ok(prompt, retries=4,
   216 def ask_ok(prompt, complaint='Yes or no!'):
   217            complaint='Yes or no!'):
       
   218     while True:
   217     while True:
   219         ok = raw_input(prompt)
   218         ok = raw_input(prompt)
   220         if ok in ('y', 'ye', 'yes'): 
   219         if ok in ('y', 'ye', 'yes'): 
   221             return True
   220             return True
   222         if ok in ('n', 'no', 'nop',
   221         if ok in ('n', 'no', 'nop',
   223                   'nope'): 
   222                   'nope'): 
   224             return False
   223             return False
   225         retries = retries - 1
       
   226         if retries < 0: 
       
   227             raise IOError, 'bad user'
       
   228         print complaint
   224         print complaint
       
   225 
       
   226 ask_ok('?')
       
   227 ask_ok('?', '[Y/N]')
   229   \end{lstlisting}
   228   \end{lstlisting}
   230 \end{frame}
   229 \end{frame}
   231 
   230 
   232 \begin{frame}[fragile]
   231 \begin{frame}[fragile]
   233   \frametitle{Functions: keyword arguments}
   232   \frametitle{Functions: keyword arguments}
   234   \small
   233   \small
   235   \begin{lstlisting}
   234   \begin{lstlisting}
   236 def parrot(voltage, state='a stiff', 
   235 def ask_ok(prompt, complaint='Yes or no!'):
   237            action='voom', type='Royal Blue'):
   236     while True:
   238     print "-- This parrot wouldn't", action,
   237         ok = raw_input(prompt)
   239     print "if you supply", voltage, "Volts."
   238         if ok in ('y', 'ye', 'yes'): 
   240     print "-- Lovely plumage, the", type
   239             return True
   241     print "-- It's", state, "!"
   240         if ok in ('n', 'no', 'nop',
   242 
   241                   'nope'): 
   243 parrot(1000)
   242             return False
   244 parrot(action = 'VOOOOOM', voltage = 1000000)
   243         print complaint
   245 parrot('a thousand',
   244 
   246        state = 'pushing up the daisies')
   245 ask_ok(prompt='?')
   247 parrot('a million', 'bereft of life', 'jump')
   246 ask_ok(prompt='?', complaint='[Y/N]')
   248 \end{lstlisting}
   247 ask_ok(complaint='[Y/N]', prompt='?')
   249 \end{frame}
   248 \end{lstlisting}
   250 
   249 \inctime{15} 
   251 \begin{frame}[fragile]
       
   252   \frametitle{Functions: arbitrary argument lists}
       
   253   \begin{itemize}
       
   254   \item Arbitrary number of arguments using \verb+*args+ or
       
   255     \verb+*whatever+
       
   256   \item Keyword arguments using \verb+**kw+
       
   257   \item Given a tuple/dict how do you call a function?
       
   258     \begin{itemize}
       
   259     \item Using argument unpacking
       
   260     \item For positional arguments: \verb+foo(*[5, 10])+
       
   261     \item For keyword args: \verb+foo(**{'a':5, 'b':10})+
       
   262     \end{itemize}
       
   263   \end{itemize}
       
   264 \end{frame}
       
   265 
       
   266   \begin{frame}[fragile]
       
   267 \begin{lstlisting}
       
   268 def foo(a=10, b=100):
       
   269     print a, b
       
   270 def func(*args, **keyword):
       
   271     print args, keyword
       
   272 # Unpacking:
       
   273 args = [5, 10]
       
   274 foo(*args)
       
   275 kw = {'a':5, 'b':10}
       
   276 foo(**kw)
       
   277 \end{lstlisting}
       
   278     \inctime{15} 
       
   279 \end{frame}
   250 \end{frame}
   280 
   251 
   281 \subsection{Functional programming}
   252 \subsection{Functional programming}
   282 \begin{frame}[fragile]
   253 \begin{frame}[fragile]
   283     \frametitle{Functional programming}
   254     \frametitle{Functional programming}
   284 What is the basic idea?\\
   255 What is the basic idea?\\
   285 Why is it interesting?\\
   256 Why is it interesting?\\
   286 \typ{map, reduce, filter}\\
   257 \typ{map, reduce, filter}\\
   287 list comprehension\\
   258 list comprehension\\
   288 generators
   259 generators
   289     \inctime{15} 
   260 \end{frame}
   290 \end{frame}
   261 
       
   262 \begin{frame}[fragile]
       
   263     \frametitle{List Comprehensions}
       
   264 Lets say we want to squares of all the numbers from 1 to 100
       
   265     \begin{lstlisting}
       
   266 squares = []
       
   267 for i in range(1, 100):
       
   268     squares.append(i * i)
       
   269     \end{lstlisting}
       
   270     \begin{lstlisting}
       
   271 # list comprehension
       
   272 squares = [i*i for i in range(1, 100)]
       
   273      \end{lstlisting}
       
   274 Which is more readable?
       
   275 \end{frame}
       
   276 
       
   277 \begin{frame}[fragile]
       
   278     \frametitle{List Comprehensions}
       
   279 What if you had a more complex function?
       
   280 Lets say we want squares of numbers from 1 to 100 ending in 1, 2, 5, 7 only
       
   281     \begin{lstlisting}
       
   282 squares = []
       
   283 for i in range(1, 100):
       
   284     if i % 10 in [1, 2, 5, 7]:
       
   285         squares.append(i * i)
       
   286     \end{lstlisting}
       
   287     \begin{lstlisting}
       
   288 # list comprehension
       
   289 squares = [i*i for i in range(1, 100)
       
   290            if i % 10 in [1, 2, 5, 7]]
       
   291      \end{lstlisting}
       
   292 Which is more readable?
       
   293 \inctime{15}
       
   294 \end{frame}
       
   295 
   291 
   296 
   292 \subsection{Debugging}
   297 \subsection{Debugging}
   293 \begin{frame}[fragile]
   298 \begin{frame}[fragile]
   294  \frametitle{Errors}
   299  \frametitle{Errors}
   295  \begin{lstlisting}
   300  \begin{lstlisting}
   323     \begin{itemize}
   328     \begin{itemize}
   324         \item  \kwrd{print} based strategy
   329         \item  \kwrd{print} based strategy
   325         \item Process: Hypothesis, test, refine, rinse-repeat
   330         \item Process: Hypothesis, test, refine, rinse-repeat
   326         \item Using \typ{\%debug} and \typ{\%pdb} in IPython
   331         \item Using \typ{\%debug} and \typ{\%pdb} in IPython
   327     \end{itemize}
   332     \end{itemize}
   328     \inctime{15} 
       
   329 \end{frame}
   333 \end{frame}
   330 
   334 
   331 \begin{frame}[fragile]
   335 \begin{frame}[fragile]
   332 \frametitle{Debugging: example}
   336 \frametitle{Debugging: example}
   333 \small
   337 \small
   367 NameError: global name 'spam' is not defined
   371 NameError: global name 'spam' is not defined
   368 > /media/python/iitb/workshops/day1/mymodule.py(2)test()
   372 > /media/python/iitb/workshops/day1/mymodule.py(2)test()
   369       0     print spam
   373       0     print spam
   370 ipdb>
   374 ipdb>
   371 \end{lstlisting}
   375 \end{lstlisting}
       
   376 \inctime{15} 
   372 \end{frame}
   377 \end{frame}
   373 
   378 
   374 \begin{frame}[fragile]
   379 \begin{frame}[fragile]
   375 \frametitle{Debugging: Exercise}
   380 \frametitle{Debugging: Exercise}
   376 \end{frame}
   381 \inctime{10}
   377 
   382 \end{frame}
       
   383 
       
   384 \begin{frame}
       
   385   \frametitle{What did we learn?}
       
   386   \begin{itemize}
       
   387     \item Creating and using Dictionaries
       
   388     \item Creating and using Sets
       
   389     \item Advances Functions: default arguments, keyword arguments
       
   390     \item Functional Programming, list comprehensions
       
   391     \item Errors and Exceptions in Python
       
   392     \item Debugging: How to use pdb, \%pdb and \%debug in IPython
       
   393   \end{itemize}
       
   394 \end{frame}
   378 \end{document}
   395 \end{document}