day1/Session-2.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-2009, 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\bfseries,
       
    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:\\Functions and basic data structures}
       
    77 
       
    78 \author[FOSSEE Team] {Asokan Pichai\\Prabhu Ramachandran}
       
    79 
       
    80 \institute[FOSSEE] {FOSSEE Team}
       
    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 \subsection{Exercises on Control flow}
       
   116 \begin{frame}
       
   117   \frametitle{Problem set 1}
       
   118   \begin{itemize}
       
   119     \item All the problems can be\\
       
   120       solved using \kwrd{if} and \kwrd{while} 
       
   121   \end{itemize}
       
   122 \end{frame}
       
   123 
       
   124 \begin{frame}{Problem 1.1}
       
   125   Write a program that displays all three digit numbers that are equal to the sum of the cubes of their digits. That is, print numbers $abc$ that have the property $abc = a^3 + b^3 + c^3$\\
       
   126 These are called $Armstrong$ numbers.
       
   127 \end{frame}
       
   128   
       
   129 \begin{frame}{Problem 1.2 - Collatz sequence}
       
   130 \begin{enumerate}
       
   131   \item Start with an arbitrary (positive) integer. 
       
   132   \item If the number is even, divide by 2; if the number is odd, multiply by 3 and add 1.
       
   133   \item Repeat the procedure with the new number.
       
   134   \item It appears that for all starting values there is a cycle of 4, 2, 1 at which the procedure loops.
       
   135 \end{enumerate}
       
   136     Write a program that accepts the starting value and prints out the Collatz sequence.
       
   137 
       
   138 \end{frame}
       
   139 
       
   140 \begin{frame}{Problem 1.3 - Kaprekar's constant}
       
   141   \begin{enumerate}
       
   142     \item Take a four digit number--with at least two digits different.
       
   143     \item Arrange the digits in ascending and descending order, giving A and D respectively.
       
   144     \item Leave leading zeros in A!
       
   145     \item Subtract A from D.
       
   146     \item With the result, repeat from step 2.
       
   147   \end{enumerate}
       
   148   Write a program to accept a 4-digit number and display the progression to Kaprekar's constant.
       
   149 \end{frame}
       
   150 
       
   151 \begin{frame}[fragile]{Problem 1.4}
       
   152   Write a program that prints the following pyramid on the screen. 
       
   153   \begin{lstlisting}
       
   154 1
       
   155 2  2
       
   156 3  3  3
       
   157 4  4  4  4
       
   158   \end{lstlisting}
       
   159 The number of lines must be obtained from the user as input.\\
       
   160 \pause
       
   161 When can your code fail?
       
   162 \only<2->{\inctime{20}}
       
   163 \end{frame}
       
   164 
       
   165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   166 % TIME: 20 m, running 20m 
       
   167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   168 
       
   169 \subsection{Functions}
       
   170 \begin{frame}[fragile]
       
   171 \frametitle{Functions: examples}
       
   172   \begin{lstlisting}
       
   173 def signum( r ):
       
   174     """returns 0 if r is zero
       
   175     -1 if r is negative
       
   176     +1 if r is positive"""
       
   177     if r < 0:
       
   178         return -1
       
   179     elif r > 0:
       
   180         return 1
       
   181     else:
       
   182         return 0
       
   183   \end{lstlisting}
       
   184 \end{frame}
       
   185 
       
   186 \begin{frame}[fragile]
       
   187   \frametitle{Functions: examples}
       
   188   \begin{lstlisting}
       
   189 def pad( n, size ): 
       
   190     """pads integer n with spaces
       
   191     into a string of length size
       
   192     """
       
   193     SPACE = ' '
       
   194     s = str( n )
       
   195     padSize = size - len( s )
       
   196     return padSize * SPACE + s
       
   197   \end{lstlisting}
       
   198 \pause
       
   199 What about \%3d?
       
   200 \end{frame}
       
   201 
       
   202 \begin{frame}[fragile]
       
   203   {What does this function do?}
       
   204   \begin{lstlisting}
       
   205 def what( n ):
       
   206     if n < 0: n = -n
       
   207     while n > 0:
       
   208         if n % 2 == 1:
       
   209             return False
       
   210         n /= 10
       
   211     return True
       
   212   \end{lstlisting}
       
   213 \end{frame}
       
   214 
       
   215 \begin{frame}[fragile]
       
   216   {What does this function do?}
       
   217 \begin{lstlisting}
       
   218 def what( n ):
       
   219     i = 1    
       
   220     while i * i < n:
       
   221         i += 1
       
   222     return i * i == n, i
       
   223   \end{lstlisting}
       
   224 \end{frame}
       
   225 
       
   226 \begin{frame}[fragile]
       
   227   {What does this function do?}
       
   228   \begin{lstlisting}
       
   229 def what( n, x ):
       
   230     z = 1.0
       
   231     if n < 0:
       
   232         x = 1.0 / x
       
   233         n = -n
       
   234     while n > 0:
       
   235         if n % 2 == 1:
       
   236             z *= x
       
   237         n /= 2
       
   238         x *= x
       
   239     return z
       
   240   \end{lstlisting}
       
   241 \end{frame}
       
   242 
       
   243 \begin{frame}
       
   244   {Before writing a function}
       
   245   \begin{itemize}
       
   246       \item Builtin functions for various and sundry
       
   247       \item \typ{abs, any, all, len, max, min}
       
   248       \item \typ{pow, range, sum, type}
       
   249       \item Refer here:
       
   250           \url{http://docs.python.org/library/functions.html}
       
   251   \end{itemize}
       
   252   \inctime{10} 
       
   253 \end{frame}
       
   254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   255 % TIME: 10 m, running 30m 
       
   256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   257 
       
   258 \begin{frame}{Problem set 2}
       
   259   The focus is on writing functions and calling them.
       
   260 \end{frame}
       
   261 
       
   262 \begin{frame}{Problem 2.1}
       
   263   Write a function to return the gcd of two numbers.
       
   264 \end{frame}
       
   265 
       
   266 \begin{frame}{Problem 2.2}
       
   267 A pythagorean triad $(a,b,c)$ has the property $a^2 + b^2 = c^2$.\\By primitive we mean triads that do not `depend' on others. For example, (4,3,5) is a variant of (3,4,5) and hence is not primitive. And (10,24,26) is easily derived from (5,12,13) and should not be displayed by our program. \\
       
   268 Write a program to print primitive pythagorean triads. The program should generate all triads with a, b values in the range 0---100
       
   269 \end{frame}
       
   270 
       
   271 \begin{frame}{Problem 2.3}
       
   272   Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\\For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).
       
   273 \end{frame}
       
   274 
       
   275 \begin{frame}{Problem 2.4}
       
   276   The aliquot of a number is defined as: the sum of the \emph{proper} divisors of the number. For example, the aliquot(12) = 1 + 2 + 3 + 4 + 6 = 16.\\
       
   277   Write a function that returns the aliquot number of a given number. 
       
   278 \end{frame}
       
   279 
       
   280 \begin{frame}{Problem 2.5}
       
   281   A pair of numbers (a, b) is said to be \alert{amicable} if the aliquot number of a is b and the aliquot number of b is a.\\
       
   282   Example: \texttt{220, 284}\\
       
   283   Write a program that prints all five digit amicable pairs.
       
   284   \inctime{25}
       
   285 \end{frame}
       
   286 
       
   287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   288 % TIME: 25 m, running 55m 
       
   289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   290 
       
   291 \subsection{Lists}
       
   292 
       
   293 \begin{frame}[fragile]
       
   294   \frametitle{List creation and indexing}
       
   295 \begin{lstlisting}
       
   296 >>> a = [] # An empty list.
       
   297 >>> a = [1, 2, 3, 4] # More useful.
       
   298 >>> len(a) 
       
   299 4
       
   300 >>> a[0] + a[1] + a[2] + a[-1]
       
   301 10
       
   302 \end{lstlisting}
       
   303   \begin{itemize}
       
   304   \item Indices start with ?
       
   305   \item Negative indices indicate ?
       
   306   \end{itemize}
       
   307 \end{frame}
       
   308 
       
   309 \begin{frame}[fragile]
       
   310   \frametitle{List: slices}
       
   311   \begin{itemize}
       
   312   \item Slicing is a basic operation
       
   313   \item \typ{list[initial:final:step]}
       
   314   \item  The step is optional
       
   315   \end{itemize}
       
   316 \begin{lstlisting}
       
   317 >>> a[1:3] # A slice.
       
   318 [2, 3]
       
   319 >>> a[1:-1]
       
   320 [2, 3]
       
   321 >>> a[1:] == a[1:-1]
       
   322 False  
       
   323 \end{lstlisting}
       
   324 Explain last result
       
   325 \end{frame}
       
   326 
       
   327 \begin{frame}[fragile]
       
   328   \frametitle{List: more slices}
       
   329 \begin{lstlisting}
       
   330 >>> a[0:-1:2] # Notice the step!
       
   331 [1, 3]
       
   332 >>> a[::2]
       
   333 [1, 3]
       
   334 >>> a[-1::-1]
       
   335 \end{lstlisting}
       
   336 What do you think the last one will do?
       
   337   \emphbar{Note: Strings also use same indexing and slicing.}
       
   338 \end{frame}
       
   339 
       
   340 \begin{frame}[fragile]
       
   341   \frametitle{List: examples}
       
   342 \begin{lstlisting}
       
   343 >>> a = [1, 2, 3, 4]
       
   344 >>> a[:2]
       
   345 [1, 2]
       
   346 >>> a[0:-1:2]
       
   347 [1, 3]
       
   348 \end{lstlisting}
       
   349 \pause
       
   350 \alert{Lists are mutable (unlike strings)}
       
   351 \begin{lstlisting}
       
   352 >>> a[1] = 20
       
   353 >>> a
       
   354 [1, 20, 3, 4]
       
   355 \end{lstlisting}
       
   356 \end{frame}
       
   357 
       
   358 \begin{frame}[fragile]
       
   359   \frametitle{Lists are mutable and heterogenous}
       
   360 \begin{lstlisting}
       
   361 >>> a = ['spam', 'eggs', 100, 1234]
       
   362 >>> a[2] = a[2] + 23
       
   363 >>> a
       
   364 ['spam', 'eggs', 123, 1234]
       
   365 >>> a[0:2] = [1, 12] # Replace items
       
   366 >>> a
       
   367 [1, 12, 123, 1234]
       
   368 >>> a[0:2] = [] # Remove items
       
   369 >>> a.append( 12345 )
       
   370 >>> a
       
   371 [123, 1234, 12345]
       
   372 \end{lstlisting}
       
   373 \inctime{10}
       
   374 \end{frame}
       
   375 
       
   376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   377 % TIME: 10 m, running 65m 
       
   378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   379 
       
   380 \begin{frame}[fragile]
       
   381   \frametitle{List methods}
       
   382 \begin{lstlisting}
       
   383 >>> a = ['spam', 'eggs', 1, 12]
       
   384 >>> a.reverse() # in situ
       
   385 >>> a
       
   386 [12, 1, 'eggs', 'spam']
       
   387 >>> a.append(['x', 1])
       
   388 >>> a
       
   389 [12, 1, 'eggs', 'spam', ['x', 1]]
       
   390 >>> a.extend([1,2]) # Extend the list.
       
   391 >>> a.remove( 'spam' )
       
   392 >>> a
       
   393 [12, 1, 'eggs', ['x', 1], 1, 2]
       
   394 \end{lstlisting}
       
   395 \end{frame}
       
   396 
       
   397 \begin{frame}[fragile]
       
   398   \frametitle{List containership}
       
   399   \begin{lstlisting}
       
   400 >>> a = ['cat', 'dog', 'rat', 'croc']
       
   401 >>> 'dog' in a
       
   402 True
       
   403 >>> 'snake' in a
       
   404 False
       
   405 >>> 'snake' not in a
       
   406 True
       
   407 >>> 'ell' in 'hello world'
       
   408 True
       
   409   \end{lstlisting}
       
   410   \inctime{5}
       
   411 \end{frame}
       
   412 
       
   413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   414 % TIME: 5 m, running 70m 
       
   415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   416 
       
   417 \begin{frame}[fragile]
       
   418   \frametitle{Tuples: immutable}
       
   419 \begin{lstlisting}
       
   420 >>> t = (0, 1, 2)
       
   421 >>> print t[0], t[1], t[2], t[-1] 
       
   422 0 1 2 2
       
   423 >>> t[0] = 1
       
   424 Traceback (most recent call last):
       
   425   File "<stdin>", line 1, in ?
       
   426 TypeError: object does not support item assignment
       
   427 \end{lstlisting}  
       
   428 \begin{itemize}
       
   429     \item Multiple return values are actually a tuple.
       
   430     \item Exchange is tuple (un)packing
       
   431 \end{itemize}
       
   432 \inctime{5}
       
   433 \end{frame}
       
   434 
       
   435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   436 % TIME: 5 m, running 75m 
       
   437 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   438 
       
   439 \begin{frame}[fragile]
       
   440   \frametitle{\typ{range()} function}
       
   441   \begin{lstlisting}
       
   442 >>> range(7)
       
   443 [0, 1, 2, 3, 4, 5, 6]
       
   444 >>> range( 3, 9)
       
   445 [3, 4, 5, 6, 7, 8]
       
   446 >>> range( 4, 17, 3)
       
   447 [4, 7, 10, 13, 16]
       
   448 >>> range( 5, 1, -1)
       
   449 [5, 4, 3, 2]
       
   450 >>> range( 8, 12, -1)
       
   451 []
       
   452   \end{lstlisting}
       
   453 \end{frame}
       
   454 
       
   455 \begin{frame}[fragile]
       
   456   \frametitle{\typ{for\ldots range(\ldots)} idiom}
       
   457   \begin{lstlisting}
       
   458 In [83]: for i in range(5):
       
   459    ....:     print i, i * i
       
   460    ....:     
       
   461    ....:     
       
   462 0 0
       
   463 1 1
       
   464 2 4
       
   465 3 9
       
   466 4 16
       
   467 \end{lstlisting}
       
   468 \end{frame}
       
   469 
       
   470 \begin{frame}[fragile]
       
   471   \frametitle{\typ{for}: the list companion}
       
   472   
       
   473   \begin{lstlisting}
       
   474 In [84]: a = ['a', 'b', 'c']
       
   475 In [85]: for x in a:
       
   476    ....:    print x, chr( ord(x) + 10 )
       
   477    ....:
       
   478 a  k
       
   479 b  l
       
   480 c  m
       
   481   \end{lstlisting}
       
   482   Iterating over the list and not the index + reference\\
       
   483   what if you want the index?
       
   484 \end{frame}
       
   485 
       
   486 \begin{frame}[fragile]
       
   487   \frametitle{\typ{for}: the list companion}
       
   488   \begin{lstlisting}
       
   489 In [89]: for p, ch in enumerate( a ):
       
   490    ....:     print p, ch
       
   491    ....:     
       
   492    ....:     
       
   493 0 a
       
   494 1 b
       
   495 2 c
       
   496   \end{lstlisting}
       
   497 Try: \typ{print enumerate(a)}
       
   498 \inctime{10}
       
   499 \end{frame}
       
   500 
       
   501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   502 % TIME: 10 m, running 85m 
       
   503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
   504 
       
   505 \begin{frame}
       
   506   \frametitle{Did we meet the goal?}
       
   507   \tableofcontents
       
   508   % You might wish to add the option [pausesections]
       
   509 \end{frame}
       
   510 
       
   511 \end{document}