day2/session1.tex
changeset 298 df494695e061
parent 288 c4e25269a86c
child 330 46533051b9d3
equal deleted inserted replaced
294:f05b1c457120 298:df494695e061
    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]{\texttt{#1}}
    54 \newcommand{\typ}[1]{\lstinline{#1}}
       
    55 
    55 
    56 
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    57 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    57 
    58 
    58 %%% This is from Fernando's setup.
    59 %%% This is from Fernando's setup.
    59 % \usepackage{color}
    60 % \usepackage{color}
   128 \begin{frame}
   129 \begin{frame}
   129   \frametitle{Primitive Data types}
   130   \frametitle{Primitive Data types}
   130   \begin{itemize}
   131   \begin{itemize}
   131     \item Numbers: float, int, complex
   132     \item Numbers: float, int, complex
   132     \item Strings
   133     \item Strings
   133     \item Boolean
   134     \item Booleans
   134   \end{itemize}
   135   \end{itemize}
   135 \end{frame}
   136 \end{frame}
   136 
   137 
   137 \subsection{Numbers}
   138 \subsection{Numbers}
   138 \begin{frame}[fragile]
   139 \begin{frame}[fragile]
   139   \frametitle{Numbers}
   140   \frametitle{Numbers}
   140   \begin{itemize}
   141   \begin{itemize}
   141     \item \kwrd{int}\\ \kwrd{int} = whole number, no matter what the size!
   142     \item \kwrd{int}\\ whole number, no matter what the size!
   142   \begin{lstlisting}
   143   \begin{lstlisting}
   143 In []: a = 13
   144 In []: a = 13
   144 
   145 
   145 In []: b = 99999999999999999999
   146 In []: b = 99999999999999999999
   146   \end{lstlisting}
   147   \end{lstlisting}
   165 In []: c.real
   166 In []: c.real
   166 Out[]: 3.0
   167 Out[]: 3.0
   167   \end{lstlisting}
   168   \end{lstlisting}
   168 \end{frame}
   169 \end{frame}
   169 
   170 
   170 \subsection{Boolean}
   171 \subsection{Booleans}
   171 \begin{frame}[fragile]
   172 \begin{frame}[fragile]
   172   \frametitle{Boolean}
   173   \frametitle{Booleans}
   173   \begin{lstlisting}
   174   \begin{lstlisting}
   174 In []: t = True
   175 In []: t = True
   175 
   176 
   176 In []: f = not t
   177 In []: f = not t
   177 
   178 
   209 In []: w = "hello"
   210 In []: w = "hello"
   210 
   211 
   211 In []: print w[0] + w[2] + w[-1]
   212 In []: print w[0] + w[2] + w[-1]
   212 Out[]: hlo
   213 Out[]: hlo
   213 
   214 
   214 In []: len(w) # guess what
   215 In []: len(w)
   215 Out[]: 5
   216 Out[]: 5
   216   \end{lstlisting}
   217   \end{lstlisting}
   217 \end{frame}
   218 \end{frame}
   218 
   219 
   219 \begin{frame}[fragile]
   220 \begin{frame}[fragile]
   220   \frametitle{Strings \ldots}
   221   \frametitle{Strings \ldots}
   221   \begin{lstlisting}
   222   \emphbar{Strings are immutable}
   222 In []: w[0] = 'H' # Can't do that!
   223   \begin{lstlisting}
       
   224 In []: w[0] = 'H' 
       
   225   \end{lstlisting}
       
   226   \pause
       
   227   \begin{lstlisting}
   223 --------------------------------------------
   228 --------------------------------------------
   224 TypeError  Traceback (most recent call last)
   229 TypeError  Traceback (most recent call last)
   225 
   230 
   226 /<ipython console> in <module>()
   231 /<ipython console> in <module>()
   227 
   232 
   246 In []: a.lower()
   251 In []: a.lower()
   247 Out[]: 'hello world'
   252 Out[]: 'hello world'
   248   \end{lstlisting}
   253   \end{lstlisting}
   249 \end{frame}
   254 \end{frame}
   250 
   255 
   251 \begin{frame}
   256 \begin{frame}[fragile]
   252   \frametitle{A bit about IPython}
   257   \frametitle{A bit about IPython}
   253   \begin{itemize}
   258   \begin{itemize}
   254     \item IPython provides better help
   259     \item IPython provides better help
   255     \item object.function?
   260     \item object.function?
   256     \begin{lstlisting}
   261     \begin{lstlisting}
   263     \end{lstlisting}
   268     \end{lstlisting}
   264   \end{itemize}
   269   \end{itemize}
   265 \end{frame}
   270 \end{frame}
   266 
   271 
   267 \begin{frame}[fragile]
   272 \begin{frame}[fragile]
   268 \frametitle{Still with strings}
   273   \frametitle{Still with strings}
   269   \begin{itemize}
   274   \begin{itemize}
   270     \item We saw split() yesterday
   275     \item We saw split() yesterday
   271     \item join() is the opposite of split()
   276     \item join() is the opposite of split()
   272   \end{itemize}
   277   \end{itemize}
   273   \begin{lstlisting}
   278   \begin{lstlisting}
   292 \end{frame}
   297 \end{frame}
   293 
   298 
   294 \section{Operators}
   299 \section{Operators}
   295 \begin{frame}[fragile]
   300 \begin{frame}[fragile]
   296   \frametitle{Arithmetic operators}
   301   \frametitle{Arithmetic operators}
       
   302   \small
   297   \begin{lstlisting}
   303   \begin{lstlisting}
   298 In []: 1786 % 12
   304 In []: 1786 % 12
   299 Out[]: 10
   305 Out[]: 10
   300 
   306 
   301 In []: 45 % 2
   307 In []: 45 % 2
   349 \end{frame}
   355 \end{frame}
   350 
   356 
   351 \begin{frame}[fragile]
   357 \begin{frame}[fragile]
   352   \frametitle{String operations}
   358   \frametitle{String operations}
   353   \begin{lstlisting}
   359   \begin{lstlisting}
   354 In []: s = 'Hello '
   360 In []: s = 'Hello'
   355 
   361 
   356 In []: p = 'World'
   362 In []: p = 'World'
   357 
   363 
   358 In []: s + p 
   364 In []: s + p 
   359 Out[]: 'Hello World'
   365 Out[]: 'HelloWorld'
   360 
   366 
   361 In []: s * 4
   367 In []: s * 4
   362 Out[]: 'Hello Hello Hello Hello'
   368 Out[]: 'HelloHelloHelloHello'
   363   \end{lstlisting}
   369   \end{lstlisting}
   364 \end{frame}
   370 \end{frame}
   365 
   371 
   366 \begin{frame}[fragile]
   372 \begin{frame}[fragile]
   367   \frametitle{String operations \ldots}
   373   \frametitle{String operations \ldots}
   368   \begin{lstlisting}
   374   \begin{lstlisting}
   369 In []: s * s
   375 In []: s * s
       
   376   \end{lstlisting}
       
   377   \pause
       
   378   \begin{lstlisting}
   370 --------------------------------------------
   379 --------------------------------------------
   371 TypeError  Traceback (most recent call last)
   380 TypeError  Traceback (most recent call last)
   372 
   381 
   373 /<ipython console> in <module>()
   382 /<ipython console> in <module>()
   374 
   383 
   375 TypeError: can't multiply sequence by
   384 TypeError: can`t multiply sequence by
   376                 non-int of type 'str'
   385                 non-int of type `str`
   377   \end{lstlisting}
   386   \end{lstlisting}
   378 \end{frame}
   387 \end{frame}
   379 
   388 
   380 \begin{frame}[fragile]
   389 \begin{frame}[fragile]
   381   \frametitle{Relational and logical operators}
   390   \frametitle{Relational and logical operators}
   399   \frametitle{Built-ins}
   408   \frametitle{Built-ins}
   400   \begin{lstlisting}
   409   \begin{lstlisting}
   401 In []: int(17 / 2.0)
   410 In []: int(17 / 2.0)
   402 Out[]: 8
   411 Out[]: 8
   403 
   412 
   404 In []: float(17 / 2)  # Recall
   413 In []: float(17 / 2)
   405 Out[]: 8.0
   414 Out[]: 8.0
   406 
   415 
   407 In []: str(17 / 2.0)
   416 In []: str(17 / 2.0)
   408 Out[]: '8.5'
   417 Out[]: '8.5'
   409 
   418 
   453 
   462 
   454 \begin{frame}[fragile]
   463 \begin{frame}[fragile]
   455   \frametitle{Simple IO: Console output}
   464   \frametitle{Simple IO: Console output}
   456   \begin{itemize}
   465   \begin{itemize}
   457     \item \texttt{print} is straight forward
   466     \item \texttt{print} is straight forward
   458     \item Put the following code snippet in a file
   467     \item Put the following code snippet in a file \emph{hello1.py}
   459   \end{itemize}
   468   \end{itemize}
   460   \begin{lstlisting}
   469   \begin{lstlisting}
   461 print "Hello"
   470 print "Hello"
   462 print "World"
   471 print "World"
   463 
   472 
   468   \end{lstlisting}
   477   \end{lstlisting}
   469 \end{frame}
   478 \end{frame}
   470 
   479 
   471 \begin{frame}[fragile]
   480 \begin{frame}[fragile]
   472   \frametitle{Simple IO: Console output \ldots}
   481   \frametitle{Simple IO: Console output \ldots}
   473 Put the following code snippet in a file
   482 Put the following code snippet in a file \emph{hello2.py}
   474   \begin{lstlisting}
   483   \begin{lstlisting}
   475 print "Hello",
   484 print "Hello",
   476 print "World"
   485 print "World"
   477 
   486 
   478 
   487 
   520 \begin{frame}[fragile]
   529 \begin{frame}[fragile]
   521   \frametitle{Ternary conditional operator}
   530   \frametitle{Ternary conditional operator}
   522   \begin{lstlisting}
   531   \begin{lstlisting}
   523 ...
   532 ...
   524 a = raw_input('Enter number(Q to quit):')
   533 a = raw_input('Enter number(Q to quit):')
   525 num = int(a) if a != 'Q' else break
   534 num = int(a) if a != 'Q' else 0
   526 ...
   535 ...
   527   \end{lstlisting}
   536   \end{lstlisting}
   528 \end{frame}
   537 \end{frame}
   529 
   538 
   530 \begin{frame}[fragile]
   539 \begin{frame}[fragile]