day1/Session-3.tex~
changeset 46 63704b5650f1
equal deleted inserted replaced
45:3b8be02d94d4 46:63704b5650f1
       
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
     2 % Tutorial slides on Python.
       
     3 %
       
     4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in>
       
     5 % Copyright (c) 2005-2008, Prabhu Ramachandran
       
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
     7 
       
     8 \documentclass[14pt,compress]{beamer}
       
     9 %\documentclass[draft]{beamer}
       
    10 %\documentclass[compress,handout]{beamer}
       
    11 %\usepackage{pgfpages} 
       
    12 %\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
       
    13 
       
    14 % Modified from: generic-ornate-15min-45min.de.tex
       
    15 \mode<presentation>
       
    16 {
       
    17   \usetheme{Warsaw}
       
    18   \useoutertheme{split}
       
    19   \setbeamercovered{transparent}
       
    20 }
       
    21 
       
    22 \usepackage[english]{babel}
       
    23 \usepackage[latin1]{inputenc}
       
    24 %\usepackage{times}
       
    25 \usepackage[T1]{fontenc}
       
    26 
       
    27 % Taken from Fernando's slides.
       
    28 \usepackage{ae,aecompl}
       
    29 \usepackage{mathpazo,courier,euler}
       
    30 \usepackage[scaled=.95]{helvet}
       
    31 
       
    32 \definecolor{darkgreen}{rgb}{0,0.5,0}
       
    33 
       
    34 \usepackage{listings}
       
    35 \lstset{language=Python,
       
    36     basicstyle=\ttfamily,
       
    37     commentstyle=\color{red}\itshape,
       
    38   stringstyle=\color{darkgreen},
       
    39   showstringspaces=false,
       
    40   keywordstyle=\color{blue}\bfseries}
       
    41 
       
    42 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    43 % Macros
       
    44 \setbeamercolor{emphbar}{bg=blue!20, fg=black}
       
    45 \newcommand{\emphbar}[1]
       
    46 {\begin{beamercolorbox}[rounded=true]{emphbar} 
       
    47       {#1}
       
    48  \end{beamercolorbox}
       
    49 }
       
    50 \newcounter{time}
       
    51 \setcounter{time}{0}
       
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
       
    53 
       
    54 \newcommand{\typ}[1]{\texttt{#1}}
       
    55 
       
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
       
    57 
       
    58 %%% This is from Fernando's setup.
       
    59 % \usepackage{color}
       
    60 % \definecolor{orange}{cmyk}{0,0.4,0.8,0.2}
       
    61 % % Use and configure listings package for nicely formatted code
       
    62 % \usepackage{listings}
       
    63 % \lstset{
       
    64 %    language=Python,
       
    65 %    basicstyle=\small\ttfamily,
       
    66 %    commentstyle=\ttfamily\color{blue},
       
    67 %    stringstyle=\ttfamily\color{orange},
       
    68 %    showstringspaces=false,
       
    69 %    breaklines=true,
       
    70 %    postbreak = \space\dots
       
    71 % }
       
    72 
       
    73 
       
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    75 % Title page
       
    76 \title[Basic Python]{Python:\\Modular Code, Handling Files and Objects}
       
    77 
       
    78 \author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}
       
    79 
       
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
       
    81 \date[] {10, October 2009}
       
    82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
    83 
       
    84 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
       
    85 %\logo{\pgfuseimage{iitmlogo}}
       
    86 
       
    87 
       
    88 %% Delete this, if you do not want the table of contents to pop up at
       
    89 %% the beginning of each subsection:
       
    90 \AtBeginSubsection[]
       
    91 {
       
    92   \begin{frame}<beamer>
       
    93     \frametitle{Outline}
       
    94     \tableofcontents[currentsection,currentsubsection]
       
    95   \end{frame}
       
    96 }
       
    97 
       
    98 
       
    99 % If you wish to uncover everything in a step-wise fashion, uncomment
       
   100 % the following command: 
       
   101 %\beamerdefaultoverlayspecification{<+->}
       
   102 
       
   103 %\includeonlyframes{current,current1,current2,current3,current4,current5,current6}
       
   104 
       
   105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   106 % DOCUMENT STARTS
       
   107 \begin{document}
       
   108 
       
   109 \begin{frame}
       
   110   \titlepage
       
   111 \end{frame}
       
   112 
       
   113 \section{Python}
       
   114 
       
   115 \begin{frame}
       
   116   {Problem set 3}
       
   117   As you can guess, idea is to use \kwrd{for}!
       
   118 \end{frame}
       
   119 
       
   120 \begin{frame}{Problem 3.1}
       
   121   Which of the earlier problems is simpler when we use \kwrd{for} instead of \kwrd{while}? 
       
   122 \end{frame}
       
   123 
       
   124 \begin{frame}{Problem 3.2}
       
   125   Given an empty chessboard and one Bishop placed in any square, say (r, c), generate the list of all squares the Bishop could move to.
       
   126 \end{frame}
       
   127 
       
   128 \begin{frame}[fragile]
       
   129   \frametitle{Problem 3.3}
       
   130 
       
   131   Given two real numbers \typ{a, b}, and an integer \typ{N}, write a
       
   132   function named \typ{linspace( a, b, N)} that returns an ordered list
       
   133   of \typ{N} points starting with \typ{a} and ending in \typ{b} and
       
   134   equally spaced.\\
       
   135 
       
   136   For example, \typ{linspace(0, 5, 11)}, should return, \\
       
   137 \begin{lstlisting}
       
   138 [ 0.0 ,  0.5,  1.0 ,  1.5,  2.0 ,  2.5,  
       
   139   3.0 ,  3.5,  4.0 ,  4.5,  5.0 ]
       
   140 \end{lstlisting}
       
   141 \end{frame}
       
   142 
       
   143 \begin{frame}[fragile]
       
   144   \frametitle{Problem 3.4a (optional)}
       
   145 
       
   146 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}
       
   148   \item \typ{f(x) = sin(x)}
       
   149   \item \typ{f(x) = sin(x) + sin(10*x)}.
       
   150 \end{itemize}
       
   151 \end{frame}
       
   152 
       
   153 \begin{frame}[fragile]
       
   154   \frametitle{Problem 3.4b (optional)}
       
   155 
       
   156   Using the tuples generated earlier, determine the intervals where the roots of the functions lie.
       
   157 
       
   158   \inctime{15}
       
   159 \end{frame}
       
   160 
       
   161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   162 % TIME: 15 m, running 185m 
       
   163 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   164 
       
   165 \subsection{IO}
       
   166 
       
   167 \begin{frame}[fragile]
       
   168   \frametitle{Simple tokenizing and parsing}
       
   169   \begin{lstlisting}
       
   170 s = """The quick brown fox jumped
       
   171        over the lazy dog"""
       
   172 for word in s.split():
       
   173     print word.capitalize()
       
   174   \end{lstlisting}
       
   175 \end{frame}
       
   176 
       
   177 \begin{frame}[fragile]
       
   178   \frametitle{Problem 4.1}
       
   179   Given a string like, ``1, 3-7, 12, 15, 18-21'', produce the list \\
       
   180   \begin{lstlisting}
       
   181     [1,3,4,5,6,7,12,15,18,19,20,21]
       
   182   \end{lstlisting}
       
   183 \end{frame}
       
   184 
       
   185 \begin{frame}[fragile]
       
   186   \frametitle{File handling}
       
   187 \begin{lstlisting}
       
   188 >>> f = open('/path/to/file_name')
       
   189 >>> data = f.read() # Read entire file.
       
   190 >>> line = f.readline() # Read one line.
       
   191 >>> f.close() # close the file.
       
   192 \end{lstlisting}
       
   193 Writing files
       
   194 \begin{lstlisting}
       
   195 >>> f = open('/path/to/file_name', 'w')
       
   196 >>> f.write('hello world\n')
       
   197 >>> f.close()
       
   198 \end{lstlisting}
       
   199 \begin{itemize}
       
   200     \item Everything read or written is a string
       
   201 \end{itemize}
       
   202 \emphbar{Try \typ{file?} for more help}
       
   203 \end{frame}
       
   204 
       
   205 \begin{frame}[fragile]
       
   206     \frametitle{File and \kwrd{for}}
       
   207 \begin{lstlisting}
       
   208 >>> f = open('/path/to/file_name')
       
   209 >>> for line in f:
       
   210 ...     print line
       
   211 ...
       
   212 \end{lstlisting}
       
   213 \end{frame}
       
   214 
       
   215 \begin{frame}{Problem 4.2}
       
   216     The given file has lakhs of records in the form:\\
       
   217     \typ{RGN;ID;NAME;MARK1;\ldots;MARK5;TOTAL;PFW}\\
       
   218     Some entries may be empty.  Read the data from this file and print the
       
   219     name of the student with the maximum total marks.
       
   220 \end{frame}
       
   221 
       
   222 \begin{frame}{Problem 4.3}
       
   223     For the same data file compute the average marks in different
       
   224     subjects, the student with the maximum mark in each subject and also
       
   225     the standard deviation of the marks.  Do this efficiently.
       
   226 
       
   227     \inctime{20}
       
   228 \end{frame}
       
   229 
       
   230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   231 % TIME: 20 m, running 205m 
       
   232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   233 
       
   234 \subsection{Modules}
       
   235 
       
   236 \begin{frame}[fragile]
       
   237     {Modules}
       
   238 \begin{lstlisting}
       
   239 >>> sqrt(2)
       
   240 Traceback (most recent call last):
       
   241   File "<stdin>", line 1, in <module>
       
   242 NameError: name 'sqrt' is not defined
       
   243 >>> import math        
       
   244 >>> math.sqrt(2)
       
   245 1.4142135623730951
       
   246 \end{lstlisting}
       
   247 \end{frame}
       
   248 
       
   249 \begin{frame}[fragile]
       
   250     {Modules}
       
   251   \begin{itemize}
       
   252     \item The \kwrd{import} keyword ``loads'' a module
       
   253     \item One can also use:
       
   254       \begin{lstlisting}
       
   255 >>> from math import sqrt
       
   256 >>> from math import *
       
   257       \end{lstlisting}    
       
   258     \item What is the difference?
       
   259     \item \alert{Use the later only in interactive mode}
       
   260     \end{itemize}
       
   261   \emphbar{Package hierarchies}
       
   262       \begin{lstlisting}
       
   263 >>> from os.path import exists
       
   264       \end{lstlisting}
       
   265 \end{frame}
       
   266 
       
   267 \begin{frame}
       
   268   \frametitle{Modules: Standard library}
       
   269   \begin{itemize}
       
   270   \item Very powerful, ``Batteries included''
       
   271   \item Some standard modules:
       
   272     \begin{itemize}
       
   273     \item Math: \typ{math}, \typ{random}
       
   274     \item Internet access: \typ{urllib2}, \typ{smtplib}
       
   275     \item System, Command line arguments: \typ{sys}
       
   276     \item Operating system interface: \typ{os}
       
   277     \item Regular expressions: \typ{re}
       
   278     \item Compression: \typ{gzip}, \typ{zipfile}, and \typ{tarfile}
       
   279     \item And a whole lot more!
       
   280     \end{itemize}
       
   281   \item Check out the Python Library reference:
       
   282     \url{http://docs.python.org/library/}
       
   283   \end{itemize}
       
   284 \end{frame}
       
   285 
       
   286 \begin{frame}[fragile]
       
   287     {Modules of special interest}
       
   288     \begin{description}[matplotlibfor2d]
       
   289 
       
   290         \item[\typ{numpy}] Efficient, powerful numeric arrays
       
   291 
       
   292         \item[\typ{matplotlib}] Easy, interactive, 2D plotting
       
   293 
       
   294         \item[\typ{scipy}] statistics, optimization, integration, linear
       
   295             algebra, Fourier transforms, signal and image processing,
       
   296             genetic algorithms, ODE solvers, special functions, and more
       
   297 
       
   298         \item[Mayavi] Easy, interactive, 3D plotting
       
   299 
       
   300     \end{description}
       
   301 \end{frame}
       
   302 
       
   303 \begin{frame}[fragile]
       
   304     {Creating your own modules}
       
   305   \begin{itemize}
       
   306   \item Define variables, functions and classes in a file with a
       
   307     \typ{.py} extension
       
   308   \item This file becomes a module!
       
   309   \item Accessible when in the current directory
       
   310   \item Use \typ{cd} in IPython to change directory
       
   311 
       
   312   \item Naming your module
       
   313       \end{itemize}
       
   314 \end{frame}
       
   315 
       
   316 \begin{frame}[fragile]
       
   317   \frametitle{Modules: example}
       
   318   \begin{lstlisting}
       
   319 # --- arith.py ---
       
   320 def gcd(a, b):
       
   321     if a%b == 0: return b
       
   322     return gcd(b, a%b)
       
   323 def lcm(a, b):
       
   324     return a*b/gcd(a, b)
       
   325 # ------------------
       
   326 >>> import arith
       
   327 >>> arith.gcd(26, 65)
       
   328 13
       
   329 >>> arith.lcm(26, 65)
       
   330 130
       
   331   \end{lstlisting}
       
   332 \end{frame}
       
   333 
       
   334 \begin{frame}[fragile]
       
   335   \frametitle{Problem 5.1}
       
   336 
       
   337   Put all the functions you have written so far as part of the problems
       
   338   into one module called \typ{iitb.py} and use this module from IPython.
       
   339 
       
   340 \inctime{20}
       
   341 \end{frame}
       
   342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   343 % TIME: 20 m, running 225m 
       
   344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   345 
       
   346 \subsection{Objects}
       
   347 \begin{frame}{Objects in Python}
       
   348     \begin{itemize}
       
   349         \item What is an Object? (Types and classes)
       
   350         \item identity
       
   351         \item type
       
   352         \item method
       
   353       \end{itemize}
       
   354 \end{frame}
       
   355 
       
   356 \begin{frame}[fragile]
       
   357   \frametitle{Why are they useful?}
       
   358   \small
       
   359   \begin{lstlisting}
       
   360 for element in (1, 2, 3):
       
   361     print element
       
   362 for key in {'one':1, 'two':2}:
       
   363     print key
       
   364 for char in "123":
       
   365     print char
       
   366 for line in open("myfile.txt"):
       
   367     print line
       
   368 for line in urllib2.urlopen('http://site.com'):
       
   369     print line
       
   370   \end{lstlisting}
       
   371 \end{frame}
       
   372 
       
   373 \begin{frame}{And the winner is \ldots OBJECTS!}
       
   374   All objects providing a similar inteface can be used the same way.\\
       
   375   Functions (and others) are first-class objects. Can be passed to and returned from functions.
       
   376   \inctime{10}
       
   377 \end{frame}
       
   378 
       
   379 \subsection{Coding Style in Python}
       
   380 \begin{frame}{Readability and Consistency}
       
   381     \begin{itemize}
       
   382         \item Readability Counts!-Code is read more often than its written.
       
   383         \item Consistency!
       
   384         \item Know when to be inconsistent.
       
   385       \end{itemize}
       
   386 \end{frame}
       
   387 
       
   388 \begin{frame}[fragile]
       
   389   \frametitle{Code Layout}
       
   390   \begin{itemize}
       
   391         \item Indentation
       
   392         \item Tabs or Spaces??
       
   393         \item Maximum Line Length
       
   394         \item Blank Lines
       
   395         \item Encodings
       
   396    \end{itemize}
       
   397 \end{frame}
       
   398 
       
   399 \begin{frame}{Whitespaces in Expressions}
       
   400   \begin{itemize}
       
   401         \item When to use extraneous whitespaces??
       
   402         \item When to avoid extra whitespaces??
       
   403         \item Use one statement per line
       
   404    \end{itemize}
       
   405 \end{frame}
       
   406 
       
   407 \begin{frame}{Comments}
       
   408   \begin{itemize}
       
   409         \item No comments better than contradicting comments
       
   410         \item Block comments
       
   411         \item Inline comments
       
   412    \end{itemize}
       
   413 \end{frame}
       
   414 
       
   415 \begin{frame}{Docstrings}
       
   416   \begin{itemize}
       
   417         \item When to write docstrings?
       
   418         \item Ending the docstrings
       
   419         \item One liner docstrings
       
   420    \end{itemize}
       
   421 \end{frame}
       
   422 \inctime{10}
       
   423 \end{document}