281 5 25 |
281 5 25 |
282 6 36 |
282 6 36 |
283 \end{lstlisting} |
283 \end{lstlisting} |
284 \end{frame} |
284 \end{frame} |
285 |
285 |
286 \section{File Handling} |
286 \begin{frame}[fragile] |
287 \begin{frame}[fragile] |
287 \frametitle{Reading pendulum.txt} |
288 \frametitle{File and \kwrd{for}} |
288 \begin{itemize} |
289 \begin{lstlisting} |
289 \item We now wish to repeat the plot using the values from a file |
290 In []: f = open('dummyfile') |
290 \item Given a file containing L vs. T values |
291 |
291 \item Column1 - L; Column2 - T |
292 In []: for line in f: |
292 \item Read the file |
293 ...: print line |
293 \item Plot points for L vs. $T^2$ |
294 ...: |
294 \end{itemize} |
295 \end{lstlisting} |
295 \end{frame} |
296 \inctime{5} |
296 |
297 \end{frame} |
297 \begin{frame}[fragile] |
298 |
298 \frametitle{Reading pendulum.txt} |
299 \section{Strings} |
299 \begin{lstlisting} |
300 \begin{frame}{Strings} |
300 In []: L = [] |
|
301 In []: T = [] |
|
302 In []: for line in open('pendulum.txt'): |
|
303 .... points = line.split() |
|
304 .... L.append(float(points[0])) |
|
305 .... T.append(float(points[1])) |
|
306 \end{lstlisting} |
|
307 \begin{itemize} |
|
308 \item We now have two lists L and T |
|
309 \item Now, Repeat previous steps for plotting |
|
310 \end{itemize} |
|
311 \end{frame} |
|
312 |
|
313 \begin{frame}[fragile] |
|
314 \frametitle{Plotting from pendulum.txt} |
|
315 \begin{lstlisting} |
|
316 In []: TSq = [] |
|
317 |
|
318 In []: for t in T: |
|
319 ....: TSq.append(t*t) |
|
320 |
|
321 In []: plot(L, TSq, '.') |
|
322 \end{lstlisting} |
|
323 \end{frame} |
|
324 |
|
325 \begin{frame}{New Concepts} |
|
326 \begin{itemize} |
|
327 \item File handling |
|
328 \item Strings |
|
329 \item Data-type conversion |
|
330 \end{itemize} |
|
331 \end{frame} |
|
332 |
|
333 \begin{frame}[fragile] |
|
334 \frametitle{Reading files \ldots} |
|
335 \typ{In []: for line in open('pendulum.txt'):} |
|
336 \begin{itemize} |
|
337 \item opening file `pendulum.txt' |
|
338 \item iterating through file using variable \typ{line} |
|
339 \item \typ{line} is a \kwrd{string} variable |
|
340 \end{itemize} |
|
341 \end{frame} |
|
342 |
|
343 \begin{frame}[fragile] |
|
344 \frametitle{Strings} |
301 Anything within ``quotes'' is a string! |
345 Anything within ``quotes'' is a string! |
302 \end{frame} |
346 \begin{lstlisting} |
303 |
347 ' This is a string ' |
304 \begin{frame}[fragile]\frametitle{Strings and \typ{split()}} |
348 " This too! " |
|
349 """ This one too! """ |
|
350 ''' And one more! ''' |
|
351 \end{lstlisting} |
|
352 \end{frame} |
|
353 |
|
354 \begin{frame}[fragile] |
|
355 \frametitle{Strings and \typ{split()}} |
305 \begin{lstlisting} |
356 \begin{lstlisting} |
306 In []: a = 'hello world' |
357 In []: line = 'hello world' |
307 |
358 |
308 In []: a.split() |
359 In []: a.split() |
309 Out[]: ['hello', 'world'] |
360 Out[]: ['hello', 'world'] |
310 \end{lstlisting} |
361 \end{lstlisting} |
311 Now try this: |
362 This is what happens with \typ{line} |
312 \begin{lstlisting} |
363 \begin{lstlisting} |
313 In []: b = 'KD, MCS, PC, SC, SV' |
364 In []: line = '1.2000e-01 7.4252e-01' |
314 |
365 |
315 In []: b.split(',') |
366 In []: line.split() |
316 Out[]: ['KD', 'MCS', 'PC', 'SC', 'SV'] |
367 Out[]: ['1.2000e-01', '7.4252e-01'] |
317 \end{lstlisting} |
368 \end{lstlisting} |
318 \inctime{5} |
369 \end{frame} |
319 \end{frame} |
370 |
|
371 \begin{frame}[fragile] |
|
372 \frametitle{Getting floats from strings} |
|
373 \begin{lstlisting} |
|
374 In []: type(points[0]) |
|
375 Out[]: <type 'str'> |
|
376 \end{lstlisting} |
|
377 But, we need floating point numbers |
|
378 \begin{lstlisting} |
|
379 In []: t = float(point[0]) |
|
380 |
|
381 In []: type(t) |
|
382 Out[]: <type 'float'> |
|
383 \end{lstlisting} |
|
384 \end{frame} |
|
385 |
|
386 \begin{frame}[fragile] |
|
387 \begin{figure} |
|
388 \includegraphics[width=3.5in]{data/L-Tsq.png} |
|
389 \end{figure} |
|
390 \vspace{-0.2in} |
|
391 Coming up - \alert{Least Square Fit \ldots} |
|
392 \end{frame} |
|
393 |
|
394 |
|
395 |
320 |
396 |
321 \end{document} |
397 \end{document} |
|
398 |
|
399 |
|
400 |
|
401 |