day1/Session-3.tex
changeset 83 be8c9a71135f
parent 79 04b620d3f172
child 106 7b539cba0f04
equal deleted inserted replaced
82:e352b67ab357 83:be8c9a71135f
   110   \titlepage
   110   \titlepage
   111 \end{frame}
   111 \end{frame}
   112 
   112 
   113 \section{\typ{for}, Lists and Tuples}
   113 \section{\typ{for}, Lists and Tuples}
   114 
   114 
   115 \begin{frame}
   115 \begin{frame}{Quick Recap}
   116   {Problem set 3}
   116   \begin{itemize}
   117   As you can guess, idea is to use \kwrd{for}!
   117     \item List indexing and slicing
       
   118     \item The \kwrd{range()} function
       
   119     \item \kwrd{for}
       
   120     \item Iterating lists and tuples using \kwrd{for} and \kwrd{range()}
       
   121   \end{itemize}
   118 \end{frame}
   122 \end{frame}
   119 
   123 
   120 \begin{frame}{Problem 3.1}
   124 \begin{frame}{Problem 3.1}
   121   Which of the earlier problems is simpler when we use \kwrd{for} instead of \kwrd{while}? 
   125   Which of the earlier problems is simpler when we use \kwrd{for} instead of \kwrd{while}? 
   122 \end{frame}
   126 \end{frame}
   139   3.0 ,  3.5,  4.0 ,  4.5,  5.0 ]
   143   3.0 ,  3.5,  4.0 ,  4.5,  5.0 ]
   140 \end{lstlisting}
   144 \end{lstlisting}
   141 \end{frame}
   145 \end{frame}
   142 
   146 
   143 \begin{frame}[fragile]
   147 \begin{frame}[fragile]
   144   \frametitle{Problem 3.4a}
   148   \frametitle{Problem 3.4}
   145 
   149 
   146 Use the \typ{linspace} function and generate a list of N tuples of the form\\
   150 Use the \typ{linspace} function and generate a list of N tuples of the form\\
   147 \typ{[($x_1$,f($x_1$)),($x_2$,f($x_2$)),\ldots,($x_N$,f($x_N$))]}\\for the following functions,\begin{itemize}
   151 \typ{[($x_1$,f($x_1$)),($x_2$,f($x_2$)),\ldots,($x_N$,f($x_N$))]}\\for the following functions,\begin{itemize}
   148   \item \typ{f(x) = sin(x)}
   152   \item \typ{f(x) = sin(x)}
   149   \item \typ{f(x) = sin(x) + sin(10*x)}.
   153   \item \typ{f(x) = sin(x) + sin(10*x)}.
   150 \end{itemize}
   154 \end{itemize}
   151 \end{frame}
   155 \end{frame}
   152 
   156 
   153 \begin{frame}[fragile]
   157 \begin{frame}[fragile]
   154   \frametitle{Problem 3.4b}
   158   \frametitle{Problem 3.5}
   155 
   159 
   156   Using the tuples generated earlier, determine the intervals where the roots of the functions lie.
   160   Using the tuples generated earlier, determine the intervals where the roots of the functions lie.
   157 
   161 
   158   \inctime{15}
   162   \inctime{15}
   159 \end{frame}
   163 \end{frame}
   160 
   164 
   161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   162 % TIME: 15 m, running 185m 
   166 % TIME: 15 m, running 185m 
   163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   164 
   168 
   165 \section{Input/Output}
   169 \section{Parsing}
   166 
   170 
   167 \begin{frame}[fragile]
   171 \begin{frame}[fragile]
   168   \frametitle{Simple tokenizing and parsing}
   172   \frametitle{Simple tokenizing and parsing}
   169   \begin{lstlisting}
   173   \begin{lstlisting}
   170 s = """The quick brown fox jumped
   174 s = """The quick brown fox jumped
   180   \begin{lstlisting}
   184   \begin{lstlisting}
   181     [1,3,4,5,6,7,12,15,18,19,20,21]
   185     [1,3,4,5,6,7,12,15,18,19,20,21]
   182   \end{lstlisting}
   186   \end{lstlisting}
   183 \end{frame}
   187 \end{frame}
   184 
   188 
       
   189 \section{Input/Output}
       
   190 
   185 \begin{frame}[fragile]
   191 \begin{frame}[fragile]
   186   \frametitle{File handling}
   192   \frametitle{File handling}
   187 \begin{lstlisting}
   193 \begin{lstlisting}
   188 >>> f = open('/path/to/file_name')
   194 >>> f = open('/path/to/file_name')
   189 >>> data = f.read() # Read entire file.
   195 >>> data = f.read() # Read entire file.
   203 \end{frame}
   209 \end{frame}
   204 
   210 
   205 \begin{frame}[fragile]
   211 \begin{frame}[fragile]
   206     \frametitle{File and \kwrd{for}}
   212     \frametitle{File and \kwrd{for}}
   207 \begin{lstlisting}
   213 \begin{lstlisting}
   208 >>> f = open('/path/to/file_name')
   214 $ cat dummyfile
       
   215 One 1
       
   216 Two 2
       
   217 Three 3
       
   218 Four 4
       
   219 Five 5
       
   220 Six 6
       
   221 Seven 7
       
   222 Eight 8
       
   223 Nine 9
       
   224 Ten 10
       
   225 \end{lstlisting}
       
   226 \end{frame}
       
   227 
       
   228 \begin{frame}[fragile]
       
   229     \frametitle{File and \kwrd{for}}
       
   230 \begin{lstlisting}
       
   231 >>> f = open('dummyfile')
   209 >>> for line in f:
   232 >>> for line in f:
   210 ...     print line
   233 ...     print line
   211 ...
   234 ...
   212 \end{lstlisting}
   235 \end{lstlisting}
       
   236 \end{frame}
       
   237 
       
   238 \begin{frame}[fragile]
       
   239     \frametitle{File and \kwrd{for}}
       
   240 \begin{lstlisting}
       
   241 In [1]: f = open('dummyfile')
       
   242 
       
   243 In [2]: for line in f:
       
   244    ...:     print line
       
   245    ...:  
       
   246 \end{lstlisting}
       
   247 
       
   248 \begin{columns}
       
   249   \column{0.3\textwidth}
       
   250 
       
   251 \begin{lstlisting}
       
   252 One 1
       
   253 
       
   254 Two 2
       
   255 
       
   256 Three 3
       
   257 \end{lstlisting}
       
   258   \column{0.6\textwidth}
       
   259 \pause
       
   260 \begin{block}{What happens when ...}
       
   261 the \kwrd{print line} is replaced by \kwrd{print line,}
       
   262 \end{block}
       
   263 \end{columns}
       
   264 \ldots
   213 \end{frame}
   265 \end{frame}
   214 
   266 
   215 \begin{frame}{Problem 4.2}
   267 \begin{frame}{Problem 4.2}
   216     The given file has lakhs of records in the form:\\
   268     The given file has lakhs of records in the form:\\
   217     \typ{RGN;ID;NAME;MARK1;\ldots;MARK5;TOTAL;PFW}\\
   269     \typ{RGN;ID;NAME;MARK1;\ldots;MARK5;TOTAL;PFW}\\
   342 
   394 
   343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   344 % TIME: 25 m, running 230m 
   396 % TIME: 25 m, running 230m 
   345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   346 
   398 
   347 \section{Coding Style in Python}
   399 \section{Coding Style}
   348 \begin{frame}{Readability and Consistency}
   400 \begin{frame}{Readability and Consistency}
   349     \begin{itemize}
   401     \begin{itemize}
   350         \item Readability Counts!-Code is read more often than its written.
   402         \item Readability Counts!\\Code is read more often than its written.
   351         \item Consistency!
   403         \item Consistency!
   352         \item Know when to be inconsistent.
   404         \item Know when to be inconsistent.
   353       \end{itemize}
   405       \end{itemize}
   354 \end{frame}
   406 \end{frame}
   355 
   407 
   384   \begin{itemize}
   436   \begin{itemize}
   385         \item When to write docstrings?
   437         \item When to write docstrings?
   386         \item Ending the docstrings
   438         \item Ending the docstrings
   387         \item One liner docstrings
   439         \item One liner docstrings
   388    \end{itemize}
   440    \end{itemize}
       
   441 More information at PEP8: http://www.python.org/dev/peps/pep-0008/
   389 \inctime{10}
   442 \inctime{10}
   390 \end{frame}
   443 \end{frame}
   391 
   444 
   392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   445 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   393 % TIME: 10 m, running 240m 
   446 % TIME: 10 m, running 240m 
   394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   447 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   395 
   448 
   396 \section{Objects}
   449 \section{Objects}
   397 \begin{frame}{Objects in Python}
   450 \begin{frame}{Objects in general}
   398     \begin{itemize}
   451     \begin{itemize}
   399         \item What is an Object? (Types and classes)
   452         \item What is an Object? (Types and classes)
   400         \item identity
   453         \item identity
   401         \item type
   454         \item type
   402         \item method
   455         \item method
   403       \end{itemize}
   456       \end{itemize}
   404 \end{frame}
   457 \end{frame}
   405 
   458 
   406 \begin{frame}{Everything is an Object!}
   459 \begin{frame}{Almost everything is an Object!}
   407   \begin{itemize}
   460   \begin{itemize}
   408     \item \typ{list, tuple}
   461     \item \typ{list}
   409     \item \typ{string, dictionary}
   462     \item \typ{tuple}
       
   463     \item \typ{string}
       
   464     \item \typ{dictionary}
   410     \item \typ{function}
   465     \item \typ{function}
   411     \item Of course, user defined class objects!
   466     \item Of course, user defined class objects!
   412   \end{itemize}
   467   \end{itemize}
   413 \end {frame}
   468 \end {frame}
   414 
   469 
   427 In [3]: l.<tab>
   482 In [3]: l.<tab>
   428   \end{lstlisting}
   483   \end{lstlisting}
   429 \end{frame}
   484 \end{frame}
   430 
   485 
   431 \begin{frame}[fragile]
   486 \begin{frame}[fragile]
   432   \frametitle{Objects provide a certain consistency}
   487   \frametitle{Objects provide consistency}
   433   \small
   488   \small
   434   \begin{lstlisting}
   489   \begin{lstlisting}
   435 for element in (1, 2, 3):
   490 for element in (1, 2, 3):
   436     print element
   491     print element
   437 for key in {'one':1, 'two':2}:
   492 for key in {'one':1, 'two':2}:
   453 
   508 
   454 \section{Summary}
   509 \section{Summary}
   455 
   510 
   456 \begin{frame}{What have we learnt so far?}
   511 \begin{frame}{What have we learnt so far?}
   457   \begin{itemize}
   512   \begin{itemize}
   458   \item Writing to and Reading from files using \typ{for}
   513   \item Operating on lists and tuples using \kwrd{for}
       
   514   \item Simple string tokenizing and parsing
       
   515   \item Writing to and Reading from files using \kwrd{for}
   459   \item Using and writing Python Modules
   516   \item Using and writing Python Modules
   460   \item Coding Style
   517   \item Coding Style
   461   \item Objects in Python
   518   \item Objects in Python
   462   \end{itemize}
   519   \end{itemize}
   463 \end{frame}
   520 \end{frame}