day2/session2.tex
changeset 288 c4e25269a86c
parent 251 457b67834245
child 297 a835affb1447
equal deleted inserted replaced
287:d4ad532525a2 288:c4e25269a86c
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     2 %Tutorial slides on Python.
     2 %Tutorial slides on Python.
     3 %
     3 %
     4 % Author: Prabhu Ramachandran <prabhu at aero.iitb.ac.in>
     4 % Author: FOSSEE 
     5 % Copyright (c) 2005-2009, Prabhu Ramachandran
     5 % Copyright (c) 2009, FOSSEE, IIT Bombay
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     7 
     7 
     8 \documentclass[14pt,compress]{beamer}
     8 \documentclass[14pt,compress]{beamer}
     9 %\documentclass[draft]{beamer}
     9 %\documentclass[draft]{beamer}
    10 %\documentclass[compress,handout]{beamer}
    10 %\documentclass[compress,handout]{beamer}
    76 \title[Basic Python]{Python language: Data structures and functions}
    76 \title[Basic Python]{Python language: Data structures and functions}
    77 
    77 
    78 \author[FOSSEE Team] {The FOSSEE Group}
    78 \author[FOSSEE Team] {The FOSSEE Group}
    79 
    79 
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \date[] {1, November 2009\\Day 2, Session 2}
    81 \date[] {8 November, 2009\\Day 2, Session 2}
    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 
   121   \frametitle{Outline}
   121   \frametitle{Outline}
   122   \tableofcontents
   122   \tableofcontents
   123   % You might wish to add the option [pausesections]
   123   % You might wish to add the option [pausesections]
   124 \end{frame}
   124 \end{frame}
   125 
   125 
       
   126 \section{Control flow}
       
   127 \subsection{Basic Looping}
       
   128 \begin{frame}[fragile]
       
   129   \frametitle{\typ{while}}
       
   130 Example: Fibonacci series
       
   131   \begin{lstlisting}
       
   132 # the sum of two elements
       
   133 # defines the next
       
   134 In []: a, b = 0, 1
       
   135 In []: while b < 10:
       
   136   ...:     print b,
       
   137   ...:     a, b = b, a + b
       
   138   ...:
       
   139   ...:
       
   140 \end{lstlisting}
       
   141 \typ{1 1 2 3 5 8}\\
       
   142 \end{frame}
       
   143 
       
   144 \begin{frame}[fragile]
       
   145 \frametitle{\typ{range()}}
       
   146 \kwrd{range([start,] stop[, step])}\\
       
   147 \begin{itemize}
       
   148   \item range() returns a list of integers
       
   149   \item The \emph{start} and the \emph{step} arguments are optional
       
   150   \item \emph{stop} argument is not included in the list
       
   151 \end{itemize}
       
   152 \vspace*{.5in}
       
   153 \begin{itemize}
       
   154   \item \alert{Anything within \typ{[]} is optional}
       
   155   \begin{itemize}
       
   156     \item Nothing to do with Python.
       
   157   \end{itemize}
       
   158 \end{itemize}
       
   159 
       
   160 \end{frame}
       
   161 
       
   162 \begin{frame}[fragile]
       
   163   \frametitle{\typ{for} \ldots \typ{range()}}
       
   164 Example: print squares of first \typ{n} numbers
       
   165   \begin{lstlisting}
       
   166 In []: for i in range(5):
       
   167  ....:     print i, i * i
       
   168  ....:
       
   169  ....:
       
   170 0 0
       
   171 1 1
       
   172 2 4
       
   173 3 9
       
   174 4 16
       
   175 \end{lstlisting}
       
   176 \end{frame}
       
   177 
       
   178 \begin{frame}[fragile]
       
   179   \frametitle{\typ{for} \ldots \typ{range()}}
       
   180 Example: print squares of odd numbers from 3 to 9
       
   181   \begin{lstlisting}
       
   182 In []: for i in range(3, 10, 2):
       
   183  ....:     print i, i * i
       
   184  ....:
       
   185  ....:
       
   186 3 9
       
   187 5 25
       
   188 7 49
       
   189 9 81
       
   190 \end{lstlisting}
       
   191 \inctime{5}
       
   192 \end{frame}
       
   193 
       
   194 \subsection{Exercises}
       
   195 
       
   196 \begin{frame}{Problem set 1: Problem 1.1}
       
   197   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$\\
       
   198 \vspace*{0.2in}
       
   199 \emphbar{These are called $Armstrong$ numbers.}
       
   200 \end{frame}
       
   201 
       
   202 \begin{frame}{Problem 1.2 - Collatz sequence}
       
   203 \begin{enumerate}
       
   204   \item Start with an arbitrary (positive) integer. 
       
   205   \item If the number is even, divide by 2; if the number is odd, multiply by 3 and add 1.
       
   206   \item Repeat the procedure with the new number.
       
   207   \item It appears that for all starting values there is a cycle of 4, 2, 1 at which the procedure loops.
       
   208 \end{enumerate}
       
   209     Write a program that accepts the starting value and prints out the Collatz sequence.
       
   210 \end{frame}
       
   211 
       
   212 \begin{frame}[fragile]{Problem 1.3}
       
   213   Write a program that prints the following pyramid on the screen. 
       
   214   \begin{lstlisting}
       
   215 1
       
   216 2  2
       
   217 3  3  3
       
   218 4  4  4  4
       
   219   \end{lstlisting}
       
   220 The number of lines must be obtained from the user.\\
       
   221 \pause
       
   222 \emphbar{When can your code fail?}
       
   223 \inctime{5}
       
   224 \end{frame}
       
   225 
   126 \section{Data structures}
   226 \section{Data structures}
   127 \subsection{Lists}
   227 \subsection{Lists}
   128 \begin{frame}[fragile]
   228 \begin{frame}[fragile]
   129   \frametitle{Lists}
   229   \frametitle{Lists}
   130 \begin{block}{We already know that}
   230 \begin{block}{We already know that}
   131   \begin{lstlisting}
   231   \begin{lstlisting}
   132 num = [1, 2, 3, 4, 5, 6, 7, 8]
   232 num = [1, 2, 3, 4]
   133   \end{lstlisting}
   233   \end{lstlisting}
   134 \centerline{is a list}
   234 \centerline{is a list}
   135 \end{block}
   235 \end{block}
   136 \end{frame}
   236 \end{frame}
   137 
   237 
   138 \begin{frame}[fragile]
   238 \begin{frame}[fragile]
   139   \frametitle{Lists: methods}
   239   \frametitle{Lists: methods}
   140   \begin{lstlisting}
   240   \begin{lstlisting}
       
   241 In []: num = [1, 2, 3, 4]
       
   242 
       
   243 In []: num.append([9, 10, 11])
       
   244 
       
   245 In []: num
       
   246 Out[]: [1, 2, 3, 4, [9, 10, 11]]
       
   247   \end{lstlisting}
       
   248 \end{frame}
       
   249 
       
   250 \begin{frame}[fragile]
       
   251   \frametitle{Lists: methods}
       
   252   \begin{lstlisting}
       
   253 In []: num = [1, 2, 3, 4]
       
   254 
       
   255 In []: num.extend([5, 6, 7])
       
   256 In []: num
       
   257 Out[]: [1, 2, 3, 4, 5, 6, 7]
       
   258 
   141 In []: num.reverse()
   259 In []: num.reverse()
   142 In []: num
   260 In []: num
   143 Out[]: [8, 7, 6, 5, 4, 3, 2, 1]
   261 Out[]: [7, 6, 5, 4, 3, 2, 1]
   144 
   262 
   145 In []: num.extend([0, -1, -2])
   263 In []: num.remove(6)
   146 In []: num
   264 In []: num
   147 Out[]: [8, 7, 6, 5, 4, 3, 2, 1, 0, -1]
   265   \end{lstlisting}
   148 
   266 \end{frame}
   149 In []: num.remove(0)
   267 
   150 In []: num
   268 \begin{frame}[fragile]
   151 Out[]: [8, 7, 6, 5, 4, 3, 2, 1, -1]
   269   \frametitle{Lists: slicing}
   152   \end{lstlisting}
   270   \begin{itemize}
       
   271     \item \typ{list[initial:final]}
       
   272   \end{itemize}
       
   273 \begin{lstlisting}
       
   274 In []: a = [1, 2, 3, 4, 5]
       
   275 
       
   276 In []: a[1:3]
       
   277 Out[]: [2, 3]
       
   278 
       
   279 In []: a[1:-1]
       
   280 Out[]: [2, 3, 4]
       
   281 
       
   282 In []: a[:3]
       
   283 Out[]: [1, 2, 3]
       
   284 \end{lstlisting}
       
   285 \end{frame}
       
   286 
       
   287 \begin{frame}[fragile]
       
   288   \frametitle{Lists: slicing}
       
   289   \begin{itemize}
       
   290     \item \typ{list[initial:final:step]}
       
   291   \end{itemize}
       
   292 \begin{lstlisting}
       
   293 In []: a[1:-1:2]
       
   294 Out[]: [2, 4]
       
   295 
       
   296 In []: a[::2]
       
   297 Out[]: [1, 3, 5]
       
   298 
       
   299 In []: a[-1::-1]
       
   300 Out[]: [5, 4, 3, 2, 1]
       
   301 \end{lstlisting}
   153 \end{frame}
   302 \end{frame}
   154 
   303 
   155 \begin{frame}[fragile]
   304 \begin{frame}[fragile]
   156 \frametitle{List containership}
   305 \frametitle{List containership}
   157 \begin{lstlisting}
   306 \begin{lstlisting}
   158 In []: a = 8
   307 In []: 4 in num
   159 
       
   160 In []: a in num
       
   161 Out[]: True
   308 Out[]: True
   162 
   309 
   163 In []: b = 10
   310 In []: b = 15
   164 In []: b in num
   311 In []: b in num
   165 Out[]: False
   312 Out[]: False
   166 
   313 
   167 In []: b not in num
   314 In []: b not in num
   168 Out[]: True
   315 Out[]: True
   172 \subsection{Tuples}
   319 \subsection{Tuples}
   173 \begin{frame}[fragile]
   320 \begin{frame}[fragile]
   174 \frametitle{Tuples: Immutable lists}
   321 \frametitle{Tuples: Immutable lists}
   175 \begin{lstlisting}
   322 \begin{lstlisting}
   176 In []: t = (1, 2, 3, 4, 5, 6, 7, 8)
   323 In []: t = (1, 2, 3, 4, 5, 6, 7, 8)
       
   324 
   177 In []: t[0] + t[3] + t[-1]
   325 In []: t[0] + t[3] + t[-1]
   178 Out[]: 13
   326 Out[]: 13
   179 \end{lstlisting}
   327 
       
   328 # Try the following!
       
   329 In []: t[4] = 7 
       
   330 \end{lstlisting}
       
   331 \pause
   180 \begin{block}{Note:}
   332 \begin{block}{Note:}
   181 \begin{itemize}
   333 \begin{itemize}
   182   \item Tuples are immutable - cannot be changed
   334   \item Tuples are immutable - cannot be changed
   183 \end{itemize}
   335 \end{itemize}
   184 \end{block}
   336 \end{block}
   198   \end{block}
   350   \end{block}
   199 \end{frame}
   351 \end{frame}
   200 
   352 
   201 \subsection{Dictionaries}
   353 \subsection{Dictionaries}
   202 \begin{frame}[fragile]
   354 \begin{frame}[fragile]
   203   \frametitle{Dictionaries: Recall}
   355   \frametitle{Dictionaries: recall}
   204   \begin{lstlisting}
   356   \begin{lstlisting}
   205 In []: player = {'Mat': 134,'Inn': 233,
   357 In []: player = {'Mat': 134,'Inn': 233,
   206           'Runs': 10823, 'Avg': 52.53}
   358           'Runs': 10823, 'Avg': 52.53}
   207 
   359 
   208 In []: player['Avg']
   360 In []: player['Avg']
   210   \end{lstlisting}
   362   \end{lstlisting}
   211   \begin{block}{Note!}
   363   \begin{block}{Note!}
   212     Duplicate keys are not allowed!\\
   364     Duplicate keys are not allowed!\\
   213     Dictionaries are iterable through keys.
   365     Dictionaries are iterable through keys.
   214   \end{block}
   366   \end{block}
       
   367 \end{frame}
       
   368 
       
   369 \begin{frame}[fragile]
       
   370   \frametitle{Dictionaries: containership}
       
   371   \begin{lstlisting}
       
   372 In []: 'Inn' in player
       
   373 Out[]: True
       
   374 
       
   375 In []: 'Econ' in player
       
   376 Out[]: False
       
   377   \end{lstlisting}
       
   378 \end{frame}
       
   379 
       
   380 \begin{frame}[fragile]
       
   381   \frametitle{Dictionaries: methods}
       
   382   \begin{lstlisting}
       
   383 In []: player.keys()
       
   384 Out[]: ['Runs', 'Inn', 'Avg', 'Mat']
       
   385 
       
   386 In []: player.values()
       
   387 Out[]: [10823, 233, 52.530000000000001, 134]
       
   388   \end{lstlisting}
   215 \end{frame}
   389 \end{frame}
   216 
   390 
   217 \begin{frame} {Problem Set 2.1: Problem 2.1.1}
   391 \begin{frame} {Problem Set 2.1: Problem 2.1.1}
   218 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.
   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.
   219 \end{frame}
   393 \end{frame}
   226       \item No ordering, no duplicates
   400       \item No ordering, no duplicates
   227       \item usual suspects: union, intersection, subset \ldots
   401       \item usual suspects: union, intersection, subset \ldots
   228       \item >, >=, <, <=, in, \ldots
   402       \item >, >=, <, <=, in, \ldots
   229     \end{itemize}
   403     \end{itemize}
   230     \begin{lstlisting}
   404     \begin{lstlisting}
   231 >>> f10 = set([1,2,3,5,8])
   405 
   232 >>> p10 = set([2,3,5,7])
   406 In []: f10 = set([1,2,3,5,8])
   233 >>> f10|p10
   407 
   234 set([1, 2, 3, 5, 7, 8])
   408 In []: p10 = set([2,3,5,7])
   235 >>> f10&p10
   409 
   236 set([2, 3, 5])
   410 In []: f10 | p10
   237 >>> f10-p10
   411 Out[]: set([1, 2, 3, 5, 7, 8])
   238 set([8, 1])
   412 \end{lstlisting}
   239 \end{lstlisting}
   413 \end{frame}
   240 \end{frame}
   414 
   241 
   415 \begin{frame}[fragile]
   242 \begin{frame}[fragile]
   416   \frametitle{Set \ldots}
   243   \frametitle{Set}
       
   244     \begin{lstlisting}
   417     \begin{lstlisting}
   245 >>> p10-f10, f10^p10
   418 In []: f10 & p10
   246 set([7]), set([1, 7, 8])
   419 Out[]: set([2, 3, 5])
   247 >>> set([2,3]) < p10
   420 
   248 True
   421 In []: f10 - p10
   249 >>> set([2,3]) <= p10
   422 Out[]: set([1, 8])
   250 True
   423 
   251 >>> 2 in p10
   424 In []: p10 - f10, f10 ^ p10
   252 True
   425 Out[]: (set([7]), set([1, 7, 8]))
   253 >>> 4 in p10
   426 
   254 False
   427 In []: set([2,3]) < p10
   255 >>> len(f10)
   428 Out[]: True
   256 5
   429 \end{lstlisting}
       
   430 \end{frame}
       
   431 
       
   432 \begin{frame}[fragile]
       
   433   \frametitle{Set \ldots}
       
   434     \begin{lstlisting}
       
   435 In []: set([2,3]) <= p10
       
   436 Out[]: True
       
   437 
       
   438 In []: 2 in p10
       
   439 Out[]: True
       
   440 
       
   441 In []: 4 in p10
       
   442 Out[]: False
       
   443 
       
   444 In []: len(f10)
       
   445 Out[]: 5
   257 \end{lstlisting}
   446 \end{lstlisting}
   258 \end{frame}
   447 \end{frame}
   259 
   448 
   260 \begin{frame}
   449 \begin{frame}
   261   \frametitle{Problem set 2.2}
   450   \frametitle{Problem set 2.2}
   262   \begin{description}
   451   \begin{description}
   263     \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?
   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?
   264     \item[2.2.2] Given a string of the form ``4-7, 9, 12, 15'' find the missing numbers in the given range.
   453 \end{description}
       
   454 \inctime{15}
       
   455 \end{frame}
       
   456 
       
   457 \begin{frame}
       
   458   \frametitle{Problem set 2.2}
       
   459   \begin{description}
       
   460     \item[2.2.2] Given a list of words, find all the anagrams in the list
   265 \end{description}
   461 \end{description}
   266 \inctime{15}
   462 \inctime{15}
   267 \end{frame}
   463 \end{frame}
   268 
   464 
   269 \section{Functions}
   465 \section{Functions}
   292         return 0
   488         return 0
   293   \end{lstlisting}
   489   \end{lstlisting}
   294 \end{frame}
   490 \end{frame}
   295 
   491 
   296 \begin{frame}[fragile]
   492 \begin{frame}[fragile]
       
   493   \frametitle {What does this function do?}
       
   494   \begin{lstlisting}
       
   495 def what( n ):
       
   496     if n < 0: n = -n
       
   497     while n > 0:
       
   498         if n % 2 == 1:
       
   499             return False
       
   500         n /= 10
       
   501     return True
       
   502   \end{lstlisting}
       
   503 \end{frame} 
       
   504 
       
   505 \begin{frame}[fragile]
   297   {What does this function do?}
   506   {What does this function do?}
   298 \begin{lstlisting}
   507 \begin{lstlisting}
   299 def what( n ):
   508 def what( n ):
   300     i = 1
   509     i = 1
   301     while i * i < n:
   510     while i * i < n:
   302         i += 1
   511         i += 1
   303     return i * i == n, i
   512     return i * i == n, i
   304   \end{lstlisting}
   513   \end{lstlisting}
   305 \end{frame}
   514 \end{frame}
   306 
   515 
   307 \subsection{Default arguments}
       
   308 \begin{frame}[fragile]
       
   309   \frametitle{Functions: default arguments}
       
   310   \small
       
   311   \begin{lstlisting}
       
   312 def ask_ok(prompt, complaint='Yes or no!'):
       
   313     while True:
       
   314         ok = raw_input(prompt)
       
   315         if ok in ('y', 'ye', 'yes'): 
       
   316             return True
       
   317         if ok in ('n', 'no', 'nop',
       
   318                   'nope'): 
       
   319             return False
       
   320         print complaint
       
   321 
       
   322 ask_ok('?')
       
   323 ask_ok('?', '[Y/N]')
       
   324   \end{lstlisting}
       
   325 \end{frame}
       
   326 
       
   327 \subsection{Built-in functions}
       
   328 \begin{frame}
       
   329   {Before writing a function}
       
   330   \begin{itemize}
       
   331       \item Variety of builtin functions are available
       
   332       \item \typ{abs, any, all, len, max, min}
       
   333       \item \typ{pow, range, sum, type}
       
   334       \item Refer here:
       
   335           \url{http://docs.python.org/library/functions.html}
       
   336   \end{itemize}
       
   337   \inctime{10} 
       
   338 \end{frame}
       
   339 
       
   340 \subsection{Exercises}
       
   341 \begin{frame}{Problem set 3: Problem 3.1}
       
   342   Write a function to return the gcd of two numbers.
       
   343 \end{frame}
       
   344 
       
   345 \begin{frame}{Problem 3.2}
       
   346 Write a program to print all primitive pythagorean triads (a, b, c) where a, b are in the range 1---100 \\
       
   347 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 is also not primitive.
       
   348 \end{frame}
       
   349 
       
   350 \begin{frame}{Problem 3.3}
       
   351   Write a program that generates a list of all four digit numbers that have all their digits even and are perfect squares.\newline\\\emph{For example, the output should include 6400 but not 8100 (one digit is odd) or 4248 (not a perfect square).}
       
   352 \inctime{15}
       
   353 \end{frame}
       
   354 
       
   355 \section{Modules}
       
   356 \begin{frame}[fragile]
       
   357     {Modules}
       
   358 \begin{lstlisting}
       
   359 >>> sqrt(2)
       
   360 Traceback (most recent call last):
       
   361   File "<stdin>", line 1, in <module>
       
   362 NameError: name 'sqrt' is not defined
       
   363 >>> import math        
       
   364 >>> math.sqrt(2)
       
   365 1.4142135623730951
       
   366 \end{lstlisting}
       
   367 \end{frame}
       
   368 
       
   369 \begin{frame}[fragile]
       
   370     {Modules}
       
   371   \begin{itemize}
       
   372     \item The \kwrd{import} keyword ``loads'' a module
       
   373     \item One can also use:
       
   374       \begin{lstlisting}
       
   375 >>> from math import sqrt
       
   376 >>> from math import *
       
   377       \end{lstlisting}    
       
   378     \item What is the difference?
       
   379     \item \alert{Use the latter only in interactive mode}
       
   380     \end{itemize}
       
   381   \emphbar{Package hierarchies}
       
   382       \begin{lstlisting}
       
   383 >>> from os.path import exists
       
   384       \end{lstlisting}
       
   385 \end{frame}
       
   386 
       
   387 \begin{frame}
       
   388   \frametitle{Modules: Standard library}
       
   389   \begin{itemize}
       
   390   \item Very powerful, ``Batteries included''
       
   391   \item Some standard modules:
       
   392     \begin{itemize}
       
   393     \item Math: \typ{math}, \typ{random}
       
   394     \item Internet access: \typ{urllib2}, \typ{smtplib}
       
   395     \item System, Command line arguments: \typ{sys}
       
   396     \item Operating system interface: \typ{os}
       
   397     \item Regular expressions: \typ{re}
       
   398     \item Compression: \typ{gzip}, \typ{zipfile}, and \typ{tarfile}
       
   399     \item And a whole lot more!
       
   400     \end{itemize}
       
   401   \item Check out the Python Library reference:
       
   402     \url{http://docs.python.org/library/}
       
   403   \end{itemize}
       
   404 \inctime{5}
       
   405 \end{frame}
       
   406 
       
   407 \section{Coding Style}
       
   408 \begin{frame}{Readability and Consistency}
       
   409     \begin{itemize}
       
   410         \item Readability Counts!\\Code is read more often than its written.
       
   411         \item Consistency!
       
   412         \item Know when to be inconsistent.
       
   413       \end{itemize}
       
   414 \end{frame}
       
   415 
       
   416 \begin{frame}[fragile] \frametitle{A question of good style}
       
   417   \begin{lstlisting}
       
   418     amount = 12.68
       
   419     denom = 0.05
       
   420     nCoins = round(amount/denom)
       
   421     rAmount = nCoins * denom
       
   422   \end{lstlisting}
       
   423   \pause
       
   424   \begin{block}{Style Rule \#1}
       
   425     Naming is 80\% of programming
       
   426   \end{block}
       
   427 \end{frame}
       
   428 
       
   429 \begin{frame}[fragile]
       
   430   \frametitle{Code Layout}
       
   431   \begin{itemize}
       
   432         \item Indentation
       
   433         \item Tabs or Spaces??
       
   434         \item Maximum Line Length
       
   435         \item Blank Lines
       
   436         \item Encodings
       
   437    \end{itemize}
       
   438 \end{frame}
       
   439 
       
   440 \begin{frame}{Whitespaces in Expressions}
       
   441   \begin{itemize}
       
   442         \item When to use extraneous whitespaces??
       
   443         \item When to avoid extra whitespaces??
       
   444         \item Use one statement per line
       
   445    \end{itemize}
       
   446 \end{frame}
       
   447 
       
   448 \begin{frame}{Comments}
       
   449   \begin{itemize}
       
   450         \item No comments better than contradicting comments
       
   451         \item Block comments
       
   452         \item Inline comments
       
   453    \end{itemize}
       
   454 \end{frame}
       
   455 
       
   456 \begin{frame}{Docstrings}
       
   457   \begin{itemize}
       
   458         \item When to write docstrings?
       
   459         \item Ending the docstrings
       
   460         \item One liner docstrings
       
   461    \end{itemize}
       
   462 More information at PEP8: http://www.python.org/dev/peps/pep-0008/
       
   463 \inctime{5}
       
   464 \end{frame}
       
   465 
       
   466 \section{Objects}
       
   467 \begin{frame}{Objects in general}
       
   468     \begin{itemize}
       
   469         \item What is an Object? (Types and classes)
       
   470         \item identity
       
   471         \item type
       
   472         \item method
       
   473       \end{itemize}
       
   474 \end{frame}
       
   475 
       
   476 \begin{frame}{Almost everything is an Object!}
       
   477   \begin{itemize}
       
   478     \item \typ{list}
       
   479     \item \typ{tuple}
       
   480     \item \typ{string}
       
   481     \item \typ{dictionary}
       
   482     \item \typ{function}
       
   483     \item Of course, user defined class objects!
       
   484   \end{itemize}
       
   485 \end {frame}
       
   486 
       
   487 \begin{frame}{Using Objects}
       
   488   \begin{itemize}
       
   489     \item Creating Objects: Initialization
       
   490     \item Object Manipulation: Object methods and ``.'' operator
       
   491   \end{itemize}
       
   492 \end{frame}
       
   493 
       
   494 \begin{frame}[fragile]
       
   495   \frametitle{Objects provide consistency}
       
   496   \small
       
   497   \begin{lstlisting}
       
   498 for element in (1, 2, 3):
       
   499     print element
       
   500 for key in {'one':1, 'two':2}:
       
   501     print key
       
   502 for char in "123":
       
   503     print char
       
   504 for line in open("myfile.txt"):
       
   505     print line
       
   506 for line in urllib2.urlopen('http://site.com'):
       
   507     print line
       
   508   \end{lstlisting}
       
   509 \inctime{10}
       
   510 \end{frame}
       
   511 
       
   512 \begin{frame}
   516 \begin{frame}
   513   \frametitle{What did we learn?}
   517   \frametitle{What did we learn?}
   514   \begin{itemize}
   518   \begin{itemize}
   515     \item Lists, Tuples, Dictionaries, Sets: creation and manipulation
   519     \item Loops: \kwrd{while}, \kwrd{for}
   516     \item More about functions
   520     \item Advanced Data structures
   517     \item Coding style
   521     \item Functions
   518     \item Objects: creation and manipulation
       
   519   \end{itemize}
   522   \end{itemize}
   520 \end{frame}
   523 \end{frame}
   521 
   524 
   522 \end{document}
   525 \end{document}