day2/session1.tex
changeset 216 c6704d4a18bd
parent 204 87f914f38ba1
child 217 2833f0b51adc
equal deleted inserted replaced
212:298cd56f4d5a 216:c6704d4a18bd
   138 \begin{frame}[fragile]
   138 \begin{frame}[fragile]
   139   \frametitle{Numbers}
   139   \frametitle{Numbers}
   140   \begin{itemize}
   140   \begin{itemize}
   141     \item \kwrd{int}\\ Any whole number is an \kwrd{int}, no matter what the size!
   141     \item \kwrd{int}\\ Any whole number is an \kwrd{int}, no matter what the size!
   142   \begin{lstlisting}
   142   \begin{lstlisting}
   143 In []: a = 13
   143 In [1]: a = 13
   144 
   144 
   145 In []: a = 99999999999999999999
   145 In [2]: b = 99999999999999999999
   146   \end{lstlisting}
   146   \end{lstlisting}
   147     \item \kwrd{float}
   147     \item \kwrd{float}
   148   \begin{lstlisting}
   148   \begin{lstlisting}
   149 In []: fl = 3.141592
   149 In [3]: fl = 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 []: cplx = 3+4j
   157 In [1]: cplx = 3+4j
   158 
   158 
   159 In []: abs(cplx)
   159 In [2]: abs(cplx)
   160 Out[]: 5.0
   160 Out[2]: 5.0
   161 
   161 
   162 In []: cplx.imag
   162 In [3]: cplx.imag
   163 Out[]: 4.0
   163 Out[3]: 4.0
   164 
   164 
   165 In []: cplx.real
   165 In [4]: cplx.real
   166 Out[]: 3.0
   166 Out[4]: 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}{Boolean}
   171 \begin{frame}[fragile]
       
   172   \frametitle{Boolean}
       
   173   \begin{lstlisting}
       
   174 In [1]: t = True
       
   175 
       
   176 In [2]: f = not t
       
   177 Out[2]: False
       
   178 
       
   179 In [3]: f or t
       
   180 Out[3]: True
       
   181 
       
   182 In [4]: f and t
       
   183 Out[4]: False
       
   184   \end{lstlisting}
       
   185 \end{frame}
       
   186 
       
   187 \subsection{Strings}
       
   188 
       
   189 \begin{frame}[fragile]
       
   190   \frametitle{Strings}
       
   191 Strings were introduced previously, let us now look at them in a little more detail.
       
   192   \begin{lstlisting}
       
   193 In [1]: w = "hello"
       
   194 
       
   195 In [2]: print w[0] + w[2] + w[-1]
       
   196 Out[2]: hlo
       
   197 
       
   198 In [3]: len(w) # guess what
       
   199 Out[3]: 5
       
   200   \end{lstlisting}
       
   201 \end{frame}
       
   202 
       
   203 \begin{frame}[fragile]
       
   204   \frametitle{Strings \ldots}
       
   205   \begin{lstlisting}
       
   206 In [1]: w[0] = 'H' # Can't do that!
       
   207 --------------------------------------------
       
   208 TypeError  Traceback (most recent call last)
       
   209 
       
   210 /<ipython console> in <module>()
       
   211 
       
   212 TypeError: 'str' object does not
       
   213          support item assignment
       
   214   \end{lstlisting}
       
   215 \end{frame}
       
   216 
       
   217 \begin{frame}[fragile]
       
   218   \frametitle{String methods}
       
   219   \begin{lstlisting}
       
   220 In [1]: a = 'Hello World'
       
   221 In [2]: a.startswith('Hell')
       
   222 Out[2]: True
       
   223 
       
   224 In [3]: a.endswith('ld')
       
   225 Out[3]: True
       
   226 
       
   227 In [4]: a.upper()
       
   228 Out[4]: 'HELLO WORLD'
       
   229 
       
   230 In [5]: a.lower()
       
   231 Out[5]: 'hello world'
       
   232   \end{lstlisting}
       
   233 \end{frame}
       
   234 
       
   235 \begin{frame}[fragile]
       
   236 \frametitle{Still with strings}
   172   \begin{itemize}
   237   \begin{itemize}
   173     \item \kwrd{True}
   238     \item We saw split() yesterday
   174     \item \kwrd{False}
   239     \item join() is the opposite of split()
   175     \item \kwrd{not}
       
   176     \item \kwrd{and}
       
   177     \item \kwrd{or}
       
   178   \end{itemize}
   240   \end{itemize}
   179 \end{frame}
   241   \begin{lstlisting}
   180 
   242 In [1]: ''.join(['a', 'b', 'c'])
   181 \subsection{Strings}
   243 Out[1]: 'abc'
   182 \begin{frame}[fragile]
   244   \end{lstlisting}
   183   \frametitle{String methods}
   245 \end{frame}
   184 Strings were introduced previously, let us now look at them in a little more detail.
   246 
   185   \begin{lstlisting}
   247 \begin{frame}[fragile]
   186 In []: a = 'hello world'
   248 \frametitle{String formatting}
   187 
   249   \begin{lstlisting}
   188 In []: a.startswith('hell')
   250 In [1]: x, y = 1, 1.234
   189 Out[]: True
   251 
   190 
   252 In [2]: 'x is %s, y is %s' %(x, y)
   191 In []: a.endswith('ld')
   253 Out[2]: 'x is 1, y is 1.234'
   192 Out[]: True
   254   \end{lstlisting}
   193   \end{lstlisting}
   255 \emphbar{
   194 \end{frame}
   256 \url{http://docs.python.org/library/stdtypes.html}\\
   195 
   257 }
   196 \begin{frame}[fragile]
   258 \inctime{10}
   197 \frametitle{Still with strings}
   259 \end{frame}
   198 We saw split() previously. join() is the opposite of split()
   260 
   199   \begin{lstlisting}
   261 \section{Operators}
   200 In []: ''.join(['a', 'b', 'c'])
   262 \begin{frame}[fragile]
   201 Out[]: 'abc'
   263   \frametitle{Arithematic operators}
   202   \end{lstlisting}
   264   \begin{lstlisting}
   203   \begin{block}{Note:}
   265 In [1]: 1786 % 12
   204 Strings are immutable.\\ That is string variables cannot be changed.
   266 Out[1]: 10
       
   267 
       
   268 In [2]: 3124 * 126789
       
   269 Out[2]: 396088836
       
   270 
       
   271 In [3]: a = 3124 * 126789
       
   272 
       
   273 In [4]: big = 1234567891234567890 ** 3
       
   274 
       
   275 In [5]: verybig = big * big * big * big
       
   276   \end{lstlisting}
       
   277 \end{frame}
       
   278 
       
   279 \begin{frame}[fragile]
       
   280   \frametitle{Arithematic operators \ldots}
       
   281   \begin{lstlisting}
       
   282 In [1]: 17/2
       
   283 Out[1]: 8
       
   284 
       
   285 In [2]: 17/2.0
       
   286 Out[2]: 8.5
       
   287 
       
   288 In [3]: 17.0/2
       
   289 Out[3]: 8.5
       
   290 
       
   291 In [4]: 17.0/8.5
       
   292 Out[4]: 2.0
       
   293   \end{lstlisting}
       
   294 \end{frame}
       
   295 
       
   296 \begin{frame}[fragile]
       
   297   \frametitle{String operations}
       
   298   \begin{lstlisting}
       
   299 In [1]: s = 'Hello '
       
   300 
       
   301 In [2]: p = 'World'
       
   302 
       
   303 In [3]: s + p 
       
   304 Out[3]: 'Hello World'
       
   305 
       
   306 In [4]: s * 12 
       
   307 Out[4]: 'Hello Hello Hello Hello ...'
       
   308   \end{lstlisting}
       
   309 \end{frame}
       
   310 
       
   311 \begin{frame}[fragile]
       
   312   \frametitle{String operations \ldots}
       
   313   \begin{lstlisting}
       
   314 In [1]: s * s
       
   315 --------------------------------------------
       
   316 TypeError  Traceback (most recent call last)
       
   317 
       
   318 /<ipython console> in <module>()
       
   319 
       
   320 TypeError: can't multiply sequence by
       
   321                 non-int of type 'str'
       
   322   \end{lstlisting}
       
   323 \end{frame}
       
   324 
       
   325 \begin{frame}[fragile]
       
   326   \frametitle{Relational and logical operators}
       
   327   \begin{lstlisting}
       
   328 In [1]: pos, zer, neg = 1, 0, -1
       
   329 In [2]: pos == neg
       
   330 Out[2]: False
       
   331 
       
   332 In [3]: pos >= neg
       
   333 Out[3]: True
       
   334 
       
   335 In [4]: neg < zer < pos
       
   336 Out[4]: True
       
   337 
       
   338 In [5]: pos + neg != zer
       
   339 Out[5]: False
       
   340   \end{lstlisting}
       
   341 \inctime{5}
       
   342 \end{frame}
       
   343 
       
   344 \begin{frame}[fragile]
       
   345   \frametitle{Built-ins}
       
   346   \begin{lstlisting}
       
   347 In [1]: int(17/2.0)
       
   348 Out[1]: 8
       
   349 
       
   350 In [2]: float(17/2)  # Recall
       
   351 Out[2]: 8.0
       
   352 
       
   353 In [3]: str(17/2.0)
       
   354 Out[3]: '8.5'
       
   355 
       
   356 In [4]: round( 7.5 )
       
   357 Out[4]: 8.0
       
   358   \end{lstlisting}
       
   359 \end{frame}
       
   360 
       
   361 \begin{frame}[fragile]
       
   362   \frametitle{Odds and ends}
       
   363   \begin{itemize}
       
   364     \item Case sensitive
       
   365     \item Dynamically typed $\Rightarrow$ need not specify a type
       
   366       \begin{lstlisting}
       
   367 In [1]: a = 1
       
   368 In [2]: a = 1.1
       
   369 In [3]: a = "Now I am a string!"
       
   370       \end{lstlisting}
       
   371     \item Comments:
       
   372       \begin{lstlisting}
       
   373 In [4]: a = 1  # In-line comments
       
   374 In [5]: # Comment in a line to itself.
       
   375 In [6]: a = "# This is not a comment!"
       
   376       \end{lstlisting}
       
   377   \end{itemize}
       
   378   \inctime{15}
       
   379 \end{frame}
       
   380 
       
   381 \begin{frame}[fragile] \frametitle{A question of good style}
       
   382   \begin{lstlisting}
       
   383     amount = 12.68
       
   384     denom = 0.05
       
   385     nCoins = round(amount/denom)
       
   386     rAmount = nCoins * denom
       
   387   \end{lstlisting}
       
   388   \pause
       
   389   \begin{block}{Style Rule \#1}
       
   390     Naming is 80\% of programming
   205   \end{block}
   391   \end{block}
   206 \end{frame}
       
   207 
       
   208 \begin{frame}[fragile]
       
   209 \frametitle{String formatting}
       
   210   \begin{lstlisting}
       
   211 In []: x, y = 1, 1.234
       
   212 In []: 'x is %s, y is %s' %(x, y)
       
   213 Out[]: 'x is 1, y is 1.234'
       
   214   \end{lstlisting}
       
   215   \small
       
   216 \url{docs.python.org/lib/typesseq-strings.html}\\
       
   217 \inctime{10}
       
   218 \end{frame}
       
   219 
       
   220 \section{Relational and logical operators}
       
   221 \begin{frame}[fragile]
       
   222   \frametitle{Relational and logical operators}
       
   223   \begin{lstlisting}
       
   224 In []: pos, zer, neg = 1, 0, -1
       
   225 In []: pos == neg
       
   226 Out[]: False
       
   227 
       
   228 In []: pos >= neg
       
   229 Out[]: True
       
   230 
       
   231 In []: neg < zer < pos
       
   232 Out[]: True
       
   233 
       
   234 In []: pos + neg != zer
       
   235 Out[]: False
       
   236   \end{lstlisting}
       
   237 \inctime{5}
       
   238 \end{frame}
   392 \end{frame}
   239 
   393 
   240 \begin{frame}
   394 \begin{frame}
   241   {A classic problem}
   395   {A classic problem}
   242   \begin{block}
   396   \begin{block}
   249 They need not be of the same type!
   403 They need not be of the same type!
   250   \end{block}
   404   \end{block}
   251   \inctime{10}
   405   \inctime{10}
   252 \end{frame}
   406 \end{frame}
   253 
   407 
       
   408 \section{Simple IO}
       
   409 \begin{frame}{Simple IO}
       
   410   \begin{block}
       
   411     {Console Input}
       
   412     \texttt{raw\_input()} waits for user input.\\Prompt string is optional.\\
       
   413     All keystrokes are Strings!\\\texttt{int()} converts string to int.
       
   414   \end{block}
       
   415   \begin{block}
       
   416     {Console output}
       
   417     \texttt{print} is straight forward. Note the distinction between \texttt{print x} and \texttt{print x,}
       
   418   \end{block}
       
   419 \end{frame}
       
   420 
   254 \section{Control flow}
   421 \section{Control flow}
   255 \begin{frame}
   422 \begin{frame}
   256   \frametitle{Control flow constructs}  
   423   \frametitle{Control flow constructs}  
   257   \begin{itemize}
   424   \begin{itemize}
   258   \item \kwrd{if/elif/else}: branching
   425   \item \kwrd{if/elif/else}: branching
   277 else:
   444 else:
   278      print 'More'
   445      print 'More'
   279 \end{lstlisting}
   446 \end{lstlisting}
   280 \end{frame}
   447 \end{frame}
   281 
   448 
   282 \begin{frame}{Simple IO}
       
   283   \begin{block}
       
   284     {Console Input}
       
   285     \texttt{raw\_input()} waits for user input.\\Prompt string is optional.\\
       
   286     All keystrokes are Strings!\\\texttt{int()} converts string to int.
       
   287   \end{block}
       
   288   \begin{block}
       
   289     {Console output}
       
   290     \texttt{print} is straight forward. Note the distinction between \texttt{print x} and \texttt{print x,}
       
   291   \end{block}
       
   292 \end{frame}
       
   293 
       
   294 \subsection{Basic Looping}
   449 \subsection{Basic Looping}
   295 \begin{frame}[fragile]
   450 \begin{frame}[fragile]
   296   \frametitle{\kwrd{while}}
   451   \frametitle{\typ{while}}
   297 Example: Fibonacci series
   452 Example: Fibonacci series
   298   \begin{lstlisting}
   453   \begin{lstlisting}
   299 # the sum of two elements
   454 # the sum of two elements
   300 # defines the next
   455 # defines the next
   301 a, b = 0, 1
   456 a, b = 0, 1
   305 \end{lstlisting}
   460 \end{lstlisting}
   306 \typ{1 1 2 3 5 8}\\  
   461 \typ{1 1 2 3 5 8}\\  
   307 \end{frame}
   462 \end{frame}
   308 
   463 
   309 \begin{frame}[fragile]
   464 \begin{frame}[fragile]
   310 \frametitle{\kwrd{range()}}
   465 \frametitle{\typ{range()}}
   311 \kwrd{range([start,] stop[, step])}\\
   466 \kwrd{range([start,] stop[, step])}\\
   312 \begin{itemize}
   467 \begin{itemize}
   313   \item \alert {range() returns a list of integers}
   468   \item \alert {range() returns a list of integers}
   314   \item \alert {The start and the step arguments are optional}  
   469   \item \alert {The start and the step arguments are optional}  
   315 \end{itemize}
   470 \end{itemize}
   316 \end{frame}
   471 \end{frame}
   317 
   472 
   318 \begin{frame}[fragile]
   473 \begin{frame}[fragile]
   319   \frametitle{\kwrd{for}}
   474   \frametitle{\typ{for} \ldots \typ{range()}}
   320 Example: print squares of first \typ{n} numbers
   475 Example: print squares of first \typ{n} numbers
   321   \begin{lstlisting}
   476   \begin{lstlisting}
   322 In []: for i in range(5):
   477 In []: for i in range(5):
   323  ....:     print i, i * i
   478  ....:     print i, i * i
   324  ....:     
   479  ....:     
   387 \inctime{5}
   542 \inctime{5}
   388 \end{frame}
   543 \end{frame}
   389 
   544 
   390 \section{Dictionaries}
   545 \section{Dictionaries}
   391 \begin{frame}[fragile]
   546 \begin{frame}[fragile]
   392 \frametitle{Dictionaries}
   547 \frametitle{Dictionary: Recall}
   393   \alert {lists and tuples: integer indexes :: dictionaries: string indexes}
   548   \alert {lists and tuples: integer indexes :: dictionaries: string indexes}
   394 \begin{lstlisting}
   549 \begin{lstlisting}
   395 In []: player = {'Mat': 134,'Inn': 233, 
   550 In []: player = {'Mat': 134,'Inn': 233, 
   396            'Runs': 10823, 'Avg': 52.53}
   551            'Runs': 10823, 'Avg': 52.53}
   397 
   552 
   398 In []: player['Avg']
   553 In []: player['Avg']
   399 Out[]: 52.530000000000001
   554 Out[]: 52.530000000000001
   400 In []: player.keys()
   555 \end{lstlisting}
   401 Out[]: ['Runs', 'Inn', 'Avg', 'Mat']
       
   402 In []: player.values()
       
   403 Out[]: [10823, 233, 
       
   404        52.530000000000001, 134]
       
   405 \end{lstlisting}
       
   406 \end{frame}
       
   407 
       
   408 \begin{frame}{Dictionaries}
       
   409 \begin{itemize}
   556 \begin{itemize}
   410 \item Duplicate keys are not allowed!
   557 \item Duplicate keys are not allowed!
   411 \item Dictionaries are iterable through keys.
   558 \item Dictionaries are iterable through keys.
   412 \end{itemize}
   559 \end{itemize}
   413 \inctime{5}
       
   414 \end{frame}
   560 \end{frame}
   415 
   561 
   416 \end{document}
   562 \end{document}