day2/session1.tex
changeset 288 c4e25269a86c
parent 250 760d5679834e
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: Basics}
    76 \title[Basic Python]{Python language: Basics}
    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 1}
    81 \date[] {8 November, 2009\\Day 2, Session 1}
    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 
   138 \begin{frame}[fragile]
   138 \begin{frame}[fragile]
   139   \frametitle{Numbers}
   139   \frametitle{Numbers}
   140   \begin{itemize}
   140   \begin{itemize}
   141     \item \kwrd{int}\\ \kwrd{int} = whole number, no matter what the size!
   141     \item \kwrd{int}\\ \kwrd{int} = whole number, no matter what the size!
   142   \begin{lstlisting}
   142   \begin{lstlisting}
   143 In [1]: a = 13
   143 In []: a = 13
   144 
   144 
   145 In [2]: b = 99999999999999999999
   145 In []: b = 99999999999999999999
   146   \end{lstlisting}
   146   \end{lstlisting}
   147     \item \kwrd{float}
   147     \item \kwrd{float}
   148   \begin{lstlisting}
   148   \begin{lstlisting}
   149 In [3]: fl = 3.141592
   149 In []: p = 3.141592
   150   \end{lstlisting}
   150   \end{lstlisting}
   151   \end{itemize}
   151   \end{itemize}
   152 \end{frame}
   152 \end{frame}
   153 
   153 
   154 \begin{frame}[fragile]
   154 \begin{frame}[fragile]
   155 \frametitle{Complex numbers}
   155 \frametitle{Complex numbers}
   156   \begin{lstlisting}
   156   \begin{lstlisting}
   157 In [1]: cplx = 3+4j
   157 In []: c = 3+4j
   158 
   158 
   159 In [2]: abs(cplx)
   159 In []: abs(c)
   160 Out[2]: 5.0
   160 Out[]: 5.0
   161 
   161 
   162 In [3]: cplx.imag
   162 In []: c.imag
   163 Out[3]: 4.0
   163 Out[]: 4.0
   164 
   164 
   165 In [4]: cplx.real
   165 In []: c.real
   166 Out[4]: 3.0
   166 Out[]: 3.0
   167   \end{lstlisting}
   167   \end{lstlisting}
   168 \end{frame}
   168 \end{frame}
   169 
   169 
   170 \subsection{Boolean}
   170 \subsection{Boolean}
   171 \begin{frame}[fragile]
   171 \begin{frame}[fragile]
   172   \frametitle{Boolean}
   172   \frametitle{Boolean}
   173   \begin{lstlisting}
   173   \begin{lstlisting}
   174 In [1]: t = True
   174 In []: t = True
   175 
   175 
   176 In [2]: f = not t
   176 In []: f = not t
   177 Out[2]: False
   177 
   178 
   178 In []: f or t
   179 In [3]: f or t
   179 Out[]: True
   180 Out[3]: True
   180 
   181 
   181 In []: f and t
   182 In [4]: f and t
   182 Out[]: False
   183 Out[4]: False
   183   \end{lstlisting}
       
   184   \inctime{5}
       
   185 \end{frame}
       
   186 
       
   187 \begin{frame}[fragile]
       
   188   \frametitle{( )  for precedence}
       
   189   \begin{lstlisting}
       
   190 In []: a = False
       
   191 In []: b = True
       
   192 In []: c = True
       
   193 
       
   194 In []: (a and b) or c
       
   195 Out[]: True
       
   196 
       
   197 In []: a and (b or c)
       
   198 Out[]: False
   184   \end{lstlisting}
   199   \end{lstlisting}
   185   \inctime{5}
   200   \inctime{5}
   186 \end{frame}
   201 \end{frame}
   187 
   202 
   188 \subsection{Strings}
   203 \subsection{Strings}
   189 
   204 
   190 \begin{frame}[fragile]
   205 \begin{frame}[fragile]
   191   \frametitle{Strings}
   206   \frametitle{Strings}
   192 Strings were introduced previously, let us now look at them in a little more detail.
   207 Strings were introduced previously, let us now look at them in a little more detail.
   193   \begin{lstlisting}
   208   \begin{lstlisting}
   194 In [1]: w = "hello"
   209 In []: w = "hello"
   195 
   210 
   196 In [2]: print w[0] + w[2] + w[-1]
   211 In []: print w[0] + w[2] + w[-1]
   197 Out[2]: hlo
   212 Out[]: hlo
   198 
   213 
   199 In [3]: len(w) # guess what
   214 In []: len(w) # guess what
   200 Out[3]: 5
   215 Out[]: 5
   201   \end{lstlisting}
   216   \end{lstlisting}
   202 \end{frame}
   217 \end{frame}
   203 
   218 
   204 \begin{frame}[fragile]
   219 \begin{frame}[fragile]
   205   \frametitle{Strings \ldots}
   220   \frametitle{Strings \ldots}
   206   \begin{lstlisting}
   221   \begin{lstlisting}
   207 In [1]: w[0] = 'H' # Can't do that!
   222 In []: w[0] = 'H' # Can't do that!
   208 --------------------------------------------
   223 --------------------------------------------
   209 TypeError  Traceback (most recent call last)
   224 TypeError  Traceback (most recent call last)
   210 
   225 
   211 /<ipython console> in <module>()
   226 /<ipython console> in <module>()
   212 
   227 
   216 \end{frame}
   231 \end{frame}
   217 
   232 
   218 \begin{frame}[fragile]
   233 \begin{frame}[fragile]
   219   \frametitle{String methods}
   234   \frametitle{String methods}
   220   \begin{lstlisting}
   235   \begin{lstlisting}
   221 In [1]: a = 'Hello World'
   236 In []: a = 'Hello World'
   222 In [2]: a.startswith('Hell')
   237 In []: a.startswith('Hell')
   223 Out[2]: True
   238 Out[]: True
   224 
   239 
   225 In [3]: a.endswith('ld')
   240 In []: a.endswith('ld')
   226 Out[3]: True
   241 Out[]: True
   227 
   242 
   228 In [4]: a.upper()
   243 In []: a.upper()
   229 Out[4]: 'HELLO WORLD'
   244 Out[]: 'HELLO WORLD'
   230 
   245 
   231 In [5]: a.lower()
   246 In []: a.lower()
   232 Out[5]: 'hello world'
   247 Out[]: 'hello world'
   233   \end{lstlisting}
   248   \end{lstlisting}
       
   249 \end{frame}
       
   250 
       
   251 \begin{frame}
       
   252   \frametitle{A bit about IPython}
       
   253   \begin{itemize}
       
   254     \item IPython provides better help
       
   255     \item object.function?
       
   256     \begin{lstlisting}
       
   257 In []: a = 'Hello World'
       
   258 In []: a.lower?
       
   259     \end{lstlisting}
       
   260     \item It provides tab completion
       
   261         \begin{lstlisting}
       
   262 In []: a.s<Tab>
       
   263     \end{lstlisting}
       
   264   \end{itemize}
   234 \end{frame}
   265 \end{frame}
   235 
   266 
   236 \begin{frame}[fragile]
   267 \begin{frame}[fragile]
   237 \frametitle{Still with strings}
   268 \frametitle{Still with strings}
   238   \begin{itemize}
   269   \begin{itemize}
   239     \item We saw split() yesterday
   270     \item We saw split() yesterday
   240     \item join() is the opposite of split()
   271     \item join() is the opposite of split()
   241   \end{itemize}
   272   \end{itemize}
   242   \begin{lstlisting}
   273   \begin{lstlisting}
   243 In [1]: ''.join(['a', 'b', 'c'])
   274 In []: ''.join(['a', 'b', 'c'])
   244 Out[1]: 'abc'
   275 Out[]: 'abc'
   245   \end{lstlisting}
   276   \end{lstlisting}
   246 \end{frame}
   277 \end{frame}
   247 
   278 
   248 \begin{frame}[fragile]
   279 \begin{frame}[fragile]
   249 \frametitle{String formatting}
   280 \frametitle{String formatting}
   250   \begin{lstlisting}
   281   \begin{lstlisting}
   251 In [1]: x, y = 1, 1.234
   282 In []: x, y = 1, 1.234
   252 
   283 
   253 In [2]: 'x is %s, y is %s' %(x, y)
   284 In []: 'x is %s, y is %s' %(x, y)
   254 Out[2]: 'x is 1, y is 1.234'
   285 Out[]: 'x is 1, y is 1.234'
   255   \end{lstlisting}
   286   \end{lstlisting}
       
   287   \begin{itemize}
       
   288     \item \emph{\%d}, \emph{\%f} etc. available
       
   289   \end{itemize}
   256   \emphbar{\url{http://docs.python.org/library/stdtypes.html}}
   290   \emphbar{\url{http://docs.python.org/library/stdtypes.html}}
   257   \inctime{10}
   291   \inctime{10}
   258 \end{frame}
   292 \end{frame}
   259 
   293 
   260 \section{Operators}
   294 \section{Operators}
   261 \begin{frame}[fragile]
   295 \begin{frame}[fragile]
   262   \frametitle{Arithematic operators}
   296   \frametitle{Arithmetic operators}
   263   \begin{lstlisting}
   297   \begin{lstlisting}
   264 In [1]: 1786 % 12
   298 In []: 1786 % 12
   265 Out[1]: 10
   299 Out[]: 10
   266 
   300 
   267 In [2]: 3124 * 126789
   301 In []: 45 % 2
   268 Out[2]: 396088836
   302 Out[]: 1
   269 
   303 
   270 In [3]: a = 3124 * 126789
   304 In []: 864675 % 10
   271 
   305 Out[]: 5
   272 In [4]: big = 1234567891234567890 ** 3
   306 
   273 
   307 In []: 3124 * 126789
   274 In [5]: verybig = big * big * big * big
   308 Out[]: 396088836
   275   \end{lstlisting}
   309 
   276 \end{frame}
   310 In []: big = 1234567891234567890 ** 3
   277 
   311 
   278 \begin{frame}[fragile]
   312 In []: verybig = big * big * big * big
   279   \frametitle{Arithematic operators \ldots}
   313   \end{lstlisting}
   280   \begin{lstlisting}
   314 \end{frame}
   281 In [1]: 17/2
   315 
   282 Out[1]: 8
   316 \begin{frame}[fragile]
   283 
   317   \frametitle{Arithmetic operators}
   284 In [2]: 17/2.0
   318   \begin{lstlisting}
   285 Out[2]: 8.5
   319 In []: 17 / 2
   286 
   320 Out[]: 8
   287 In [3]: 17.0/2
   321 
   288 Out[3]: 8.5
   322 In []: 17 / 2.0
   289 
   323 Out[]: 8.5
   290 In [4]: 17.0/8.5
   324 
   291 Out[4]: 2.0
   325 In []: 17.0 / 2
       
   326 Out[]: 8.5
       
   327 
       
   328 In []: 17.0 / 8.5
       
   329 Out[]: 2.0
       
   330   \end{lstlisting}
       
   331 \end{frame}
       
   332 
       
   333 \begin{frame}[fragile]
       
   334   \frametitle{Arithmetic operators}
       
   335   \begin{lstlisting}
       
   336 In []: a = 7546
       
   337 
       
   338 In []: a += 1
       
   339 In []: a
       
   340 Out[]: 7547
       
   341 
       
   342 In []: a -= 5
       
   343 In []: a
       
   344 
       
   345 In []: a *= 2
       
   346 
       
   347 In []: a /= 5
   292   \end{lstlisting}
   348   \end{lstlisting}
   293 \end{frame}
   349 \end{frame}
   294 
   350 
   295 \begin{frame}[fragile]
   351 \begin{frame}[fragile]
   296   \frametitle{String operations}
   352   \frametitle{String operations}
   297   \begin{lstlisting}
   353   \begin{lstlisting}
   298 In [1]: s = 'Hello '
   354 In []: s = 'Hello '
   299 
   355 
   300 In [2]: p = 'World'
   356 In []: p = 'World'
   301 
   357 
   302 In [3]: s + p 
   358 In []: s + p 
   303 Out[3]: 'Hello World'
   359 Out[]: 'Hello World'
   304 
   360 
   305 In [4]: s * 4
   361 In []: s * 4
   306 Out[4]: 'Hello Hello Hello Hello'
   362 Out[]: 'Hello Hello Hello Hello'
   307   \end{lstlisting}
   363   \end{lstlisting}
   308 \end{frame}
   364 \end{frame}
   309 
   365 
   310 \begin{frame}[fragile]
   366 \begin{frame}[fragile]
   311   \frametitle{String operations \ldots}
   367   \frametitle{String operations \ldots}
   312   \begin{lstlisting}
   368   \begin{lstlisting}
   313 In [1]: s * s
   369 In []: s * s
   314 --------------------------------------------
   370 --------------------------------------------
   315 TypeError  Traceback (most recent call last)
   371 TypeError  Traceback (most recent call last)
   316 
   372 
   317 /<ipython console> in <module>()
   373 /<ipython console> in <module>()
   318 
   374 
   322 \end{frame}
   378 \end{frame}
   323 
   379 
   324 \begin{frame}[fragile]
   380 \begin{frame}[fragile]
   325   \frametitle{Relational and logical operators}
   381   \frametitle{Relational and logical operators}
   326   \begin{lstlisting}
   382   \begin{lstlisting}
   327 In [1]: pos, zer, neg = 1, 0, -1
   383 In []: p, z, n = 1, 0, -1
   328 In [2]: pos == neg
   384 In []: p == n
   329 Out[2]: False
   385 Out[]: False
   330 
   386 
   331 In [3]: pos >= neg
   387 In []: p >= n
   332 Out[3]: True
   388 Out[]: True
   333 
   389 
   334 In [4]: neg < zer < pos
   390 In []: n < z < p
   335 Out[4]: True
   391 Out[]: True
   336 
   392 
   337 In [5]: pos + neg != zer
   393 In []: p + n != z
   338 Out[5]: False
   394 Out[]: False
   339   \end{lstlisting}
   395   \end{lstlisting}
   340 \end{frame}
   396 \end{frame}
   341 
   397 
   342 \begin{frame}[fragile]
   398 \begin{frame}[fragile]
   343   \frametitle{Built-ins}
   399   \frametitle{Built-ins}
   344   \begin{lstlisting}
   400   \begin{lstlisting}
   345 In [1]: int(17/2.0)
   401 In []: int(17 / 2.0)
   346 Out[1]: 8
   402 Out[]: 8
   347 
   403 
   348 In [2]: float(17/2)  # Recall
   404 In []: float(17 / 2)  # Recall
   349 Out[2]: 8.0
   405 Out[]: 8.0
   350 
   406 
   351 In [3]: str(17/2.0)
   407 In []: str(17 / 2.0)
   352 Out[3]: '8.5'
   408 Out[]: '8.5'
   353 
   409 
   354 In [4]: round( 7.5 )
   410 In []: round( 7.5 )
   355 Out[4]: 8.0
   411 Out[]: 8.0
   356   \end{lstlisting}
   412   \end{lstlisting}
   357 \end{frame}
   413 \end{frame}
   358 
   414 
   359 \begin{frame}[fragile]
   415 \begin{frame}[fragile]
   360   \frametitle{Odds and ends}
   416   \frametitle{Odds and ends}
   361   \begin{itemize}
   417   \begin{itemize}
   362     \item Case sensitive
   418     \item Case sensitive
   363     \item Dynamically typed $\Rightarrow$ need not specify a type
   419     \item Dynamically typed $\Rightarrow$ need not specify a type
   364       \begin{lstlisting}
   420       \begin{lstlisting}
   365 In [1]: a = 1
   421 In []: a = 1
   366 In [2]: a = 1.1
   422 In []: a = 1.1
   367 In [3]: a = "Now I am a string!"
   423 In []: a = "Now I am a string!"
   368       \end{lstlisting}
   424       \end{lstlisting}
   369     \item Comments:
   425     \item Comments:
   370       \begin{lstlisting}
   426       \begin{lstlisting}
   371 In [4]: a = 1  # In-line comments
   427 In []: a = 1  # In-line comments
   372 In [5]: # A comment line.
   428 In []: # A comment line.
   373 In [6]: a = "# Not a comment!"
   429 In []: a = "# Not a comment!"
   374       \end{lstlisting}
   430       \end{lstlisting}
   375   \end{itemize}
   431   \end{itemize}
   376   \inctime{15}
   432   \inctime{15}
   377 \end{frame}
   433 \end{frame}
   378 
   434 
   379 \section{Simple IO}
   435 \section{Simple IO}
   380 \begin{frame}[fragile]
   436 \begin{frame}[fragile]
   381   \frametitle{Simple IO: Console Input}
   437   \frametitle{Simple IO: Console Input}
       
   438   \small
   382   \begin{itemize}
   439   \begin{itemize}
   383     \item raw\_input() waits for user input.
   440     \item raw\_input() waits for user input.
   384       \begin{lstlisting}
   441       \begin{lstlisting}
   385 In [1]: a = raw_input()
   442 In []: a = raw_input()
   386 5
   443 5
   387 
   444 
   388 In [2]: a = raw_input('prompt > ')
   445 In []: a = raw_input('Enter a value: ')
   389 prompt > 5
   446 Enter a value: 5
   390       \end{lstlisting}
   447       \end{lstlisting}
   391     \item Prompt string is optional.
   448     \item Prompt string is optional.
   392     \item All keystrokes are Strings!
   449     \item All keystrokes are Strings!
   393     \item \texttt{int()} converts string to int.
   450     \item \texttt{int()} converts string to int.
   394   \end{itemize}
   451   \end{itemize}
   395 \end{frame}
   452 \end{frame}
   396 
   453 
   397 \begin{frame}{Simple IO: Console output}
   454 \begin{frame}[fragile]
       
   455   \frametitle{Simple IO: Console output}
   398   \begin{itemize}
   456   \begin{itemize}
   399     \item \texttt{print} is straight forward
   457     \item \texttt{print} is straight forward
   400     \item Note the distinction between \texttt{print x} and \texttt{print x,}
   458     \item Put the following code snippet in a file
   401   \end{itemize}
   459   \end{itemize}
       
   460   \begin{lstlisting}
       
   461 print "Hello"
       
   462 print "World"
       
   463 
       
   464 
       
   465 In []: %run -i hello1.py
       
   466 Hello
       
   467 World
       
   468   \end{lstlisting}
       
   469 \end{frame}
       
   470 
       
   471 \begin{frame}[fragile]
       
   472   \frametitle{Simple IO: Console output \ldots}
       
   473 Put the following code snippet in a file
       
   474   \begin{lstlisting}
       
   475 print "Hello",
       
   476 print "World"
       
   477 
       
   478 
       
   479 In []: %run -i hello2.py
       
   480 Hello World
       
   481   \end{lstlisting}
       
   482 
       
   483 \emphbar{Note the distinction between \texttt{print x} and \texttt{print x,}}
   402 \end{frame}
   484 \end{frame}
   403 
   485 
   404 \section{Control flow}
   486 \section{Control flow}
   405 \begin{frame}
   487 \begin{frame}
   406   \frametitle{Control flow constructs}  
   488   \frametitle{Control flow constructs}  
   407   \begin{itemize}
   489   \begin{itemize}
   408   \item \kwrd{if/elif/else}: branching
   490   \item \kwrd{if/elif/else}: branching
       
   491   \item \kwrd{C if X else Y}: Ternary conditional operator
   409   \item \kwrd{while}: looping
   492   \item \kwrd{while}: looping
   410   \item \kwrd{for}: iterating 
   493   \item \kwrd{for}: iterating
   411   \item \kwrd{break, continue}: modify loop 
   494   \item \kwrd{break, continue}: modify loop 
   412   \item \kwrd{pass}: syntactic filler
   495   \item \kwrd{pass}: syntactic filler
   413   \end{itemize}
   496   \end{itemize}
   414 \end{frame}
   497 \end{frame}
   415 
   498 
   416 \subsection{Basic Conditional flow}
   499 \subsection{Basic Conditional flow}
   417 \begin{frame}[fragile]
   500 \begin{frame}[fragile]
   418   \frametitle{\typ{If...elif...else} example}
   501   \frametitle{\typ{If...elif...else} example}
   419   \begin{lstlisting}
   502   \small
   420 x = int(raw_input("Enter an integer:"))
   503   \begin{lstlisting}
   421 if x < 0:
   504 In []: x = int(raw_input("Enter an integer:"))
   422      print 'Be positive!'
   505 
   423 elif x == 0:
   506 In []: if x < 0:
   424      print 'Zero'
   507   ...:     print 'Be positive!'
   425 elif x == 1:
   508   ...: elif x == 0:
   426      print 'Single'
   509   ...:     print 'Zero'
   427 else:
   510   ...: elif x == 1:
   428      print 'More'
   511   ...:     print 'Single'
       
   512   ...: else:
       
   513   ...:     print 'More'
       
   514   ...:
       
   515   ...:
   429   \end{lstlisting}
   516   \end{lstlisting}
   430   \inctime{10}
   517   \inctime{10}
   431 \end{frame}
   518 \end{frame}
   432 
   519 
   433 \subsection{Basic Looping}
   520 \begin{frame}[fragile]
   434 \begin{frame}[fragile]
   521   \frametitle{Ternary conditional operator}
   435   \frametitle{\typ{while}}
   522   \begin{lstlisting}
   436 Example: Fibonacci series
   523 ...
   437   \begin{lstlisting}
   524 a = raw_input('Enter number(Q to quit):')
   438 # the sum of two elements
   525 num = int(a) if a != 'Q' else break
   439 # defines the next
   526 ...
   440 a, b = 0, 1
   527   \end{lstlisting}
   441 while b < 10:
       
   442     print b,
       
   443     a, b = b, a + b 
       
   444 \end{lstlisting}
       
   445 \typ{1 1 2 3 5 8}\\  
       
   446 \end{frame}
       
   447 
       
   448 \begin{frame}[fragile]
       
   449 \frametitle{\typ{range()}}
       
   450 \kwrd{range([start,] stop[, step])}\\
       
   451 \begin{itemize}
       
   452   \item range() returns a list of integers
       
   453   \item The \emph{start} and the \emph{step} arguments are optional
       
   454   \item \emph{stop} argument is not included in the list
       
   455 \end{itemize}
       
   456 \end{frame}
       
   457 
       
   458 \begin{frame}[fragile]
       
   459   \frametitle{\typ{for} \ldots \typ{range()}}
       
   460 Example: print squares of first \typ{n} numbers
       
   461   \begin{lstlisting}
       
   462 In []: for i in range(5):
       
   463  ....:     print i, i * i
       
   464  ....:
       
   465  ....:
       
   466 0 0
       
   467 1 1
       
   468 2 4
       
   469 3 9
       
   470 4 16
       
   471 \end{lstlisting}
       
   472 \inctime{5}
       
   473 \end{frame}
       
   474 
       
   475 \subsection{Exercises}
       
   476 
       
   477 \begin{frame}{Problem set 1: Problem 1.1}
       
   478   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$\\
       
   479 \vspace*{0.2in}
       
   480 \emphbar{These are called $Armstrong$ numbers.}
       
   481 \end{frame}
       
   482 
       
   483 \begin{frame}{Problem 1.2 - Collatz sequence}
       
   484 \begin{enumerate}
       
   485   \item Start with an arbitrary (positive) integer. 
       
   486   \item If the number is even, divide by 2; if the number is odd, multiply by 3 and add 1.
       
   487   \item Repeat the procedure with the new number.
       
   488   \item It appears that for all starting values there is a cycle of 4, 2, 1 at which the procedure loops.
       
   489 \end{enumerate}
       
   490     Write a program that accepts the starting value and prints out the Collatz sequence.
       
   491 \end{frame}
       
   492 
       
   493 \begin{frame}[fragile]{Problem 1.3}
       
   494   Write a program that prints the following pyramid on the screen. 
       
   495   \begin{lstlisting}
       
   496 1
       
   497 2  2
       
   498 3  3  3
       
   499 4  4  4  4
       
   500   \end{lstlisting}
       
   501 The number of lines must be obtained from the user as input.\\
       
   502 \pause
       
   503 \emphbar{When can your code fail?}
       
   504 \inctime{5}
       
   505 \end{frame}
   528 \end{frame}
   506 
   529 
   507 \begin{frame}[fragile]
   530 \begin{frame}[fragile]
   508   \frametitle{What did we learn?}
   531   \frametitle{What did we learn?}
   509   \begin{itemize}
   532   \begin{itemize}
   510     \item Basic data types
   533     \item Data types: int, float, complex, boolean, string
   511     \item Operators
   534     \item Operators: +, -, *, /, \%, **, +=, -=, *=, /=, >, <, <=, >=, ==, !=, a < b < c
   512     \item Conditional structures
   535     \item Simple IO: \kwrd{raw\_input} and \kwrd{print}
   513     \item Loops
   536     \item Conditional structures: \kwrd{if/elif/else},\\ \kwrd{C if X else Y}
   514   \end{itemize}
   537   \end{itemize}
   515 \end{frame}
   538 \end{frame}
   516 
   539 
   517 \end{document}
   540 \end{document}