day1/session2.tex
changeset 179 eea01ca072ff
parent 163 f7aeeedb9aa3
child 183 b34c3a22d726
child 193 a31b0e76c0fb
equal deleted inserted replaced
178:8a3a9d98fa84 179:eea01ca072ff
   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{Why we didn't close the IPython??}
       
   128 \begin{itemize}
       
   129   \item Because all the command history is lost
       
   130   \item We can go back, edit, and re-execute our commands
       
   131 \end{itemize}
       
   132 \end{frame}
       
   133 
       
   134 \begin{frame}
       
   135 \frametitle{But its impractical..}
       
   136 \begin{itemize}
       
   137   \item Because we can't always keep running the IPython shell for days
       
   138   \item And lets admit it, its a pain to go back and edit
       
   139 \end{itemize}
       
   140 And the solution is..\\
       
   141 \begin{center}
       
   142 \alert {\typ{Scripts!!}}
       
   143 \end{center}
       
   144 \end{frame}
       
   145 
   126 \section{Creating and running scripts}
   146 \section{Creating and running scripts}
   127 \begin{frame}[fragile]
   147 \begin{frame}[fragile]
   128 \frametitle{Python Scripts}
   148 \frametitle{Python Scripts}
   129 \begin{itemize}
   149 \begin{itemize}
   130 \item Let us now put all the commands used in the review problem into a file. 
   150 \item Let us now put all the commands used in the review problem into a file. 
   139 \begin{frame}
   159 \begin{frame}
   140 \frametitle{Python Scripts\ldots}
   160 \frametitle{Python Scripts\ldots}
   141   \begin{itemize}
   161   \begin{itemize}
   142     \item Open a new file in an \alert{editor}
   162     \item Open a new file in an \alert{editor}
   143     \item Copy and paste required lines from the output of \typ{\%hist -n}
   163     \item Copy and paste required lines from the output of \typ{\%hist -n}
   144     \item Save the file as \typ{first_plot.py}
   164     \item Save the file as \typ{sine_plot.py}
   145   \end{itemize}
   165   \end{itemize}
   146   \begin{itemize}
   166   \begin{itemize}
   147   \item run the file in IPython using \typ{\%run first_plot.py}\\
   167   \item run the file in IPython using \typ{\%run sine_plot.py}\\
   148   \end{itemize}
   168   \end{itemize}
   149 \end{frame}
   169 \end{frame}
   150 
   170 
   151 \section{Plotting Points}
   171 \begin{frame}[fragile]
   152 \begin{frame}[fragile]
   172 \frametitle{How often do we plot analytical functions?}
   153 \frametitle{Simple Pendulum - L and T}
   173 Let us look at a small example:
   154   \begin{itemize}
   174 \begin{lstlisting}
   155     \item Given data of Length and Time-period of a Simple pendulum 
   175 In []: x = [0, 1, 2, 3]
   156     \item $T^2 = \frac{4\pi^2}{g}L$\\ \alert{{$L$} and {$T^2$} vary linearly}
   176 
   157     \item We wish to plot L vs. \alert{$T^2$}
   177 In []: y = [7, 11, 15, 19]
   158   \end{itemize}    
   178 
   159 \begin{lstlisting}
   179 In []: plot(x, y)
   160 In []: L = [0.1,  0.2,  0.3,  
   180 Out[]: [<matplotlib.lines.Line2D object at 0xa73aa8c>]
   161             0.4,  0.5,  0.6,  
       
   162             0.7,  0.8,  0.9]
       
   163 In []: T = [0.6529, 0.8485, 1.0590, 
       
   164             1.2390, 1.4124, 1.5061, 
       
   165             1.6441, 1.7949, 1.8758]
       
   166 \end{lstlisting}
       
   167 \end{frame}
       
   168 
       
   169 \begin{frame}[fragile]
       
   170 \frametitle{Plotting $L$ vs. $T^2$}
       
   171 \begin{itemize}
       
   172 \item We must square each of the values in T
       
   173 \item How to do it?
       
   174 \item T is a \kwrd{list} and we use a \kwrd{for} loop to iterate over it
       
   175 \end{itemize}
       
   176 \end{frame}
       
   177 
       
   178 \begin{frame}[fragile]
       
   179 \frametitle{Plotting $L$ vs $T^2$}
       
   180 \begin{lstlisting}
       
   181 In []: TSq = []
       
   182 
       
   183 In []: for t in T:
       
   184  ....:     TSq.append(t*t)
       
   185 
       
   186 In []: plot(L, TSq)
       
   187 Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>]
       
   188 \end{lstlisting}
   181 \end{lstlisting}
   189 \end{frame}
   182 \end{frame}
   190 
   183 
   191 \begin{frame}[fragile]
   184 \begin{frame}[fragile]
   192 \frametitle{Plotting points}
   185 \frametitle{Plotting points}
   193 \begin{itemize}
   186 \begin{itemize}
   194 \item But we want to plot points!
   187 \item What if we want to plot points!
   195 \end{itemize}
   188 \end{itemize}
   196 \begin{lstlisting}
   189 \begin{lstlisting}
   197   In []: clf()
   190   In []: clf()
   198 
   191 
   199   In []: plot(L, TSq, 'o')
   192   In []: plot(L, TSq, 'o')
   211   \item \kwrd{'o'} - Dots
   204   \item \kwrd{'o'} - Dots
   212   \item \kwrd{'.'} - Smaller Dots
   205   \item \kwrd{'.'} - Smaller Dots
   213   \item \kwrd{'-'} - Lines
   206   \item \kwrd{'-'} - Lines
   214   \item \kwrd{'- -'} - Dashed lines
   207   \item \kwrd{'- -'} - Dashed lines
   215 \end{itemize}
   208 \end{itemize}
       
   209 \end{frame}
       
   210 
       
   211 \section{Lists}
       
   212 \begin{frame}[fragile]
       
   213   \frametitle{How to create?}
       
   214 What are \typ{x} and \typ{y} here??\\
       
   215 \begin{center}
       
   216 \alert{\typ{lists!!}}
       
   217 \end{center}
       
   218 \begin{lstlisting}
       
   219 In []: mtlist = [] #Empty List
       
   220 
       
   221 In []: lst = [1,2,3,4,5] 
       
   222 \end{lstlisting}
       
   223 \end{frame}
       
   224 
       
   225 \begin{frame}[fragile]
       
   226 \frametitle{Accessing elements of a list}
       
   227 \begin{lstlisting}
       
   228 In []: lst[0]+lst[1]+lst[-1]
       
   229 Out[]: 7
       
   230 \end{lstlisting}
       
   231 \end{frame}
       
   232 
       
   233 \begin{frame}[fragile]
       
   234   \frametitle{List: Slicing}
       
   235 \alert{\typ{list[initial:final:step]}}
       
   236 \begin{lstlisting}
       
   237 In []: lst[1:3]  # A slice.
       
   238 Out[]: [2, 3]
       
   239 
       
   240 In []: lst[1:-1]
       
   241 Out[]: [2, 3]
       
   242 \end{lstlisting}
       
   243 \end{frame}
       
   244 
       
   245 \begin{frame}[fragile]
       
   246   \frametitle{List concatenation and list methods}
       
   247 \begin{lstlisting}
       
   248 In []: anthrlst = [6,7,8,9]
       
   249 In []: lnglst = lst + anthrlst
       
   250 
       
   251 In []: lnglst
       
   252 Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
       
   253 
       
   254 In []: lst.append(6)
       
   255 In []: lst
       
   256 Out[]: [1, 2, 3, 4, 5, 6]
       
   257 \end{lstlisting}
       
   258 %\inctime{10}
       
   259 \end{frame}
       
   260 
       
   261 \section{Simple Pendulum}
       
   262 \begin{frame}[fragile]
       
   263 \frametitle{Simple Pendulum - L and T}
       
   264 Let us look at a more realistic example of the Simple Pendulum experiment.
       
   265 \end{frame}
       
   266 
       
   267 \begin{frame}[fragile]
       
   268 \frametitle{Plotting $L$ vs. $T^2$}
       
   269 \begin{itemize}
       
   270 \item We must square each of the values in T
       
   271 \item How to do it?
       
   272 \item T is a \kwrd{list} and we use a \kwrd{for} loop to iterate over it
       
   273 \end{itemize}
       
   274 \end{frame}
       
   275 
       
   276 \begin{frame}[fragile]
       
   277 \frametitle{Plotting $L$ vs $T^2$}
       
   278 \begin{lstlisting}
       
   279 In []: TSq = []
       
   280 
       
   281 In []: for t in T:
       
   282  ....:     TSq.append(t*t)
       
   283 
       
   284 In []: plot(L, TSq)
       
   285 Out[]: [<matplotlib.lines.Line2D object at 0xa5b05ac>]
       
   286 \end{lstlisting}
   216 \end{frame}
   287 \end{frame}
   217 
   288 
   218 \begin{frame}{New Concepts}
   289 \begin{frame}{New Concepts}
   219   \begin{itemize}
   290   \begin{itemize}
   220     \item lists
   291     \item lists
   221     \item \typ{for}
   292     \item \typ{for}
   222   \end{itemize}
   293   \end{itemize}
   223 \end{frame}
       
   224 
       
   225 \section{Lists}
       
   226 \begin{frame}[fragile]
       
   227   \frametitle{How to create and use lists?}
       
   228 \begin{lstlisting}
       
   229 In []: mtlist = [] #Empty List
       
   230 
       
   231 In []: lst = [1,2,3,4] 
       
   232 
       
   233 In []: lst[0]+lst[1]+lst[-1]
       
   234 Out[]: 7
       
   235 \end{lstlisting}
       
   236 \end{frame}
       
   237 
       
   238 \begin{frame}[fragile]
       
   239   \frametitle{List: Slicing}
       
   240 list[initial:final:step]
       
   241 \begin{lstlisting}
       
   242 In []: lst[1:3]  # A slice.
       
   243 Out[]: [2, 3]
       
   244 
       
   245 In []: lst[1:-1]
       
   246 Out[]: [2, 3]
       
   247 \end{lstlisting}
       
   248 \end{frame}
       
   249 
       
   250 \begin{frame}[fragile]
       
   251   \frametitle{List methods}
       
   252 \begin{lstlisting}
       
   253 In []: lst.append(6)
       
   254 In []: lst
       
   255 Out[]: [1, 2, 3, 4, 5, 6]
       
   256 \end{lstlisting}
       
   257 %\inctime{10}
       
   258 \end{frame}
   294 \end{frame}
   259 
   295 
   260 \begin{frame}[fragile]
   296 \begin{frame}[fragile]
   261 \frametitle{\texttt{for}}
   297 \frametitle{\texttt{for}}
   262 Used to iterate over lists\\ Let us look at another example.
   298 Used to iterate over lists\\ Let us look at another example.