day1/session2.tex
changeset 132 c10d8cb8d690
parent 130 8905a5badd7e
child 134 31b3e5051b06
equal deleted inserted replaced
131:b3a78754c4a9 132:c10d8cb8d690
   162 
   162 
   163 \section{Creating and running scripts}
   163 \section{Creating and running scripts}
   164 \begin{frame}
   164 \begin{frame}
   165   {Creating python files}
   165   {Creating python files}
   166   \begin{itemize}
   166   \begin{itemize}
   167     \item aka scripts
       
   168     \item use your editor
   167     \item use your editor
   169     \item extension \typ{.py}
   168     \item extension \typ{.py}
   170     \item run with \typ{python hello.py} at the command line
       
   171     \item in IPython using \kwrd{\%run}
   169     \item in IPython using \kwrd{\%run}
   172   \end{itemize}
   170   \end{itemize}
   173 \inctime{5}
   171 \inctime{5}
   174 \end{frame}
   172 \end{frame}
   175 
   173 
   176 \section{Files Handling}
   174 \section{Files Handling}
   177 \begin{frame}[fragile]
       
   178   \frametitle{Basic File Operations}
       
   179 Opening and Reading files
       
   180 \begin{small}
       
   181 \begin{lstlisting}
       
   182 In []: f = open('hello.py')
       
   183 In []: data = f.read() # Read entire file.
       
   184 In []: line = f.readline() # Read 1 line.
       
   185 In []: f.close() # close the file.
       
   186 \end{lstlisting}
       
   187 \end{small}
       
   188 Writing files
       
   189 \begin{lstlisting}
       
   190 In []: f = open('hello.txt', 'w')
       
   191 In []: f.write('hello world\n')
       
   192 In []: f.close()
       
   193 \end{lstlisting}
       
   194 \begin{itemize}
       
   195     \item Everything read or written is a string
       
   196 \end{itemize}
       
   197 \end{frame}
       
   198 
       
   199 \begin{frame}[fragile]
   175 \begin{frame}[fragile]
   200     \frametitle{File and \kwrd{for}}
   176     \frametitle{File and \kwrd{for}}
   201 \begin{lstlisting}
   177 \begin{lstlisting}
   202 In []: f = open('dummyfile')
   178 In []: f = open('dummyfile')
   203 
   179 
   204 In []: for line in f:
   180 In []: for line in f:
   205     ...:     print line
   181     ...:     print line
   206     ...:  
   182     ...:  
   207 \end{lstlisting}
   183 \end{lstlisting}
   208 \inctime{10}
   184 \inctime{5}
   209 \end{frame}
   185 \end{frame}
   210 
   186 
   211 \section{Strings}
   187 \section{Strings}
   212 \begin{frame}[fragile]
   188 \begin{frame}{Strings}
   213   \frametitle{Strings}
   189 Anything within ``quotes'' is a string!
   214   \begin{lstlisting}
       
   215 s = 'this is a string'
       
   216 s = 'This one has "quotes" inside!'
       
   217 s = "I have 'single-quotes' inside!"
       
   218 l = "A string spanning many lines\
       
   219 one more line\
       
   220 yet another"
       
   221 t = """A triple quoted string does
       
   222 not need to be escaped at the end and 
       
   223 "can have nested quotes" etc."""
       
   224   \end{lstlisting}
       
   225 \end{frame}
   190 \end{frame}
   226 
   191 
   227 \begin{frame}[fragile]\frametitle{Strings and \typ{split()}}
   192 \begin{frame}[fragile]\frametitle{Strings and \typ{split()}}
   228   \begin{lstlisting}
   193   \begin{lstlisting}
   229 In []: a = 'hello world'
   194 In []: a = 'hello world'
   242 \end{frame}
   207 \end{frame}
   243 
   208 
   244 \section{Plotting points}
   209 \section{Plotting points}
   245 \begin{frame}[fragile]
   210 \begin{frame}[fragile]
   246 \frametitle{How to plot points?}
   211 \frametitle{How to plot points?}
   247 We saw how to plot graphs, lets now look at how to plot points.\\
       
   248 \begin{lstlisting}
   212 \begin{lstlisting}
   249 In []: plt.plot(x, sin(x), 'ro')
   213 In []: plt.plot(x, sin(x), 'ro')
   250 Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
   214 Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
   251 \end{lstlisting}
   215 \end{lstlisting}
   252 \begin{itemize}
   216 \begin{itemize}
   261 \section{Lists}
   225 \section{Lists}
   262 
   226 
   263 \begin{frame}[fragile]
   227 \begin{frame}[fragile]
   264   \frametitle{List creation and indexing}
   228   \frametitle{List creation and indexing}
   265 \begin{lstlisting}
   229 \begin{lstlisting}
   266 In []: lst = [] #Empty list
       
   267 
       
   268 In []: lst = [1,2,3,4] 
   230 In []: lst = [1,2,3,4] 
   269 #More useful list
       
   270 
       
   271 In []: len(lst)
       
   272 Out[]: 4
       
   273 
   231 
   274 In []: lst[0]+lst[1]+lst[-1]
   232 In []: lst[0]+lst[1]+lst[-1]
   275 Out[]: 7
   233 Out[]: 7
   276 \end{lstlisting}
   234 \end{lstlisting}
   277 \begin{itemize}
   235 \begin{itemize}
   280   \end{itemize}
   238   \end{itemize}
   281 \end{frame}
   239 \end{frame}
   282 
   240 
   283 \begin{frame}[fragile]
   241 \begin{frame}[fragile]
   284   \frametitle{List: slices}
   242   \frametitle{List: slices}
   285   \begin{itemize}
   243   \typ{list[initial:final:step]}
   286   \item Slicing is a basic operation
       
   287   \item \typ{list[initial:final:step]}
       
   288   \item  The step is optional
       
   289   \end{itemize}
       
   290 \begin{lstlisting}
   244 \begin{lstlisting}
   291 In []: lst[1:3]  # A slice.
   245 In []: lst[1:3]  # A slice.
   292 Out[]: [2, 3]
   246 Out[]: [2, 3]
   293 
   247 
   294 In []: lst[1:-1]
   248 In []: lst[1:-1]
   295 Out[]: [2, 3]
   249 Out[]: [2, 3]
   296 
   250 \end{lstlisting}
   297 In []: lst[1:] == lst[1:-1]
       
   298 Out[]: False
       
   299 \end{lstlisting}
       
   300 Explain last result
       
   301 \end{frame}
       
   302 
       
   303 \begin{frame}[fragile]
       
   304   \frametitle{List: more slices}
       
   305 \begin{lstlisting}
       
   306 In []: lst[0:-1:2] # Notice the step!
       
   307 Out[]: [1, 3]
       
   308 
       
   309 In []: lst[::2]
       
   310 Out[]: [1, 3]
       
   311 
       
   312 In []: lst[-1::-1]
       
   313 \end{lstlisting}
       
   314 What do you think the last one will do?
       
   315 \end{frame}
   251 \end{frame}
   316 
   252 
   317 \begin{frame}[fragile]
   253 \begin{frame}[fragile]
   318   \frametitle{List methods}
   254   \frametitle{List methods}
   319 \begin{lstlisting}
   255 \begin{lstlisting}
   320 In []: lst.append(5)
       
   321 In []: lst
       
   322 Out[]: [1, 2, 3, 4, 5]
       
   323 
       
   324 In []: lst.append([6,7])
   256 In []: lst.append([6,7])
   325 In []: lst
   257 In []: lst
   326 Out[]: [1, 2, 3, 4, 5, [6, 7]]
   258 Out[]: [1, 2, 3, 4, 5, [6, 7]]
   327 
   259 \end{lstlisting}
   328 In []: lst.extend([8,9])
   260 \inctime{10}
   329 In []: lst
       
   330 Out[]: [1, 2, 3, 4, 5, [6, 7], 8, 9]
       
   331 \end{lstlisting}
       
   332 \end{frame}
       
   333 
       
   334 \begin{frame}[fragile]
       
   335   \frametitle{List containership}
       
   336   \begin{lstlisting}
       
   337 In []: animals = ['cat', 'dog', 'rat', 'croc']
       
   338 
       
   339 In []: 'dog' in animals
       
   340 Out[]: True
       
   341 
       
   342 In []: 'snake' in animals
       
   343 Out[]: False
       
   344   \end{lstlisting}
       
   345   \inctime{10}
       
   346 \end{frame}
   261 \end{frame}
   347 
   262 
   348 \section{Modules and import}
   263 \section{Modules and import}
   349 \begin{frame}{Modules and \typ{import}}
   264 \begin{frame}{Modules and \typ{import}}
   350   \begin{itemize}
   265   \begin{itemize}