day2/session1.tex
branchscipyin2010
changeset 449 49e10e9fc660
parent 420 5e408de16a14
equal deleted inserted replaced
448:dc8d666a594a 449:49e10e9fc660
    49 }
    49 }
    50 \newcounter{time}
    50 \newcounter{time}
    51 \setcounter{time}{0}
    51 \setcounter{time}{0}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    53 
    53 
    54 \newcommand{\typ}[1]{\lstinline{#1}}
    54 \newcommand{\typ}[1]{\textbf{\texttt{{#1}}}}
    55 
    55 
    56 
    56 
    57 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    57 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    58 
    58 
    59 %%% This is from Fernando's setup.
    59 %%% This is from Fernando's setup.
    77 \title[Basic Python]{Python language: Basics}
    77 \title[Basic Python]{Python language: Basics}
    78 
    78 
    79 \author[FOSSEE Team] {The FOSSEE Group}
    79 \author[FOSSEE Team] {The FOSSEE Group}
    80 
    80 
    81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    82 \date[] {SciPy 2010, Introductory tutorials\\Day 2, Session 1}
    82 \date[] {SciPy.in 2010, Tutorials}
    83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    84 
    84 
    85 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    85 %\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
    86 %\logo{\pgfuseimage{iitmlogo}}
    86 %\logo{\pgfuseimage{iitmlogo}}
    87 
    87 
   147   \end{lstlisting}
   147   \end{lstlisting}
   148     \item \kwrd{float}
   148     \item \kwrd{float}
   149   \begin{lstlisting}
   149   \begin{lstlisting}
   150 In []: p = 3.141592
   150 In []: p = 3.141592
   151   \end{lstlisting}
   151   \end{lstlisting}
   152   \end{itemize}
   152     \item \kwrd{complex}
   153 \end{frame}
       
   154 
       
   155 \begin{frame}[fragile]
       
   156 \frametitle{Complex numbers}
       
   157   \begin{lstlisting}
   153   \begin{lstlisting}
   158 In []: c = 3+4j
   154 In []: c = 3+4j
   159 
   155   \end{lstlisting}
   160 In []: abs(c)
   156   \end{itemize}
   161 Out[]: 5.0
       
   162 
       
   163 In []: c.imag
       
   164 Out[]: 4.0
       
   165 
       
   166 In []: c.real
       
   167 Out[]: 3.0
       
   168   \end{lstlisting}
       
   169 \end{frame}
   157 \end{frame}
   170 
   158 
   171 \subsection{Booleans}
   159 \subsection{Booleans}
   172 \begin{frame}[fragile]
   160 \begin{frame}[fragile]
   173   \frametitle{Booleans}
   161   \frametitle{Booleans}
   180 Out[]: True
   168 Out[]: True
   181 
   169 
   182 In []: F and t
   170 In []: F and t
   183 Out[]: False
   171 Out[]: False
   184   \end{lstlisting}
   172   \end{lstlisting}
   185   \inctime{5}
   173 %%  \inctime{5}
   186 \end{frame}
   174 \end{frame}
   187 
   175 
   188 \begin{frame}[fragile]
   176 \begin{frame}[fragile]
   189   \frametitle{( )  for precedence}
   177   \frametitle{( )  for precedence}
   190   \begin{lstlisting}
   178   \begin{lstlisting}
   196 Out[]: True
   184 Out[]: True
   197 
   185 
   198 In []: a and (b or c)
   186 In []: a and (b or c)
   199 Out[]: False
   187 Out[]: False
   200   \end{lstlisting}
   188   \end{lstlisting}
   201   \inctime{5}
   189 %%  \inctime{5}
   202 \end{frame}
   190 \end{frame}
   203 
   191 
   204 \subsection{Strings}
   192 \subsection{Strings}
   205 
   193 
   206 \begin{frame}[fragile]
   194 \begin{frame}[fragile]
       
   195 \frametitle{Strings}
       
   196 Anything within ``quotes'' is a string!
       
   197 \begin{lstlisting}
       
   198 ' This is a string '  
       
   199 " This too! "
       
   200 """ This one too! """
       
   201 ''' And one more! '''
       
   202 \end{lstlisting}
       
   203 \end{frame}
       
   204 
       
   205 \begin{frame}[fragile]
       
   206 \frametitle{Strings}
       
   207 Why so many?
       
   208 \begin{lstlisting}
       
   209 ' "Do or do not.  No try." said Yoda.'  
       
   210 " ' is a mighty lonely quote."
       
   211 \end{lstlisting}
       
   212 The triple quoted ones can span multiple lines!
       
   213 
       
   214 \begin{lstlisting}
       
   215 """ The quick brown
       
   216 fox jumped over
       
   217     the lazy dingbat. 
       
   218 """
       
   219 \end{lstlisting}
       
   220 \end{frame}
       
   221 
       
   222 \begin{frame}[fragile]
   207   \frametitle{Strings}
   223   \frametitle{Strings}
   208 Strings were introduced previously, let us now look at them in a little more detail.
       
   209   \begin{lstlisting}
   224   \begin{lstlisting}
   210 In []: w = "hello"
   225 In []: w = "hello"
   211 
   226 
   212 In []: print w[0] + w[2] + w[-1]
   227 In []: print w[0], w[1], w[-1]
   213 Out[]: hlo
       
   214 
   228 
   215 In []: len(w)
   229 In []: len(w)
   216 Out[]: 5
   230 Out[]: 5
   217   \end{lstlisting}
   231   \end{lstlisting}
   218 \end{frame}
   232 \end{frame}
   230 
   244 
   231 <ipython console> in <module>()
   245 <ipython console> in <module>()
   232 
   246 
   233 TypeError: 'str' object does not
   247 TypeError: 'str' object does not
   234          support item assignment
   248          support item assignment
       
   249   \end{lstlisting}
       
   250 \end{frame}
       
   251 
       
   252 \section{Operators}
       
   253 
       
   254 \begin{frame}[fragile]
       
   255   \frametitle{Arithmetic operators}
       
   256   \small
       
   257   \begin{lstlisting}
       
   258 In []: 1786 % 12
       
   259 Out[]: 10
       
   260 
       
   261 In []: 45 % 2
       
   262 Out[]: 1
       
   263 
       
   264 In []: 864675 % 10
       
   265 Out[]: 5
       
   266 
       
   267 In []: 3124 * 126789
       
   268 Out[]: 396088836
       
   269 
       
   270 In []: big = 1234567891234567890 ** 3
       
   271 
       
   272 In []: verybig = big * big * big * big
       
   273   \end{lstlisting}
       
   274 \end{frame}
       
   275 
       
   276 \begin{frame}[fragile]
       
   277   \frametitle{Arithmetic operators}
       
   278   \begin{lstlisting}
       
   279 In []: 17 / 2
       
   280 Out[]: 8
       
   281 
       
   282 In []: 17 / 2.0
       
   283 Out[]: 8.5
       
   284 
       
   285 In []: 17.0 / 2
       
   286 Out[]: 8.5
       
   287 
       
   288 In []: 17.0 / 8.5
       
   289 Out[]: 2.0
       
   290   \end{lstlisting}
       
   291 \end{frame}
       
   292 
       
   293 \begin{frame}[fragile]
       
   294 \frametitle{Arithmetic operators}
       
   295   \begin{lstlisting}
       
   296 In []: c = 3+4j
       
   297 
       
   298 In []: abs(c)
       
   299 Out[]: 5.0
       
   300 
       
   301 In []: c.imag
       
   302 Out[]: 4.0
       
   303 
       
   304 In []: c.real
       
   305 Out[]: 3.0
       
   306   \end{lstlisting}
       
   307 \end{frame}
       
   308 
       
   309 \begin{frame}[fragile]
       
   310   \frametitle{Arithmetic operators}
       
   311   \begin{lstlisting}
       
   312 In []: a = 7546
       
   313 
       
   314 In []: a += 1
       
   315 In []: a
       
   316 Out[]: 7547
       
   317 
       
   318 In []: a -= 5
       
   319 In []: a
       
   320 
       
   321 In []: a *= 2
       
   322 
       
   323 In []: a /= 5
       
   324   \end{lstlisting}
       
   325 \end{frame}
       
   326 
       
   327 \begin{frame}[fragile]
       
   328   \frametitle{String operations}
       
   329   \begin{lstlisting}
       
   330 In []: s = 'Hello'
       
   331 
       
   332 In []: p = 'World'
       
   333 
       
   334 In []: s + p 
       
   335 Out[]: 'HelloWorld'
       
   336 
       
   337 In []: s * 4
       
   338 Out[]: 'HelloHelloHelloHello'
       
   339   \end{lstlisting}
       
   340 \end{frame}
       
   341 
       
   342 \begin{frame}[fragile]
       
   343   \frametitle{String operations \ldots}
       
   344   \begin{lstlisting}
       
   345 In []: s * s
       
   346   \end{lstlisting}
       
   347   \pause
       
   348   \begin{lstlisting}
       
   349 --------------------------------------------
       
   350 TypeError  Traceback (most recent call last)
       
   351 
       
   352 <ipython console> in <module>()
       
   353 
       
   354 TypeError: can`t multiply sequence by
       
   355                 non-int of type `str`
   235   \end{lstlisting}
   356   \end{lstlisting}
   236 \end{frame}
   357 \end{frame}
   237 
   358 
   238 \begin{frame}[fragile]
   359 \begin{frame}[fragile]
   239   \frametitle{String methods}
   360   \frametitle{String methods}
   252 Out[]: 'hello world'
   373 Out[]: 'hello world'
   253   \end{lstlisting}
   374   \end{lstlisting}
   254 \end{frame}
   375 \end{frame}
   255 
   376 
   256 \begin{frame}[fragile]
   377 \begin{frame}[fragile]
   257   \frametitle{A bit about IPython}
   378 \frametitle{Strings: \typ{split} \& \typ{join}}
   258   Recall, we showed a few features of IPython, here is one more:
   379   \begin{lstlisting}
   259   \begin{itemize}
   380 In []: chars = 'a b c'
   260     \item IPython provides better help
   381 In []: chars.split()
   261     \item object.function?
   382 Out[]: ['a', 'b', 'c']
   262     \begin{lstlisting}
   383 In []: ' '.join(['a', 'b', 'c'])
   263 In []: a = 'Hello World'
   384 Out[]: 'a b c'
   264 In []: a.lower?
   385   \end{lstlisting}
   265     \end{lstlisting}
   386 
   266   \end{itemize}
   387   \begin{lstlisting}
   267 \end{frame}
   388 In []: alpha = ', '.join(['a', 'b', 'c'])
   268 
   389 In []: alpha
   269 \begin{frame}[fragile]
       
   270   \frametitle{Still with strings}
       
   271   \begin{itemize}
       
   272     \item We saw split() yesterday
       
   273     \item join() is the opposite of split()
       
   274   \end{itemize}
       
   275   \begin{lstlisting}
       
   276 In []: ''.join(['a', 'b', 'c'])
       
   277 Out[]: 'abc'
       
   278 
       
   279 In []: ', '.join(['a', 'b', 'c'])
       
   280 Out[]: 'a, b, c'
   390 Out[]: 'a, b, c'
       
   391 In []: alpha.split(', ')
       
   392 Out[]: ['a', 'b', 'c']
   281   \end{lstlisting}
   393   \end{lstlisting}
   282 \end{frame}
   394 \end{frame}
   283 
   395 
   284 \begin{frame}[fragile]
   396 \begin{frame}[fragile]
   285 \frametitle{String formatting}
   397 \frametitle{String formatting}
   291   \end{lstlisting}
   403   \end{lstlisting}
   292   \begin{itemize}
   404   \begin{itemize}
   293     \item \emph{\%d}, \emph{\%f} etc. available
   405     \item \emph{\%d}, \emph{\%f} etc. available
   294   \end{itemize}
   406   \end{itemize}
   295   \emphbar{\url{http://docs.python.org/library/stdtypes.html}}
   407   \emphbar{\url{http://docs.python.org/library/stdtypes.html}}
   296   \inctime{10}
   408 %%  \inctime{10}
   297 \end{frame}
       
   298 
       
   299 \section{Operators}
       
   300 \begin{frame}[fragile]
       
   301   \frametitle{Arithmetic operators}
       
   302   \small
       
   303   \begin{lstlisting}
       
   304 In []: 1786 % 12
       
   305 Out[]: 10
       
   306 
       
   307 In []: 45 % 2
       
   308 Out[]: 1
       
   309 
       
   310 In []: 864675 % 10
       
   311 Out[]: 5
       
   312 
       
   313 In []: 3124 * 126789
       
   314 Out[]: 396088836
       
   315 
       
   316 In []: big = 1234567891234567890 ** 3
       
   317 
       
   318 In []: verybig = big * big * big * big
       
   319   \end{lstlisting}
       
   320 \end{frame}
       
   321 
       
   322 \begin{frame}[fragile]
       
   323   \frametitle{Arithmetic operators}
       
   324   \begin{lstlisting}
       
   325 In []: 17 / 2
       
   326 Out[]: 8
       
   327 
       
   328 In []: 17 / 2.0
       
   329 Out[]: 8.5
       
   330 
       
   331 In []: 17.0 / 2
       
   332 Out[]: 8.5
       
   333 
       
   334 In []: 17.0 / 8.5
       
   335 Out[]: 2.0
       
   336   \end{lstlisting}
       
   337 \end{frame}
       
   338 
       
   339 \begin{frame}[fragile]
       
   340   \frametitle{Arithmetic operators}
       
   341   \begin{lstlisting}
       
   342 In []: a = 7546
       
   343 
       
   344 In []: a += 1
       
   345 In []: a
       
   346 Out[]: 7547
       
   347 
       
   348 In []: a -= 5
       
   349 In []: a
       
   350 
       
   351 In []: a *= 2
       
   352 
       
   353 In []: a /= 5
       
   354   \end{lstlisting}
       
   355 \end{frame}
       
   356 
       
   357 \begin{frame}[fragile]
       
   358   \frametitle{String operations}
       
   359   \begin{lstlisting}
       
   360 In []: s = 'Hello'
       
   361 
       
   362 In []: p = 'World'
       
   363 
       
   364 In []: s + p 
       
   365 Out[]: 'HelloWorld'
       
   366 
       
   367 In []: s * 4
       
   368 Out[]: 'HelloHelloHelloHello'
       
   369   \end{lstlisting}
       
   370 \end{frame}
       
   371 
       
   372 \begin{frame}[fragile]
       
   373   \frametitle{String operations \ldots}
       
   374   \begin{lstlisting}
       
   375 In []: s * s
       
   376   \end{lstlisting}
       
   377   \pause
       
   378   \begin{lstlisting}
       
   379 --------------------------------------------
       
   380 TypeError  Traceback (most recent call last)
       
   381 
       
   382 <ipython console> in <module>()
       
   383 
       
   384 TypeError: can`t multiply sequence by
       
   385                 non-int of type `str`
       
   386   \end{lstlisting}
       
   387 \end{frame}
   409 \end{frame}
   388 
   410 
   389 \begin{frame}[fragile]
   411 \begin{frame}[fragile]
   390   \frametitle{Relational and logical operators}
   412   \frametitle{Relational and logical operators}
   391   \begin{lstlisting}
   413   \begin{lstlisting}
   436 In []: a = 1  # In-line comments
   458 In []: a = 1  # In-line comments
   437 In []: # A comment line.
   459 In []: # A comment line.
   438 In []: a = "# Not a comment!"
   460 In []: a = "# Not a comment!"
   439       \end{lstlisting}
   461       \end{lstlisting}
   440   \end{itemize}
   462   \end{itemize}
   441   \inctime{15}
   463 %%  \inctime{15}
   442 \end{frame}
   464 \end{frame}
   443 
   465 
   444 \section{Simple IO}
   466 \section{Simple IO}
   445 \begin{frame}[fragile]
   467 \begin{frame}[fragile]
   446   \frametitle{Simple IO: Console Input}
   468   \frametitle{Simple IO: Console Input}
   457 In []: a = raw_input('Enter a value: ')
   479 In []: a = raw_input('Enter a value: ')
   458 Enter a value: 5
   480 Enter a value: 5
   459       \end{lstlisting}
   481       \end{lstlisting}
   460     \item Prompt string is optional.
   482     \item Prompt string is optional.
   461     \item All keystrokes are strings!
   483     \item All keystrokes are strings!
   462     \item \texttt{int()} converts string to int.
   484     \item \typ{int()} converts string to int.
   463   \end{itemize}
   485   \end{itemize}
   464 \end{frame}
   486 \end{frame}
   465 
   487 
   466 \begin{frame}[fragile]
   488 \begin{frame}[fragile]
   467   \frametitle{Simple IO: Console output}
   489   \frametitle{Simple IO: Console output}
   498 \section{Control flow}
   520 \section{Control flow}
   499 \begin{frame}
   521 \begin{frame}
   500   \frametitle{Control flow constructs}  
   522   \frametitle{Control flow constructs}  
   501   \begin{itemize}
   523   \begin{itemize}
   502   \item \kwrd{if/elif/else}: branching
   524   \item \kwrd{if/elif/else}: branching
   503   \item \kwrd{C if X else D}: Ternary conditional operator
       
   504   \item \kwrd{while}: looping
   525   \item \kwrd{while}: looping
   505   \item \kwrd{for}: iterating
   526   \item \kwrd{for}: iterating
   506   \item \kwrd{break, continue}: modify loop 
   527   \item \kwrd{break, continue}: modify loop 
   507   \item \kwrd{pass}: syntactic filler
   528   \item \kwrd{pass}: syntactic filler
   508   \end{itemize}
   529   \end{itemize}
   509 \end{frame}
   530 \end{frame}
   510 
   531 
   511 \subsection{Basic Conditional flow}
   532 \subsection{Basic Conditional flow}
   512 \begin{frame}[fragile]
   533 \begin{frame}[fragile]
   513   \frametitle{\typ{If...elif...else} example}
   534   \frametitle{\typ{if...elif...else} example}
   514 Type the following code in an editor \& save as \alert{ladder.py}
   535 Type the following code in an editor \& save as \alert{ladder.py}
   515   \small
   536   \small
   516   \begin{lstlisting}
   537   \begin{lstlisting}
   517 x = int(raw_input("Enter an integer:"))
   538 x = int(raw_input("Enter an integer:"))
   518 if x < 0:
   539 if x < 0:
   523     print 'Single'
   544     print 'Single'
   524 else:
   545 else:
   525     print 'More'
   546     print 'More'
   526 
   547 
   527   \end{lstlisting}
   548   \end{lstlisting}
   528   \inctime{10}
   549 %%  \inctime{10}
   529 \end{frame}
   550 \end{frame}
   530 
   551 
   531 \begin{frame}[fragile]
   552 \section{Control flow}
   532   \frametitle{Ternary conditional operator}
   553 \subsection{Basic Looping}
   533   \begin{lstlisting}
   554 \begin{frame}[fragile]
   534 ...
   555   \frametitle{\typ{while}}
   535 a = raw_input('Enter number(Q to quit):')
   556 \begin{block}{Example: Fibonacci series}
   536 num = int(a) if a != 'Q' else 0
   557   Sum of previous two elements defines the next
   537 ...
   558 \end{block}
   538   \end{lstlisting}
   559   \begin{lstlisting}
   539 \end{frame}
   560 In []: a, b = 0, 1
       
   561 In []: while b < 10:
       
   562   ...:     print b,
       
   563   ...:     a, b = b, a + b
       
   564   ...:
       
   565   ...:
       
   566 \end{lstlisting}
       
   567 \typ{1 1 2 3 5 8}\\
       
   568 \end{frame}
       
   569 
       
   570 \begin{frame}[fragile]
       
   571 \frametitle{\typ{range()}}
       
   572 \kwrd{range([start,] stop[, step])}\\
       
   573 \begin{itemize}
       
   574   \item \typ{range()} returns a list of integers
       
   575   \item The \typ{start} and the \typ{step} arguments are optional
       
   576   \item \typ{stop} is not included in the list
       
   577 \end{itemize}
       
   578 \vspace*{.5in}
       
   579 \begin{block}{Documentation convention}
       
   580   \begin{itemize}
       
   581     \item \alert{Anything within \typ{[]} is optional}
       
   582     \item Nothing to do with Python.
       
   583   \end{itemize}
       
   584 \end{block}
       
   585 \end{frame}
       
   586 
       
   587 \begin{frame}[fragile]
       
   588   \frametitle{\typ{for} \ldots \typ{range()}}
       
   589 Example: print squares of first \typ{5} numbers
       
   590   \begin{lstlisting}
       
   591 In []: for i in range(5):
       
   592  ....:     print i, i * i
       
   593  ....:
       
   594  ....:
       
   595 0 0
       
   596 1 1
       
   597 2 4
       
   598 3 9
       
   599 4 16
       
   600 \end{lstlisting}
       
   601 \end{frame}
       
   602 
       
   603 \begin{frame}[fragile]
       
   604   \frametitle{\typ{for} \ldots \typ{range()}}
       
   605 Example: print squares of odd numbers from 3 to 9
       
   606   \begin{lstlisting}
       
   607 In []: for i in range(3, 10, 2):
       
   608  ....:     print i, i * i
       
   609  ....:
       
   610  ....:
       
   611 3 9
       
   612 5 25
       
   613 7 49
       
   614 9 81
       
   615 \end{lstlisting}
       
   616 %% \inctime{5}
       
   617 \end{frame}
       
   618 
       
   619 \subsection{Exercises}
       
   620 
       
   621 \begin{frame}{Problem 1.1: \emph{Armstrong} numbers}
       
   622   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$\\
       
   623 For example, $153 = 1^3 + 5^3 + 3^3$\\
       
   624 \vspace*{0.2in}
       
   625 \end{frame}
       
   626 
       
   627 \begin{frame}{Problem 1.2: Collatz sequence}
       
   628 \begin{enumerate}
       
   629   \item Start with an arbitrary (positive) integer. 
       
   630   \item If the number is even, divide by 2; if the number is odd, multiply by 3 and add 1.
       
   631   \item Repeat the procedure with the new number.
       
   632   \item It appears that for all starting values there is a cycle of 4, 2, 1 at which the procedure loops.
       
   633 \end{enumerate}
       
   634     Write a program that accepts the starting value and prints out the Collatz sequence.
       
   635 %% \inctime{5}
       
   636 \end{frame}
       
   637 
   540 
   638 
   541 \begin{frame}[fragile]
   639 \begin{frame}[fragile]
   542   \frametitle{What did we learn?}
   640   \frametitle{What did we learn?}
   543   \begin{itemize}
   641   \begin{itemize}
   544     \item Data types: int, float, complex, boolean, string
   642     \item Data types: int, float, complex, boolean, string
   545     \item Operators: +, -, *, /, \%, **, +=, -=, *=, /=, >, <, <=, >=, ==, !=, a < b < c
   643     \item Operators: +, -, *, /, \%, **, +=, -=, *=, /=, >, <, <=, >=, ==, !=, a < b < c
   546     \item Simple IO: \kwrd{raw\_input} and \kwrd{print}
   644     \item Simple IO: \kwrd{raw\_input} and \kwrd{print}
   547     \item Conditional structures: \kwrd{if/elif/else},\\ \kwrd{C if X else D}
   645     \item Conditionals: \kwrd{if elif else}
       
   646     \item Looping: \kwrd{while} \& \kwrd{for} 
   548   \end{itemize}
   647   \end{itemize}
   549 \end{frame}
   648 \end{frame}
   550 
   649 
   551 \end{document}
   650 \end{document}
   552 
   651