|
1 % Created 2010-10-12 Tue 17:12 |
|
2 \documentclass[presentation]{beamer} |
|
3 \usepackage[latin1]{inputenc} |
|
4 \usepackage[T1]{fontenc} |
|
5 \usepackage{fixltx2e} |
|
6 \usepackage{graphicx} |
|
7 \usepackage{longtable} |
|
8 \usepackage{float} |
|
9 \usepackage{wrapfig} |
|
10 \usepackage{soul} |
|
11 \usepackage{t1enc} |
|
12 \usepackage{textcomp} |
|
13 \usepackage{marvosym} |
|
14 \usepackage{wasysym} |
|
15 \usepackage{latexsym} |
|
16 \usepackage{amssymb} |
|
17 \usepackage{hyperref} |
|
18 \tolerance=1000 |
|
19 \usepackage[english]{babel} \usepackage{ae,aecompl} |
|
20 \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} |
|
21 \usepackage{listings} |
|
22 \lstset{language=Python, basicstyle=\ttfamily\bfseries, |
|
23 commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, |
|
24 showstringspaces=false, keywordstyle=\color{blue}\bfseries} |
|
25 \providecommand{\alert}[1]{\textbf{#1}} |
|
26 |
|
27 \title{Using python modules} |
|
28 \author{FOSSEE} |
|
29 \date{} |
|
30 |
|
31 \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} |
|
32 \begin{document} |
|
33 |
|
34 \maketitle |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 \begin{frame} |
|
45 \frametitle{Outline} |
|
46 \label{sec-1} |
|
47 |
|
48 \begin{itemize} |
|
49 \item Running python scripts from command line |
|
50 \item Importing python modules |
|
51 \item Importing scipy \& pylab modules |
|
52 \item About python standard library. |
|
53 \end{itemize} |
|
54 \end{frame} |
|
55 \begin{frame}[fragile] |
|
56 \frametitle{Running Python script from command line} |
|
57 \label{sec-2} |
|
58 |
|
59 \begin{itemize} |
|
60 \item Create a script, open text editor and type the following |
|
61 \begin{verbatim} |
|
62 print "hello world!" |
|
63 print |
|
64 \end{verbatim} |
|
65 |
|
66 \item Save the script as \texttt{hello.py} |
|
67 \end{itemize} |
|
68 \end{frame} |
|
69 \begin{frame}[fragile] |
|
70 \frametitle{Running Python script from command line (cont'd)} |
|
71 \label{sec-3} |
|
72 |
|
73 \begin{itemize} |
|
74 \item Run the script |
|
75 \begin{verbatim} |
|
76 $ python hello.py |
|
77 \end{verbatim} |
|
78 |
|
79 \end{itemize} |
|
80 |
|
81 \emph{Syntax :} \textbf{python filename} |
|
82 \end{frame} |
|
83 \begin{frame} |
|
84 \frametitle{Four plot problem} |
|
85 \label{sec-4} |
|
86 |
|
87 \begin{center} |
|
88 \includegraphics[scale=0.4]{four_plot} |
|
89 \end{center} |
|
90 \end{frame} |
|
91 \begin{frame}[fragile] |
|
92 \frametitle{Fix \texttt{linspace()} problem} |
|
93 \label{sec-5} |
|
94 |
|
95 \begin{verbatim} |
|
96 from scipy import * |
|
97 \end{verbatim} |
|
98 \end{frame} |
|
99 \begin{frame}[fragile] |
|
100 \frametitle{Fix \texttt{plot()} problem} |
|
101 \label{sec-6} |
|
102 |
|
103 \begin{verbatim} |
|
104 from pylab import * |
|
105 \end{verbatim} |
|
106 \end{frame} |
|
107 \begin{frame}[fragile] |
|
108 \frametitle{Better way of fixing} |
|
109 \label{sec-7} |
|
110 |
|
111 \begin{verbatim} |
|
112 from scipy import linspace |
|
113 \end{verbatim} |
|
114 |
|
115 instead of |
|
116 \begin{verbatim} |
|
117 from scipy import * |
|
118 \end{verbatim} |
|
119 |
|
120 \texttt{*} means import all functions from name-space \texttt{scipy}. |
|
121 \end{frame} |
|
122 \begin{frame}[fragile] |
|
123 \frametitle{Instead of \texttt{*}} |
|
124 \label{sec-8} |
|
125 |
|
126 \begin{verbatim} |
|
127 from scipy import linspace, pi, sin |
|
128 from pylab import plot, legend, annotate |
|
129 from pylab import xlim, ylim, title, show |
|
130 \end{verbatim} |
|
131 |
|
132 Is better than, \texttt{from scipy import *} \& \texttt{from pylab import *}. |
|
133 \end{frame} |
|
134 \begin{frame}[fragile] |
|
135 \frametitle{Another Fix} |
|
136 \label{sec-9} |
|
137 |
|
138 \begin{verbatim} |
|
139 import scipy |
|
140 import pylab |
|
141 x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500) |
|
142 pylab.plot(x, x, 'b') |
|
143 pylab.plot(x, -x, 'b') |
|
144 pylab.plot(x, scipy.sin(x), 'g', linewidth=2) |
|
145 pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3) |
|
146 pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)']) |
|
147 pylab.annotate('origin', xy = (0, 0)) |
|
148 pylab.xlim(-5*scipy.pi, 5*scipy.pi) |
|
149 pylab.ylim(-5*scipy.pi, 5*scipy.pi) |
|
150 \end{verbatim} |
|
151 \end{frame} |
|
152 \begin{frame} |
|
153 \frametitle{Exercise 1} |
|
154 \label{sec-10} |
|
155 |
|
156 Write a python script to plot a sine wave from |
|
157 $-2\Pi$ |
|
158 to |
|
159 $2\Pi$ |
|
160 . |
|
161 \end{frame} |
|
162 \begin{frame} |
|
163 \frametitle{What is a module?} |
|
164 \label{sec-11} |
|
165 |
|
166 Module is simply a file containing Python definitions and |
|
167 statements. Definitions from a module can be imported into other |
|
168 modules or into the main module. |
|
169 \end{frame} |
|
170 \begin{frame} |
|
171 \frametitle{Python standard library} |
|
172 \label{sec-12} |
|
173 |
|
174 Python has a very rich standard library of modules. |
|
175 \begin{itemize} |
|
176 \item Few libraries |
|
177 |
|
178 \begin{itemize} |
|
179 \item Math: \texttt{math}, \texttt{random} |
|
180 \item Internet access: \texttt{urllib2}, \texttt{smtplib} |
|
181 \item System, Command line arguments: \texttt{sys} |
|
182 \item Operating system interface: \texttt{os} |
|
183 \item regular expressions: \texttt{re} |
|
184 \item compression: \texttt{gzip}, \texttt{zipfile}, \texttt{tarfile} |
|
185 \end{itemize} |
|
186 |
|
187 \item More information |
|
188 |
|
189 \begin{itemize} |
|
190 \item \href{http://docs.python.org/library}{http://docs.python.org/library} |
|
191 \end{itemize} |
|
192 |
|
193 \end{itemize} |
|
194 \end{frame} |
|
195 \begin{frame} |
|
196 \frametitle{Summary} |
|
197 \label{sec-13} |
|
198 |
|
199 \begin{itemize} |
|
200 \item Running scripts from command line |
|
201 \item Learned about modules |
|
202 |
|
203 \begin{itemize} |
|
204 \item importing modules |
|
205 \end{itemize} |
|
206 |
|
207 \item Python standard library |
|
208 \end{itemize} |
|
209 \end{frame} |
|
210 \begin{frame} |
|
211 \frametitle{Thank you!} |
|
212 \label{sec-14} |
|
213 |
|
214 \begin{block}{} |
|
215 \begin{center} |
|
216 This spoken tutorial has been produced by the |
|
217 \textcolor{blue}{FOSSEE} team, which is funded by the |
|
218 \end{center} |
|
219 \begin{center} |
|
220 \textcolor{blue}{National Mission on Education through \\ |
|
221 Information \& Communication Technology \\ |
|
222 MHRD, Govt. of India}. |
|
223 \end{center} |
|
224 \end{block} |
|
225 \end{frame} |
|
226 |
|
227 \end{document} |