day1/session2.tex
changeset 290 10528e3073ef
parent 286 ac457f7d1702
child 293 f7d7b5565232
equal deleted inserted replaced
286:ac457f7d1702 290:10528e3073ef
   121   \frametitle{Outline}
   121   \frametitle{Outline}
   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}
       
   127 %\frametitle{Python Scripts\ldots}
       
   128 %  \begin{itemize}
       
   129 %    \item Open a new file in an \alert{editor}
       
   130 %    \item Copy and paste from the output of \typ{\%hist -n}
       
   131 %    \item Save the file as \typ{sine_plot.py}
       
   132 %  \end{itemize}
       
   133 %  \begin{itemize}
       
   134 %  \item run the file in IPython using \typ{\%run -i sine_plot.py}\\
       
   135 %  \end{itemize}
       
   136 %\end{frame}
       
   137 
   126 \section{Scripts}
   138 \section{Scripts}
   127 
   139 
   128 \begin{frame}[fragile]
   140 \begin{frame}[fragile]
   129 \frametitle{Python Scripts}
   141 \frametitle{Python Scripts}
   130   \begin{itemize}
   142   \begin{itemize}
   195 \end{itemize}
   207 \end{itemize}
   196 \end{frame}
   208 \end{frame}
   197 
   209 
   198 \section{Lists}
   210 \section{Lists}
   199 \begin{frame}[fragile]
   211 \begin{frame}[fragile]
   200   \frametitle{How to create the data?}
   212   \frametitle{Lists: Introduction}
   201 What were \typ{x} and \typ{y}?\\
   213   \begin{lstlisting}
       
   214     In []: x = [0, 1, 2, 3]
       
   215 
       
   216     In []: y = [7, 11, 15, 19]
       
   217 
       
   218   \end{lstlisting}
       
   219 What are \typ{x} and \typ{y}?\\
   202 \begin{center}
   220 \begin{center}
   203 \alert{\typ{lists!!}}
   221 \alert{\typ{lists!!}}
   204 \end{center}
   222 \end{center}
       
   223 \end{frame}
       
   224 
       
   225 \begin{frame}[fragile]
       
   226 \frametitle{Lists: Initializing \& accessing elements}
   205 \begin{lstlisting}
   227 \begin{lstlisting}
   206 In []: mtlist = [] #Empty List
   228 In []: mtlist = [] #Empty List
   207 
   229 
   208 In []: lst = [ 1, 2, 3, 4, 5] 
   230 In []: a = [ 1, 2, 3, 4, 5] 
   209 \end{lstlisting}
   231 
   210 \end{frame}
   232 In []: a[0]+a[1]+a[-1]
   211 
       
   212 \begin{frame}[fragile]
       
   213 \frametitle{Accessing elements of a list}
       
   214 \begin{lstlisting}
       
   215 In []: lst[0]+lst[1]+lst[-1]
       
   216 Out[]: 8
   233 Out[]: 8
   217 \end{lstlisting}
   234 \end{lstlisting}
   218 \end{frame}
   235 \end{frame}
   219 
   236 
   220 \begin{frame}[fragile]
   237 \begin{frame}[fragile]
   221   \frametitle{List: Slicing}
   238   \frametitle{List: Slicing}
   222   \begin{block}{Remember\ldots}
   239   \begin{block}{Remember\ldots}
   223 	\kwrd{In []: lst = [ 1, 2, 3, 4, 5]}
   240 	\kwrd{In []: a = [ 1, 2, 3, 4, 5]}
   224   \end{block}
   241   \end{block}
   225 \begin{lstlisting}
   242 \begin{lstlisting}
   226 In []: lst[1:3]  # A slice.
   243 In []: a[1:3]  # A slice.
   227 Out[]: [2, 3]
   244 Out[]: [2, 3]
   228 
   245 
   229 In []: lst[1:-1]
   246 In []: a[1:-1]
   230 Out[]: [2, 3, 4]
   247 Out[]: [2, 3, 4]
   231 \end{lstlisting}
   248 \end{lstlisting}
   232 \alert{\typ{list[initial:final]}}
   249 \alert{\typ{list[initial:final]}}
   233 \end{frame}
   250 \end{frame}
   234 
   251 
   235 %% more on list slicing
   252 %% more on list slicing
   236 \begin{frame}[fragile]
   253 \begin{frame}[fragile]
   237 \frametitle{List operations}
   254 \frametitle{List operations}
   238 \begin{lstlisting}
   255 \begin{lstlisting}
   239 In []: a = [ 6, 7, 8, 9]
   256 In []: b = [ 6, 7, 8, 9]
   240 In []: b = lst + a
   257 In []: c = a + b
   241 
   258 
   242 In []: b
   259 In []: c
   243 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
   260 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
   244 
   261 
   245 In []: lst.append(6)
   262 In []: a.append(6)
   246 In []: lst
   263 In []: a
   247 Out[]: [ 1, 2, 3, 4, 5, 6]
   264 Out[]: [ 1, 2, 3, 4, 5, 6]
   248 \end{lstlisting}
   265 \end{lstlisting}
   249 %\inctime{10}
   266 %\inctime{10}
   250 \end{frame}
   267 \end{frame}
   251 
   268 
   252 \section{Simple Pendulum}
   269 \section{Simple Pendulum}
   253 \begin{frame}[fragile]
   270 \begin{frame}[fragile]
   254 \frametitle{Simple Pendulum - L and T}
   271 \frametitle{Simple Pendulum - L and T}
   255 Let us look at the example of the Simple Pendulum experiment.
   272 Let us look at the Simple Pendulum experiment.
   256 \begin{center}
   273 \begin{center}
   257 \begin{small}
   274 \begin{small}
   258 \begin{tabular}{| c | c | c |}
   275 \begin{tabular}{| c | c | c |}
   259 \hline
   276 \hline
   260 $L$ & $T$ & $T^2$ \\ \hline
   277 $L$ & $T$ & $T^2$ \\ \hline
   274 \end{frame}
   291 \end{frame}
   275 
   292 
   276 \begin{frame}[fragile]
   293 \begin{frame}[fragile]
   277 \frametitle{Lets use lists}
   294 \frametitle{Lets use lists}
   278 \begin{lstlisting}
   295 \begin{lstlisting}
   279 In []: L = [0.1, 0.2, 0.3, 0.4, 0.5, 
   296 In []: l = [0.1, 0.2, 0.3, 0.4, 0.5, 
   280             0.6, 0.7, 0.8, 0.9]
   297             0.6, 0.7, 0.8, 0.9]
   281 
   298 
   282 In []: T = [0.69, 0.8989, 1.1867, 
   299 In []: t = [0.69, 0.8989, 1.1867, 
   283             1.2991, 1.4656, 1.5843, 
   300             1.2991, 1.4656, 1.5843, 
   284             1.7706, 1.8296, 1.9440]
   301             1.7706, 1.8296, 1.9440]
   285 \end{lstlisting}
   302 \end{lstlisting}
   286 \end{frame}
   303 \end{frame}
   287 
   304 
   288 \begin{frame}[fragile]
   305 \begin{frame}[fragile]
   289 \frametitle{Plotting $L$ vs $T^2$}
   306 \frametitle{Plotting $L$ vs $T^2$}
   290 \begin{itemize}
   307 \begin{itemize}
   291 \item We must square each of the values in T
   308 \item We must square each of the values in \typ{t}
   292 \item How to do it?
   309 \item How to do it?
   293 \item We use a \kwrd{for} loop to iterate over T
   310 \item We use a \kwrd{for} loop to iterate over \typ{t}
   294 \end{itemize}
   311 \end{itemize}
   295 \end{frame}
   312 \end{frame}
   296 
   313 
   297 \begin{frame}[fragile]
   314 \begin{frame}[fragile]
   298 \frametitle{Plotting $L$ vs $T^2$}
   315 \frametitle{Plotting $L$ vs $T^2$}
   299 \begin{lstlisting}
   316 \begin{lstlisting}
   300 In []: TSq = []
   317 In []: tsq = []
   301 
   318 
   302 In []: for t in T:
   319 In []: for time in t:
   303  ....:     TSq.append(t*t)
   320  ....:     tsq.append(time*time)
   304 
   321 
   305 In []: plot(L, TSq)
   322 In []: plot(l, tsq)
   306 Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>]
   323 Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>]
   307 \end{lstlisting}
   324 \end{lstlisting}
   308 This gives \kwrd{TSq} which is the list of squares of T values.
   325 This gives \kwrd{tsq} which is the list of squares of \typ{t} values.
       
   326 \end{frame}
       
   327 
       
   328 \begin{frame}[fragile]
       
   329   \frametitle{How to come out of the \textt{for} loop?}
       
   330   Hit the ``enter'' key twice to come to the previous indentation level
       
   331   \begin{lstlisting}
       
   332     In []: for time in t:
       
   333      ....:     tsq.append(time*time)
       
   334      ....:     
       
   335      ....:     
       
   336 
       
   337     In []:
       
   338   \end{lstlisting}
   309 \end{frame}
   339 \end{frame}
   310 
   340 
   311 \begin{frame}[fragile]
   341 \begin{frame}[fragile]
   312 \begin{figure}
   342 \begin{figure}
   313 \includegraphics[width=3.5in]{data/L-TSq-limited.png}
   343 \includegraphics[width=3.5in]{data/L-TSq-limited.png}
   326 1.3000e-01 7.5360e-01
   356 1.3000e-01 7.5360e-01
   327 1.4000e-01 8.3568e-01
   357 1.4000e-01 8.3568e-01
   328 1.5000e-01 8.6789e-01
   358 1.5000e-01 8.6789e-01
   329 \end{lstlisting}
   359 \end{lstlisting}
   330 \ldots
   360 \ldots
       
   361 \begin{block}{Windows users:}
       
   362   >type pendulum.txt
       
   363 \end{block}
   331 \end{frame}
   364 \end{frame}
   332 
   365 
   333 \begin{frame}[fragile]
   366 \begin{frame}[fragile]
   334 \frametitle{Reading \typ{pendulum.txt}}
   367 \frametitle{Reading \typ{pendulum.txt}}
   335 \begin{itemize}
   368 \begin{itemize}
   338   \item L - Column1; T - Column2
   371   \item L - Column1; T - Column2
   339 \end{itemize}
   372 \end{itemize}
   340 \end{frame}
   373 \end{frame}
   341 
   374 
   342 \begin{frame}[fragile]
   375 \begin{frame}[fragile]
   343 \frametitle{Reading \typ{pendulum.txt}}
       
   344 \begin{lstlisting}
       
   345 In []: L = []
       
   346 In []: T = []
       
   347 In []: for line in open('pendulum.txt'):
       
   348   ....     points = line.split()
       
   349   ....     L.append(float(points[0]))
       
   350   ....     T.append(float(points[1]))
       
   351 \end{lstlisting}
       
   352 \begin{itemize}
       
   353 \item We now have two lists L and T
       
   354 \item Now, repeat previous steps for plotting
       
   355 \end{itemize}
       
   356 \end{frame}
       
   357 
       
   358 \begin{frame}[fragile]
       
   359 \frametitle{Plotting from \typ{pendulum.txt}}
   376 \frametitle{Plotting from \typ{pendulum.txt}}
   360 \begin{lstlisting}
   377 Open a new script and type the following:
   361 In []: TSq = []
   378 \begin{lstlisting}
   362 
   379 l = []
   363 In []: for t in T:
   380 t = []
   364  ....:     TSq.append(t*t)
   381 for line in open('pendulum.txt'):
   365 
   382     points = line.split()
   366 In []: plot(L, TSq, '.')
   383     l.append(float(points[0]))
   367 \end{lstlisting}
   384     t.append(float(points[1]))
       
   385 tsq = []
       
   386 for time in t:
       
   387     tsq.append(time*time)
       
   388 plot(l, tsq, '.')
       
   389 \end{lstlisting}
       
   390 \end{frame}
       
   391 
       
   392 \begin{frame}
       
   393 \frametitle{Save and run}
       
   394 \begin{itemize}
       
   395   \item Save as pendulum\_plot.py.
       
   396   \item Run using \kwrd{\%run -i pendulum\_plot.py}
       
   397 \end{itemize}
   368 \end{frame}
   398 \end{frame}
   369 
   399 
   370 \begin{frame}[fragile]
   400 \begin{frame}[fragile]
   371 \begin{figure}
   401 \begin{figure}
   372 \includegraphics[width=3.5in]{data/L-Tsq.png}
   402 \includegraphics[width=3.5in]{data/L-Tsq.png}
   373 \end{figure}
   403 \end{figure}
   374 \end{frame}
   404 \end{frame}
   375 
   405 
   376 \begin{frame}[fragile]
   406 \begin{frame}[fragile]
   377   \frametitle{Reading files \ldots}
   407   \frametitle{Reading files \ldots}
   378 \typ{In []: for line in open('pendulum.txt'):}
   408 \typ{for line in open('pendulum.txt'):}
   379 \begin{itemize}
   409 \begin{itemize}
   380 \item opening file `\typ{pendulum.txt}'
   410 \item opening file `\typ{pendulum.txt}'
   381 \item reading the file line by line
   411 \item reading the file line by line
   382 \item \typ{line} is a \kwrd{string}
   412 \item \typ{line} is a \kwrd{string}
   383 \end{itemize}
   413 \end{itemize}
   427   \end{lstlisting}
   457   \end{lstlisting}
   428 \end{frame}
   458 \end{frame}
   429 
   459 
   430 \begin{frame}[fragile]
   460 \begin{frame}[fragile]
   431 \frametitle{Let's review the code}
   461 \frametitle{Let's review the code}
   432 \begin{small}
   462 \begin{lstlisting}
   433 \begin{lstlisting}
   463 l = []
   434 In []: L = []
   464 t = []
   435 In []: T = []
   465 for line in open('pendulum.txt'):
   436 In []: for line in open('pendulum.txt'):
   466     points = line.split()
   437   ....     points = line.split()
   467     l.append(float(points[0]))
   438   ....     L.append(float(points[0]))
   468     t.append(float(points[1]))
   439   ....     T.append(float(points[1]))
   469 tsq = []
   440 
   470 for time in t:
   441 In []: TSq = []
   471     tsq.append(time*time)
   442 
   472 plot(l, tsq, '.')
   443 In []: for t in T:
   473 \end{lstlisting}
   444  ....:     TSq.append(t*t)
       
   445 
       
   446 In []: plot(L, TSq, '.')
       
   447 \end{lstlisting}
       
   448 \end{small}
       
   449 \end{frame}
   474 \end{frame}
   450 
   475 
   451 \begin{frame}[fragile]
   476 \begin{frame}[fragile]
   452 \begin{figure}
   477 \begin{figure}
   453 \includegraphics[width=3.5in]{data/L-Tsq.png}
   478 \includegraphics[width=3.5in]{data/L-Tsq.png}