day1/session2.tex
changeset 130 8905a5badd7e
parent 118 c7f85ba59af3
child 132 c10d8cb8d690
equal deleted inserted replaced
129:d3aae4b05e99 130:8905a5badd7e
    49 }
    49 }
    50 \newcounter{time}
    50 \newcounter{time}
    51 \setcounter{time}{0}
    51 \setcounter{time}{0}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    52 \newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
    53 
    53 
    54 \newcommand{\typ}[1]{\texttt{#1}}
    54 \newcommand{\typ}[1]{\lstinline{#1}}
    55 
    55 
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    56 \newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
    57 
    57 
    58 %%% This is from Fernando's setup.
    58 %%% This is from Fernando's setup.
    59 % \usepackage{color}
    59 % \usepackage{color}
    71 % }
    71 % }
    72 
    72 
    73 
    73 
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    75 % Title page
    75 % Title page
    76 \title[Basic Python]{Python:Basic Overview\\}
    76 \title[Basic Python]{Basic Overview\\}
    77 
    77 
    78 \author[FOSEE Team] {The FOSSEE Group}
    78 \author[FOSEE Team] {The FOSSEE Group}
    79 
    79 
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    80 \institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
    81 \date[] {31, October 2009\\Day 1, Session 2}
    81 \date[] {31, October 2009\\Day 1, Session 2}
   131 \end{frame}
   131 \end{frame}
   132 
   132 
   133 \begin{frame}[fragile]
   133 \begin{frame}[fragile]
   134 \frametitle{Functions: Example 1}
   134 \frametitle{Functions: Example 1}
   135   \begin{lstlisting}
   135   \begin{lstlisting}
   136 In [1]: def plot_sinx():
   136 In []: def plot_sinx():
   137    ....:     x = linspace(0, 2*pi, 100)
   137    ....:     x = linspace(0, 2*pi, 100)
   138    ....:     plt.plot(x, sin(x))
   138    ....:     plt.plot(x, sin(x))
   139    ....:     plt.show()
   139    ....:     plt.show()
   140    ....:    
   140    ....:    
   141 
   141 
   142 In [2]: plot_sinx()
   142 In []: plot_sinx()
   143   \end{lstlisting}
   143   \end{lstlisting}
   144 \end{frame}
   144 \end{frame}
   145 
   145 
   146 \begin{frame}[fragile]
   146 \begin{frame}[fragile]
   147 \frametitle{Functions: Example 2}
   147 \frametitle{Functions: Example 2}
   148   \begin{lstlisting}
   148   \begin{lstlisting}
   149 In [3]: def f(x):
   149 In []: def f(x):
   150    ....:     return sin(x*x*x)+(3*x*x)
   150    ....:     return sin(x*x*x)+(3*x*x)
   151 
   151 
   152 In [4]: x = linspace(0,2*pi, 1000)
   152 In []: x = linspace(0,2*pi, 1000)
   153 
   153 
   154 In [5]: plt.plot(x, f(x))
   154 In []: plt.plot(x, f(x))
   155   \end{lstlisting}
   155   \end{lstlisting}
   156   \inctime{10}
   156   \inctime{10}
   157 \end{frame}
   157 \end{frame}
   158 
   158 
   159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   165   {Creating python files}
   165   {Creating python files}
   166   \begin{itemize}
   166   \begin{itemize}
   167     \item aka scripts
   167     \item aka scripts
   168     \item use your editor
   168     \item use your editor
   169     \item extension \typ{.py}
   169     \item extension \typ{.py}
   170     \item run with \texttt{python hello.py} at the command line
   170     \item run with \typ{python hello.py} at the command line
   171     \item in IPython using \kwrd{\%run}
   171     \item in IPython using \kwrd{\%run}
   172   \end{itemize}
   172   \end{itemize}
   173 \inctime{5}
   173 \inctime{5}
   174 \end{frame}
   174 \end{frame}
   175 
   175 
   176 \section{Files Handling}
   176 \section{Files Handling}
   177 \begin{frame}[fragile]
   177 \begin{frame}[fragile]
   178   \frametitle{Basic File Operations}
   178   \frametitle{Basic File Operations}
   179 Opening and Reading files
   179 Opening and Reading files
   180 \begin{lstlisting}
   180 \begin{small}
   181 In [6]: f = open('/path/to/file_name')
   181 \begin{lstlisting}
   182 In [7]: data = f.read() # Read entire file.
   182 In []: f = open('hello.py')
   183 In [8]: line = f.readline() # Read one line.
   183 In []: data = f.read() # Read entire file.
   184 In [9]: f.close() # close the file.
   184 In []: line = f.readline() # Read 1 line.
   185 \end{lstlisting}
   185 In []: f.close() # close the file.
       
   186 \end{lstlisting}
       
   187 \end{small}
   186 Writing files
   188 Writing files
   187 \begin{lstlisting}
   189 \begin{lstlisting}
   188 In [10]: f = open('/path/to/file_name', 'w')
   190 In []: f = open('hello.txt', 'w')
   189 In [11]: f.write('hello world\n')
   191 In []: f.write('hello world\n')
   190 In [12]: f.close()
   192 In []: f.close()
   191 \end{lstlisting}
   193 \end{lstlisting}
   192 \begin{itemize}
   194 \begin{itemize}
   193     \item Everything read or written is a string
   195     \item Everything read or written is a string
   194 \end{itemize}
   196 \end{itemize}
   195 \end{frame}
   197 \end{frame}
   196 
   198 
   197 \begin{frame}[fragile]
   199 \begin{frame}[fragile]
   198     \frametitle{File and \kwrd{for}}
   200     \frametitle{File and \kwrd{for}}
   199 \begin{lstlisting}
   201 \begin{lstlisting}
   200 In [13]: f = open('dummyfile')
   202 In []: f = open('dummyfile')
   201 
   203 
   202 In [14]: for line in f:
   204 In []: for line in f:
   203     ...:     print line
   205     ...:     print line
   204     ...:  
   206     ...:  
   205 \end{lstlisting}
   207 \end{lstlisting}
   206 \inctime{10}
   208 \inctime{10}
   207 \end{frame}
   209 \end{frame}
   222   \end{lstlisting}
   224   \end{lstlisting}
   223 \end{frame}
   225 \end{frame}
   224 
   226 
   225 \begin{frame}[fragile]\frametitle{Strings and \typ{split()}}
   227 \begin{frame}[fragile]\frametitle{Strings and \typ{split()}}
   226   \begin{lstlisting}
   228   \begin{lstlisting}
   227 In [15]: a = 'hello world'
   229 In []: a = 'hello world'
   228 
   230 
   229 In [16]: a.split()
   231 In []: a.split()
   230 Out[17]: ['hello', 'world']
   232 Out[]: ['hello', 'world']
   231   \end{lstlisting}
   233   \end{lstlisting}
   232 Now try this:
   234 Now try this:
   233   \begin{lstlisting}
   235   \begin{lstlisting}
   234 In [18]: b = 'KD, Madhu, Punchagan, Shantanu, Vattam'
   236 In []: b = 'KD, MCS, PC, SC, SV'
   235 
   237 
   236 In [19]: b.split(',')
   238 In []: b.split(',')
   237 Out[20]: ['KD', ' Madhu', ' Punchagan', ' Shantanu', ' Vattam']
   239 Out[]: ['KD', 'MCS', 'PC', 'SC', 'SV']
   238   \end{lstlisting}
   240   \end{lstlisting}
   239 \inctime{5}
   241 \inctime{5}
   240 \end{frame}
   242 \end{frame}
   241 
   243 
   242 \section{Plotting points}
   244 \section{Plotting points}
   243 \begin{frame}[fragile]
   245 \begin{frame}[fragile]
   244 \frametitle{How to plot points?}
   246 \frametitle{How to plot points?}
   245 We saw how to plot graphs, lets now look at how to plot points.\\
   247 We saw how to plot graphs, lets now look at how to plot points.\\
   246 \begin{lstlisting}
   248 \begin{lstlisting}
   247 In [21]: plt.plot(x, sin(x), 'ro')
   249 In []: plt.plot(x, sin(x), 'ro')
   248 Out[22]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
   250 Out[]: [<matplotlib.lines.Line2D object at 0xac17e0c>]
   249 \end{lstlisting}
   251 \end{lstlisting}
   250 \begin{itemize}
   252 \begin{itemize}
   251   \item \kwrd{'r'},\kwrd{'g'},\kwrd{'b'} for red, green and blue
   253   \item \kwrd{'r'},\kwrd{'g'},\kwrd{'b'} for red, green and blue
   252   \item \kwrd{'o'} - Dots
   254   \item \kwrd{'o'} - Dots
   253   \item \kwrd{'-'} - Lines
   255   \item \kwrd{'-'} - Lines
   259 \section{Lists}
   261 \section{Lists}
   260 
   262 
   261 \begin{frame}[fragile]
   263 \begin{frame}[fragile]
   262   \frametitle{List creation and indexing}
   264   \frametitle{List creation and indexing}
   263 \begin{lstlisting}
   265 \begin{lstlisting}
   264 In [23]: lst = [] #Empty list
   266 In []: lst = [] #Empty list
   265 
   267 
   266 In [24]: lst = [1,2,3,4] #More useful list
   268 In []: lst = [1,2,3,4] 
   267 
   269 #More useful list
   268 In [25]: len(lst)
   270 
   269 Out[26]: 4
   271 In []: len(lst)
   270 
   272 Out[]: 4
   271 In [27]: lst[0]+lst[1]+lst[-1]
   273 
   272 Out[27]: 7
   274 In []: lst[0]+lst[1]+lst[-1]
       
   275 Out[]: 7
   273 \end{lstlisting}
   276 \end{lstlisting}
   274 \begin{itemize}
   277 \begin{itemize}
   275   \item Indices start with ?
   278   \item Indices start with ?
   276   \item Negative indices indicate ?
   279   \item Negative indices indicate ?
   277   \end{itemize}
   280   \end{itemize}
   283   \item Slicing is a basic operation
   286   \item Slicing is a basic operation
   284   \item \typ{list[initial:final:step]}
   287   \item \typ{list[initial:final:step]}
   285   \item  The step is optional
   288   \item  The step is optional
   286   \end{itemize}
   289   \end{itemize}
   287 \begin{lstlisting}
   290 \begin{lstlisting}
   288 In [28]: lst[1:3]  # A slice.
   291 In []: lst[1:3]  # A slice.
   289 Out[28]: [2, 3]
   292 Out[]: [2, 3]
   290 
   293 
   291 In [29]: lst[1:-1]
   294 In []: lst[1:-1]
   292 Out[29]: [2, 3]
   295 Out[]: [2, 3]
   293 
   296 
   294 In [30]: lst[1:] == lst[1:-1]
   297 In []: lst[1:] == lst[1:-1]
   295 Out[30]: False
   298 Out[]: False
   296 \end{lstlisting}
   299 \end{lstlisting}
   297 Explain last result
   300 Explain last result
   298 \end{frame}
   301 \end{frame}
   299 
   302 
   300 \begin{frame}[fragile]
   303 \begin{frame}[fragile]
   301   \frametitle{List: more slices}
   304   \frametitle{List: more slices}
   302 \begin{lstlisting}
   305 \begin{lstlisting}
   303 In [31]: lst[0:-1:2] # Notice the step!
   306 In []: lst[0:-1:2] # Notice the step!
   304 Out[31]: [1, 3]
   307 Out[]: [1, 3]
   305 
   308 
   306 In [31]: lst[::2]
   309 In []: lst[::2]
   307 Out[31]: [1, 3]
   310 Out[]: [1, 3]
   308 
   311 
   309 In [32]: lst[-1::-1]
   312 In []: lst[-1::-1]
   310 \end{lstlisting}
   313 \end{lstlisting}
   311 What do you think the last one will do?
   314 What do you think the last one will do?
   312 \end{frame}
   315 \end{frame}
   313 
   316 
   314 \begin{frame}[fragile]
   317 \begin{frame}[fragile]
   315   \frametitle{List methods}
   318   \frametitle{List methods}
   316 \begin{lstlisting}
   319 \begin{lstlisting}
   317 In [33]: lst.append(5)
   320 In []: lst.append(5)
   318 
   321 In []: lst
   319 In [34]: lst
   322 Out[]: [1, 2, 3, 4, 5]
   320 Out[34]: [1, 2, 3, 4, 5]
   323 
   321 
   324 In []: lst.append([6,7])
   322 In [35]: lst.append([6,7])
   325 In []: lst
   323 
   326 Out[]: [1, 2, 3, 4, 5, [6, 7]]
   324 In [36]: lst
   327 
   325 Out[36]: [1, 2, 3, 4, 5, [6, 7]]
   328 In []: lst.extend([8,9])
   326 
   329 In []: lst
   327 In [37]: lst.extend([8,9])
   330 Out[]: [1, 2, 3, 4, 5, [6, 7], 8, 9]
   328 
       
   329 In [38]: lst
       
   330 Out[38]: [1, 2, 3, 4, 5, [6, 7], 8, 9]
       
   331 \end{lstlisting}
   331 \end{lstlisting}
   332 \end{frame}
   332 \end{frame}
   333 
   333 
   334 \begin{frame}[fragile]
   334 \begin{frame}[fragile]
   335   \frametitle{List containership}
   335   \frametitle{List containership}
   336   \begin{lstlisting}
   336   \begin{lstlisting}
   337 In [39]: animals = ['cat', 'dog', 'rat', 'croc']
   337 In []: animals = ['cat', 'dog', 'rat', 'croc']
   338 
   338 
   339 In [40]: 'dog' in animals
   339 In []: 'dog' in animals
   340 Out[40]: True
   340 Out[]: True
   341 
   341 
   342 In [41]: 'snake' in animals
   342 In []: 'snake' in animals
   343 Out[41]: False
   343 Out[]: False
   344   \end{lstlisting}
   344   \end{lstlisting}
   345   \inctime{10}
   345   \inctime{10}
   346 \end{frame}
   346 \end{frame}
   347 
   347 
   348 \section{Modules and import}
   348 \section{Modules and import}