day1/session2.tex
changeset 274 34f71bdd0263
parent 253 e446ed7287d7
child 275 71e50184d482
equal deleted inserted replaced
259:bb77a470e00a 274:34f71bdd0263
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     2 %Tutorial slides on Python.
     2 %Tutorial slides on Python.
     3 %
     3 %
     4 % Author: FOSSEE
     4 % Author: FOSSEE
     5 % Copyright (c) 2009, FOSSEE, IIT Bombay
     5 % Copyright (c) 2009, FOSSEE, IIT Bombay
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   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 \begin{frame}
   126 \begin{frame}
   127 \frametitle{Why we didn't close the IPython??}
   127 \frametitle{Why we didn't close IPython?}
   128 \begin{itemize}
   128   IPython provides a convenient feature to go back, edit, and re-run commands.\\
   129   \item IPython provides a convenient feature
   129   \alert{But when you close, all this is lost.}
   130   \item To go back, edit, and re-run commands
       
   131   \item But when you close, this is lost
       
   132 \end{itemize}
       
   133 \end{frame}
   130 \end{frame}
   134 
   131 
   135 \begin{frame}
   132 \begin{frame}
   136 \frametitle{But its impractical..}
   133 \frametitle{But its impractical..}
   137 \begin{itemize}
   134 \begin{itemize}
   146 
   143 
   147 \section{Scripts}
   144 \section{Scripts}
   148 \begin{frame}[fragile]
   145 \begin{frame}[fragile]
   149 \frametitle{Python Scripts}
   146 \frametitle{Python Scripts}
   150 \begin{itemize}
   147 \begin{itemize}
   151 \item Put all commands used in review problem into a file. 
   148 \item Put commands used in review problem into file. 
   152 \item use hist command of IPython.
   149 \item use hist command of IPython.
   153 \end{itemize}
   150 \end{itemize}
   154 \begin{lstlisting}
   151 \begin{lstlisting}
   155   In []: %hist
   152   In []: %hist
   156   In []: %hist -n
   153   In []: %hist -n
   159 
   156 
   160 \begin{frame}
   157 \begin{frame}
   161 \frametitle{Python Scripts\ldots}
   158 \frametitle{Python Scripts\ldots}
   162   \begin{itemize}
   159   \begin{itemize}
   163     \item Open a new file in an \alert{editor}
   160     \item Open a new file in an \alert{editor}
   164     \item Copy and paste required lines from the output of \typ{\%hist -n}
   161     \item Copy and paste from the output of \typ{\%hist -n}
   165     \item Save the file as \typ{sine_plot.py}
   162     \item Save the file as \typ{sine_plot.py}
   166   \end{itemize}
   163   \end{itemize}
   167   \begin{itemize}
   164   \begin{itemize}
   168   \item run the file in IPython using \typ{\%run sine_plot.py}\\
   165   \item run the file in IPython using \typ{\%run -i sine_plot.py}\\
   169   \end{itemize}
   166   \end{itemize}
   170 \end{frame}
   167 \end{frame}
   171 
   168 
   172 \begin{frame}[fragile]
   169 \begin{frame}[fragile]
   173 \frametitle{Why would I plot f(x)?}
   170 \frametitle{Why would I plot f(x)?}
   174 How often do we plot analytical functions?\\We plot experimental data more.
   171 How often do we plot analytical functions?\\We plot experimental data more.
       
   172 \begin{small}
   175 \begin{lstlisting}
   173 \begin{lstlisting}
   176 In []: x = [0, 1, 2, 3]
   174 In []: x = [0, 1, 2, 3]
   177 
   175 
   178 In []: y = [7, 11, 15, 19]
   176 In []: y = [7, 11, 15, 19]
   179 
   177 
   180 In []: plot(x, y)
   178 In []: plot(x, y)
   181 Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>]
   179 Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>]
   182 \end{lstlisting}
   180 
       
   181 In []: xlabel('X')
       
   182 Out[]: <matplotlib.text.Text object at 0x986e9ac>
       
   183 
       
   184 In []: ylabel('Y')
       
   185 Out[]: <matplotlib.text.Text object at 0x98746ec>
       
   186 \end{lstlisting}
       
   187 \end{small}
   183 \end{frame}
   188 \end{frame}
   184 
   189 
   185 \begin{frame}[fragile]
   190 \begin{frame}[fragile]
   186 \begin{figure}
   191 \begin{figure}
   187 \includegraphics[width=3.5in]{data/straightline.png}
   192 \includegraphics[width=3.5in]{data/straightline.png}
   188 \end{figure}
   193 \end{figure}
   189 \alert{Is this what you have??}
   194 \alert{Is this what you have?}
   190 \end{frame}
   195 \end{frame}
   191 
   196 
   192 \begin{frame}[fragile]
   197 \begin{frame}[fragile]
   193 \frametitle{Plotting points}
   198 \frametitle{Plotting points}
   194 \begin{itemize}
   199 \begin{itemize}
   195 \item What if we want to plot the points!
   200 \item What if we want to plot the points!
   196 \end{itemize}
   201 \end{itemize}
   197 \begin{lstlisting}
   202 \begin{lstlisting}
   198   In []: clf()
   203   In []: clf()
   199 
   204 
   200   In []: plot(L, TSq, 'o')
   205   In []: plot(x, y, 'o')
   201   Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
   206   Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
   202 
   207 
   203   In []: clf()
   208   In []: clf()
   204   In []: plot(L, TSq, '.')
   209   In []: plot(x, y, '.')
   205   Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
   210   Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
   206 \end{lstlisting}
   211 \end{lstlisting}
   207 \end{frame}
   212 \end{frame}
   208 
   213 
   209 \begin{frame}[fragile]
   214 \begin{frame}[fragile]
   214 \end{frame}
   219 \end{frame}
   215 
   220 
   216 \begin{frame}[fragile]
   221 \begin{frame}[fragile]
   217 \frametitle{Additional Plotting Attributes}
   222 \frametitle{Additional Plotting Attributes}
   218 \begin{itemize}
   223 \begin{itemize}
   219   \item \kwrd{'o'} - Dots
   224   \item \kwrd{'o'} - Filled circles
   220   \item \kwrd{'.'} - Smaller Dots
   225   \item \kwrd{'.'} - Small Dots
   221   \item \kwrd{'-'} - Lines
   226   \item \kwrd{'-'} - Lines
   222   \item \kwrd{'- -'} - Dashed lines
   227   \item \kwrd{'- -'} - Dashed lines
   223 \end{itemize}
   228 \end{itemize}
   224 \end{frame}
   229 \end{frame}
   225 
   230 
   226 \section{Lists}
   231 \section{Lists}
   227 \begin{frame}[fragile]
   232 \begin{frame}[fragile]
   228   \frametitle{How to create the data?}
   233   \frametitle{How to create the data?}
   229 What were \typ{x} and \typ{y}??\\
   234 What were \typ{x} and \typ{y}?\\
   230 \begin{center}
   235 \begin{center}
   231 \alert{\typ{lists!!}}
   236 \alert{\typ{lists!!}}
   232 \end{center}
   237 \end{center}
   233 \begin{lstlisting}
   238 \begin{lstlisting}
   234 In []: mtlist = [] #Empty List
   239 In []: mtlist = [] #Empty List
   235 
   240 
   236 In []: lst = [1,2,3,4,5] 
   241 In []: lst = [ 1, 2, 3, 4, 5] 
   237 \end{lstlisting}
   242 \end{lstlisting}
   238 \end{frame}
   243 \end{frame}
   239 
   244 
   240 \begin{frame}[fragile]
   245 \begin{frame}[fragile]
   241 \frametitle{Accessing elements of a list}
   246 \frametitle{Accessing elements of a list}
   246 \end{frame}
   251 \end{frame}
   247 
   252 
   248 \begin{frame}[fragile]
   253 \begin{frame}[fragile]
   249   \frametitle{List: Slicing}
   254   \frametitle{List: Slicing}
   250   \begin{block}{Remember\ldots}
   255   \begin{block}{Remember\ldots}
   251 	\kwrd{In []: lst = [1,2,3,4,5]}
   256 	\kwrd{In []: lst = [ 1, 2, 3, 4, 5]}
   252   \end{block}
   257   \end{block}
   253 \alert{\typ{list[initial:final:step]}}
       
   254 \begin{lstlisting}
   258 \begin{lstlisting}
   255 In []: lst[1:3]  # A slice.
   259 In []: lst[1:3]  # A slice.
   256 Out[]: [2, 3]
   260 Out[]: [2, 3]
   257 
   261 
   258 In []: lst[1:-1]
   262 In []: lst[1:-1]
   259 Out[]: [2, 3]
   263 Out[]: [2, 3]
   260 \end{lstlisting}
   264 \end{lstlisting}
       
   265 \alert{\typ{list[initial:final]}}
   261 \end{frame}
   266 \end{frame}
   262 
   267 
   263 \begin{frame}[fragile]
   268 \begin{frame}[fragile]
   264 \frametitle{List operations}
   269 \frametitle{List operations}
   265 \begin{lstlisting}
   270 \begin{lstlisting}
   266 In []: anthrlst = [6,7,8,9]
   271 In []: anthrlst = [ 6, 7, 8, 9]
   267 In []: lnglst = lst + anthrlst
   272 In []: lnglst = lst + anthrlst
   268 
   273 
   269 In []: lnglst
   274 In []: lnglst
   270 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
   275 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
   271 
   276 
   272 In []: lst.append(6)
   277 In []: lst.append(6)
   273 In []: lst
   278 In []: lst
   274 Out[]: [1, 2, 3, 4, 5, 6]
   279 Out[]: [ 1, 2, 3, 4, 5, 6]
   275 \end{lstlisting}
   280 \end{lstlisting}
   276 %\inctime{10}
   281 %\inctime{10}
   277 \end{frame}
   282 \end{frame}
   278 
   283 
   279 \section{Simple Pendulum}
   284 \section{Simple Pendulum}
   330  ....:     TSq.append(t*t)
   335  ....:     TSq.append(t*t)
   331 
   336 
   332 In []: plot(L, TSq)
   337 In []: plot(L, TSq)
   333 Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>]
   338 Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>]
   334 \end{lstlisting}
   339 \end{lstlisting}
   335 This gives the list \kwrd{TSq} which is the list of squares of T values.
   340 This gives \kwrd{TSq} which is the list of squares of T values.
   336 \end{frame}
   341 \end{frame}
   337 
   342 
   338 \begin{frame}[fragile]
   343 \begin{frame}[fragile]
   339 \begin{figure}
   344 \begin{figure}
   340 \includegraphics[width=3.5in]{data/L-TSq-limited.png}
   345 \includegraphics[width=3.5in]{data/L-TSq-limited.png}
   341 \end{figure}
   346 \end{figure}
   342 \end{frame}
   347 \end{frame}
   343 
   348 
   344 \begin{frame}[fragile]
   349 \begin{frame}[fragile]
   345 \frametitle{More of \texttt{for}}
   350 \frametitle{What about larger data sets?}
   346 \begin{itemize}
       
   347 \item Used to iterate over lists
       
   348 \item Let us look at another example.
       
   349 \end{itemize}
       
   350 \begin{lstlisting}
       
   351 In []: lst = [1,2,3,4,5,6]
       
   352 In []: for num in lst:
       
   353  ....:     print num, num*num
       
   354  ....:    
       
   355 1 1
       
   356 2 4
       
   357 3 9
       
   358 4 16
       
   359 5 25
       
   360 6 36
       
   361 \end{lstlisting}
       
   362 \end{frame}
       
   363 
       
   364 \begin{frame}[fragile]
       
   365 \frametitle{What about larger data sets??}
       
   366 \alert{Data is usually present in a file!} \\
   351 \alert{Data is usually present in a file!} \\
   367 Lets look at the pendulum.txt file.
   352 Lets look at the \typ{pendulum.txt} file.
   368 \begin{lstlisting}
   353 \begin{lstlisting}
   369 $cat data/pendulum.txt 
   354 $ cat pendulum.txt 
   370 1.0000e-01 6.9004e-01
   355 1.0000e-01 6.9004e-01
   371 1.1000e-01 6.9497e-01
   356 1.1000e-01 6.9497e-01
   372 1.2000e-01 7.4252e-01
   357 1.2000e-01 7.4252e-01
   373 1.3000e-01 7.5360e-01
   358 1.3000e-01 7.5360e-01
   374 1.4000e-01 8.3568e-01
   359 1.4000e-01 8.3568e-01
   376 \end{lstlisting}
   361 \end{lstlisting}
   377 \ldots
   362 \ldots
   378 \end{frame}
   363 \end{frame}
   379 
   364 
   380 \begin{frame}[fragile]
   365 \begin{frame}[fragile]
   381 \frametitle{Reading pendulum.txt}
   366 \frametitle{Reading \typ{pendulum.txt}}
   382 \begin{itemize}
   367 \begin{itemize}
   383   \item We now wish to repeat the plot using the values from a file
   368   \item Let us generate a plot from the data file
   384   \item Given a file containing L vs. T values 
   369   \item File contains L vs. T values 
   385   \item Column1 - L; Column2 - T  
   370   \item L - Column1; T - Column2
   386   \item Read the file
   371 \end{itemize}
   387   \item Plot points for L vs. $T^2$ 
   372 \end{frame}
   388 \end{itemize}
   373 
   389 \end{frame}
   374 \begin{frame}[fragile]
   390 
   375 \frametitle{Reading \typ{pendulum.txt}}
   391 \begin{frame}[fragile]
       
   392 \frametitle{Reading pendulum.txt}
       
   393 \begin{lstlisting}
   376 \begin{lstlisting}
   394 In []: L = []
   377 In []: L = []
   395 In []: T = []
   378 In []: T = []
   396 In []: for line in open('pendulum.txt'):
   379 In []: for line in open('pendulum.txt'):
   397   ....     points = line.split()
   380   ....     points = line.split()
   398   ....     L.append(float(points[0]))
   381   ....     L.append(float(points[0]))
   399   ....     T.append(float(points[1]))
   382   ....     T.append(float(points[1]))
   400 \end{lstlisting}
   383 \end{lstlisting}
   401 \begin{itemize}
   384 \begin{itemize}
   402 \item We now have two lists L and T
   385 \item We now have two lists L and T
   403 \item Now, Repeat previous steps for plotting
   386 \item Now, repeat previous steps for plotting
   404 \end{itemize}
   387 \end{itemize}
   405 \end{frame}
   388 \end{frame}
   406 
   389 
   407 \begin{frame}[fragile]
   390 \begin{frame}[fragile]
   408 \frametitle{Plotting from pendulum.txt}
   391 \frametitle{Plotting from \typ{pendulum.txt}}
   409 \begin{lstlisting}
   392 \begin{lstlisting}
   410 In []: TSq = []
   393 In []: TSq = []
   411 
   394 
   412 In []: for t in T:
   395 In []: for t in T:
   413  ....:     TSq.append(t*t)
   396  ....:     TSq.append(t*t)
   424 
   407 
   425 \begin{frame}[fragile]
   408 \begin{frame}[fragile]
   426   \frametitle{Reading files \ldots}
   409   \frametitle{Reading files \ldots}
   427 \typ{In []: for line in open('pendulum.txt'):}
   410 \typ{In []: for line in open('pendulum.txt'):}
   428 \begin{itemize}
   411 \begin{itemize}
   429 \item opening file `pendulum.txt'
   412 \item opening file `\typ{pendulum.txt}'
   430 \item iterating through the file by reading each line into variable \typ{line}
   413 \item reading the file line by line
   431 \item \typ{line} is a \kwrd{string} variable
   414 \item \typ{line} is a \kwrd{string}
   432 \end{itemize}
   415 \end{itemize}
   433 \end{frame}
   416 \end{frame}
   434 
   417 
   435 \section{Strings}
   418 \section{Strings}
   436 \begin{frame}[fragile]
   419 \begin{frame}[fragile]
   445 \end{frame}
   428 \end{frame}
   446 
   429 
   447 \begin{frame}[fragile]
   430 \begin{frame}[fragile]
   448 \frametitle{Strings and \typ{split()}}
   431 \frametitle{Strings and \typ{split()}}
   449   \begin{lstlisting}
   432   \begin{lstlisting}
   450 In []: line = 'hello world'
   433 In []: greet = 'hello world'
   451 
   434 
   452 In []: line.split()
   435 In []: greet.split()
   453 Out[]: ['hello', 'world']
   436 Out[]: ['hello', 'world']
   454   \end{lstlisting}
   437   \end{lstlisting}
   455 This is what happens with \typ{line}
   438 This is what happens with \typ{line}
   456   \begin{lstlisting}
   439   \begin{lstlisting}
   457 In []: line = '1.2000e-01 7.4252e-01'
   440 In []: line = '1.2000e-01 7.4252e-01'
   474 In []: type(t)
   457 In []: type(t)
   475 Out[]: <type 'float'>
   458 Out[]: <type 'float'>
   476   \end{lstlisting}
   459   \end{lstlisting}
   477 \end{frame}
   460 \end{frame}
   478 
   461 
       
   462 \begin{frame}[fragile]
       
   463 \frametitle{Let's review the code}
       
   464 \begin{small}
       
   465 \begin{lstlisting}
       
   466 In []: L = []
       
   467 In []: T = []
       
   468 In []: for line in open('pendulum.txt'):
       
   469   ....     points = line.split()
       
   470   ....     L.append(float(points[0]))
       
   471   ....     T.append(float(points[1]))
       
   472 
       
   473 In []: TSq = []
       
   474 
       
   475 In []: for t in T:
       
   476  ....:     TSq.append(t*t)
       
   477 
       
   478 In []: plot(L, TSq, '.')
       
   479 \end{lstlisting}
       
   480 \end{small}
       
   481 \end{frame}
       
   482 
       
   483 \begin{frame}[fragile]
       
   484 \begin{figure}
       
   485 \includegraphics[width=3.5in]{data/L-Tsq.png}
       
   486 \end{figure}
       
   487 \end{frame}
       
   488 
   479 \section {Summary}
   489 \section {Summary}
   480 \begin{frame}
   490 \begin{frame}[fragile]
   481 \frametitle{Summary}
   491 \frametitle{What did we learn?}
   482 So what did we learn in this session??
   492 \begin{itemize}
   483 \begin{itemize}
   493   \item \kwrd{\%hist -n}
   484   \item Creating and running Python scripts
   494   \item Python scripts
   485   \item Plotting points and Plotting attributes
   495   \item \kwrd{\%run -i}
       
   496   \item Plotting points
       
   497   \item Plot attributes
   486   \item Lists
   498   \item Lists
   487   \item \kwrd{for}
   499   \item \kwrd{for}
   488   \item Reading files
   500   \item Reading files
   489   \item Strings
   501   \item Strings
   490 \end{itemize}
   502 \end{itemize}