day2/session2.tex
changeset 297 a835affb1447
parent 288 c4e25269a86c
child 330 46533051b9d3
equal deleted inserted replaced
296:2d08c45681a1 297:a835affb1447
    49 }
    49 }
    50 \newcounter{time}
    50 \newcounter{time}
    51 \setcounter{time}{0}
    51 \setcounter{time}{0}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    53 
    53 
    54 \newcommand{\typ}[1]{\texttt{#1}}
    54 \newcommand{\typ}[1]{\lstinline{#1}}
    55 
    55 
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    57 
    57 
    58 %%% This is from Fernando's setup.
    58 %%% This is from Fernando's setup.
    59 % \usepackage{color}
    59 % \usepackage{color}
   125 
   125 
   126 \section{Control flow}
   126 \section{Control flow}
   127 \subsection{Basic Looping}
   127 \subsection{Basic Looping}
   128 \begin{frame}[fragile]
   128 \begin{frame}[fragile]
   129   \frametitle{\typ{while}}
   129   \frametitle{\typ{while}}
   130 Example: Fibonacci series
   130 \begin{block}{Example: Fibonacci series}
   131   \begin{lstlisting}
   131   Sum of previous two elements defines the next
   132 # the sum of two elements
   132 \end{block}
   133 # defines the next
   133   \begin{lstlisting}
   134 In []: a, b = 0, 1
   134 In []: a, b = 0, 1
   135 In []: while b < 10:
   135 In []: while b < 10:
   136   ...:     print b,
   136   ...:     print b,
   137   ...:     a, b = b, a + b
   137   ...:     a, b = b, a + b
   138   ...:
   138   ...:
   143 
   143 
   144 \begin{frame}[fragile]
   144 \begin{frame}[fragile]
   145 \frametitle{\typ{range()}}
   145 \frametitle{\typ{range()}}
   146 \kwrd{range([start,] stop[, step])}\\
   146 \kwrd{range([start,] stop[, step])}\\
   147 \begin{itemize}
   147 \begin{itemize}
   148   \item range() returns a list of integers
   148   \item \typ{range()} returns a list of integers
   149   \item The \emph{start} and the \emph{step} arguments are optional
   149   \item The \typ{start} and the \typ{step} arguments are optional
   150   \item \emph{stop} argument is not included in the list
   150   \item \typ{stop} is not included in the list
   151 \end{itemize}
   151 \end{itemize}
   152 \vspace*{.5in}
   152 \vspace*{.5in}
   153 \begin{itemize}
   153 \begin{block}{Documentation convention}
   154   \item \alert{Anything within \typ{[]} is optional}
       
   155   \begin{itemize}
   154   \begin{itemize}
   156     \item Nothing to do with Python.
   155     \item \alert{Anything within \typ{[]} is optional}
       
   156     \begin{itemize}
       
   157       \item Nothing to do with Python.
       
   158     \end{itemize}
   157   \end{itemize}
   159   \end{itemize}
   158 \end{itemize}
   160 \end{block}
   159 
   161 \end{frame}
   160 \end{frame}
   162 
   161 
   163 \begin{frame}[fragile]
   162 \begin{frame}[fragile]
   164   \frametitle{\texttt{for} \ldots \typ{range()}}
   163   \frametitle{\typ{for} \ldots \typ{range()}}
   165 Example: print squares of first \typ{5} numbers
   164 Example: print squares of first \typ{n} numbers
       
   165   \begin{lstlisting}
   166   \begin{lstlisting}
   166 In []: for i in range(5):
   167 In []: for i in range(5):
   167  ....:     print i, i * i
   168  ....:     print i, i * i
   168  ....:
   169  ....:
   169  ....:
   170  ....:
   174 4 16
   175 4 16
   175 \end{lstlisting}
   176 \end{lstlisting}
   176 \end{frame}
   177 \end{frame}
   177 
   178 
   178 \begin{frame}[fragile]
   179 \begin{frame}[fragile]
   179   \frametitle{\typ{for} \ldots \typ{range()}}
   180   \frametitle{\texttt{for} \ldots \typ{range()}}
   180 Example: print squares of odd numbers from 3 to 9
   181 Example: print squares of odd numbers from 3 to 9
   181   \begin{lstlisting}
   182   \begin{lstlisting}
   182 In []: for i in range(3, 10, 2):
   183 In []: for i in range(3, 10, 2):
   183  ....:     print i, i * i
   184  ....:     print i, i * i
   184  ....:
   185  ....:
   238 \begin{frame}[fragile]
   239 \begin{frame}[fragile]
   239   \frametitle{Lists: methods}
   240   \frametitle{Lists: methods}
   240   \begin{lstlisting}
   241   \begin{lstlisting}
   241 In []: num = [1, 2, 3, 4]
   242 In []: num = [1, 2, 3, 4]
   242 
   243 
       
   244 In []: num + [9, 10, 11]
       
   245 Out[]: [1, 2, 3, 4, 9, 10, 11]
       
   246 
   243 In []: num.append([9, 10, 11])
   247 In []: num.append([9, 10, 11])
   244 
   248 
   245 In []: num
   249 In []: num
   246 Out[]: [1, 2, 3, 4, [9, 10, 11]]
   250 Out[]: [1, 2, 3, 4, [9, 10, 11]]
   247   \end{lstlisting}
   251   \end{lstlisting}
   323 In []: t = (1, 2, 3, 4, 5, 6, 7, 8)
   327 In []: t = (1, 2, 3, 4, 5, 6, 7, 8)
   324 
   328 
   325 In []: t[0] + t[3] + t[-1]
   329 In []: t[0] + t[3] + t[-1]
   326 Out[]: 13
   330 Out[]: 13
   327 
   331 
   328 # Try the following!
       
   329 In []: t[4] = 7 
   332 In []: t[4] = 7 
   330 \end{lstlisting}
   333 \end{lstlisting}
   331 \pause
   334 \pause
   332 \begin{block}{Note:}
   335 \begin{block}{Note:}
   333 \begin{itemize}
   336 \begin{itemize}
   359 
   362 
   360 In []: player['Avg']
   363 In []: player['Avg']
   361 Out[]: 52.530000000000001
   364 Out[]: 52.530000000000001
   362   \end{lstlisting}
   365   \end{lstlisting}
   363   \begin{block}{Note!}
   366   \begin{block}{Note!}
   364     Duplicate keys are not allowed!\\
   367     Duplicate keys $\Rightarrow$ overwritten!\\
   365     Dictionaries are iterable through keys.
   368     You can iterate through a dictionary using keys.
   366   \end{block}
   369   \end{block}
   367 \end{frame}
   370 \end{frame}
   368 
   371 
   369 \begin{frame}[fragile]
   372 \begin{frame}[fragile]
   370   \frametitle{Dictionaries: containership}
   373   \frametitle{Dictionaries: containership}
   373 Out[]: True
   376 Out[]: True
   374 
   377 
   375 In []: 'Econ' in player
   378 In []: 'Econ' in player
   376 Out[]: False
   379 Out[]: False
   377   \end{lstlisting}
   380   \end{lstlisting}
       
   381   \begin{block}{Note}
       
   382     \begin{itemize}
       
   383       \item We can check for the containership of keys only
       
   384       \item Not values
       
   385     \end{itemize}
       
   386   \end{block}
   378 \end{frame}
   387 \end{frame}
   379 
   388 
   380 \begin{frame}[fragile]
   389 \begin{frame}[fragile]
   381   \frametitle{Dictionaries: methods}
   390   \frametitle{Dictionaries: methods}
   382   \begin{lstlisting}
   391   \begin{lstlisting}
   383 In []: player.keys()
   392 In []: player.keys()
   384 Out[]: ['Runs', 'Inn', 'Avg', 'Mat']
   393 Out[]: ['Runs', 'Inn', 'Avg', 'Mat']
   385 
   394 
   386 In []: player.values()
   395 In []: player.values()
   387 Out[]: [10823, 233, 52.530000000000001, 134]
   396 Out[]: [10823, 233, 
       
   397         52.530000000000001, 134]
   388   \end{lstlisting}
   398   \end{lstlisting}
   389 \end{frame}
   399 \end{frame}
   390 
   400 
   391 \begin{frame} {Problem Set 2.1: Problem 2.1.1}
   401 \begin{frame} {Problem Set 2.1: Problem 2.1.1}
   392 You are given date strings of the form ``29, Jul 2009'', or ``4 January 2008''. In other words a number, a string and another number, with a comma sometimes separating the items.\\Write a function that takes such a string and returns a tuple (yyyy, mm, dd) where all three elements are ints.
   402 You are given date strings of the form ``29 Jul, 2009'', or ``4 January 2008''. In other words a number, a string and another number, with a comma sometimes separating the items.\\Write a function that takes such a string and returns a tuple (yyyy, mm, dd) where all three elements are ints.
   393 \end{frame}
   403 \end{frame}
   394 
   404 
   395 \subsection{Set}
   405 \subsection{Sets}
   396 \begin{frame}[fragile]
   406 \begin{frame}[fragile]
   397   \frametitle{Set}
   407   \frametitle{Sets}
   398     \begin{itemize}
   408     \begin{itemize}
   399       \item Simplest container, mutable
   409       \item Simplest container, mutable
   400       \item No ordering, no duplicates
   410       \item No ordering, no duplicates
   401       \item usual suspects: union, intersection, subset \ldots
   411       \item usual suspects: union, intersection, subset \ldots
   402       \item >, >=, <, <=, in, \ldots
   412       \item >, >=, <, <=, in, \ldots
   445 Out[]: 5
   455 Out[]: 5
   446 \end{lstlisting}
   456 \end{lstlisting}
   447 \end{frame}
   457 \end{frame}
   448 
   458 
   449 \begin{frame}
   459 \begin{frame}
   450   \frametitle{Problem set 2.2}
   460   \frametitle{Problem set 2.2: Problem 2.2.1}
   451   \begin{description}
   461 Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these?
   452     \item[2.2.1] Given a dictionary of the names of students and their marks, identify how many duplicate marks are there? and what are these?
       
   453 \end{description}
       
   454 \inctime{15}
       
   455 \end{frame}
   462 \end{frame}
   456 
   463 
   457 \begin{frame}
   464 \begin{frame}
   458   \frametitle{Problem set 2.2}
   465   \frametitle{Problem 2.2.2}
   459   \begin{description}
   466 Given a list of words, find all the anagrams in the list.
   460     \item[2.2.2] Given a list of words, find all the anagrams in the list
   467 
   461 \end{description}
       
   462 \inctime{15}
   468 \inctime{15}
   463 \end{frame}
   469 \end{frame}
   464 
   470 
   465 \section{Functions}
   471 \section{Functions}
   466 \begin{frame}[fragile]
   472 \begin{frame}[fragile]
   467   \frametitle{Functions}
   473   \frametitle{Functions}
   468   \begin{itemize}
   474   \begin{itemize}
   469     \item \kwrd{def} - keyword to define a function
   475     \item \kwrd{def} - keyword to define a function
   470     \item Arguments are local to a function
   476     \item Arguments are local to a function
   471     \item Docstrings are important!
       
   472     \item Functions can return multiple values
   477     \item Functions can return multiple values
   473   \end{itemize}
   478   \end{itemize}
   474 \end{frame}
   479 \end{frame}
   475 
   480 
   476 \begin{frame}[fragile]
   481 \begin{frame}[fragile]
   485     elif r > 0:
   490     elif r > 0:
   486         return 1
   491         return 1
   487     else:
   492     else:
   488         return 0
   493         return 0
   489   \end{lstlisting}
   494   \end{lstlisting}
       
   495   \emphbar{Note docstrings}
   490 \end{frame}
   496 \end{frame}
   491 
   497 
   492 \begin{frame}[fragile]
   498 \begin{frame}[fragile]
   493   \frametitle {What does this function do?}
   499   \frametitle {What does this function do?}
   494   \begin{lstlisting}
   500   \begin{lstlisting}
   515 
   521 
   516 \begin{frame}
   522 \begin{frame}
   517   \frametitle{What did we learn?}
   523   \frametitle{What did we learn?}
   518   \begin{itemize}
   524   \begin{itemize}
   519     \item Loops: \kwrd{while}, \kwrd{for}
   525     \item Loops: \kwrd{while}, \kwrd{for}
   520     \item Advanced Data structures
   526     \item Advanced Data structures:
       
   527     \begin{itemize}
       
   528       \item Lists
       
   529       \item Tuples
       
   530       \item Dictionaries
       
   531       \item Sets
       
   532     \end{itemize}
   521     \item Functions
   533     \item Functions
       
   534     \item Docstrings
   522   \end{itemize}
   535   \end{itemize}
   523 \end{frame}
   536 \end{frame}
   524 
   537 
   525 \end{document}
   538 \end{document}