1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
% Created 2011-07-13 Wed 15:46
\documentclass[presentation]{beamer}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage[english]{babel} \usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
\usepackage{listings}
\lstset{language=Python, basicstyle=\ttfamily\bfseries,
commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
showstringspaces=false, keywordstyle=\color{blue}\bfseries}
\providecommand{\alert}[1]{\textbf{#1}}
\title{}
\author{FOSSEE}
\date{}
\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
\begin{document}
\begin{frame}
\begin{center}
\vspace{12pt}
\textcolor{blue}{\huge Using python modules}
\end{center}
\vspace{18pt}
\begin{center}
\vspace{10pt}
\includegraphics[scale=0.95]{../images/fossee-logo.png}\\
\vspace{5pt}
\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\
\scriptsize Funded by National Mission on Education through ICT\\
\scriptsize MHRD,Govt. of India\\
\includegraphics[scale=0.30]{../images/iitb-logo.png}\\
\end{center}
\end{frame}
\begin{frame}
\frametitle{Objectives}
\label{sec-2}
At the end of this tutorial, you will be able to ,
\begin{itemize}
\item Execute python scripts from command line.
\item Use import in scripts.
\item Import scipy and pylab modules.
\item Use python standard modules and 3rd party modules.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Pre-requisite}
\label{sec-3}
Spoken tutorial on -
\begin{itemize}
\item Using plot interactively.
\item Embellishing a plot.
\item Saving plots.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Running Python script from command line}
\label{sec-4}
\begin{itemize}
\item Create a script, open text editor and type the following
\begin{verbatim}
print "hello world!"
print
\end{verbatim}
\item Save the script as \verb~hello.py~
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Running Python script from command line (cont'd)}
\label{sec-5}
\begin{itemize}
\item Run the script
\begin{verbatim}
$ python hello.py
\end{verbatim}
\end{itemize}
\emph{Syntax :} \textbf{python filename}
\end{frame}
\begin{frame}
\frametitle{Four plot problem}
\label{sec-6}
\begin{center}
\includegraphics[scale=0.4]{four_plot}
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Fix \verb~linspace()~ problem}
\label{sec-7}
\begin{verbatim}
from scipy import *
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Fix \verb~plot()~ problem}
\label{sec-8}
\begin{verbatim}
from pylab import *
\end{verbatim}
\end{frame}
\begin{frame}[fragile]
\frametitle{Better way of fixing}
\label{sec-9}
\begin{verbatim}
from scipy import linspace
\end{verbatim}
instead of
\begin{verbatim}
from scipy import *
\end{verbatim}
\verb~*~ means import all functions from name-space \verb~scipy~.
\end{frame}
\begin{frame}[fragile]
\frametitle{Instead of \verb~*~}
\label{sec-10}
\begin{verbatim}
from scipy import linspace, pi, sin
from pylab import plot, legend, annotate
from pylab import xlim, ylim, title, show
\end{verbatim}
Is better than, \verb~from scipy import *~ \& \verb~from pylab import *~.
\end{frame}
\begin{frame}[fragile]
\frametitle{Another Fix}
\label{sec-11}
\lstset{language=Python}
\begin{lstlisting}
import scipy
import pylab
x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500)
pylab.plot(x, x, 'b')
pylab.plot(x, -x, 'b')
pylab.plot(x, scipy.sin(x), 'g', linewidth=2)
pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3)
pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)'])
pylab.annotate('origin', xy = (0, 0))
pylab.xlim(-5*scipy.pi, 5*scipy.pi)
pylab.ylim(-5*scipy.pi, 5*scipy.pi)
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Exercise 1}
\label{sec-12}
\begin{itemize}
\item Write a python script to plot a sine wave from
$-2\Pi$
to
$2\Pi$
.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{What is a module?}
\label{sec-13}
Module is simply a file containing Python definitions and
statements. Definitions from a module can be imported into other
modules or into the main module.
\end{frame}
\begin{frame}
\frametitle{Python standard library}
\label{sec-14}
Python has a very rich standard library of modules.
\begin{itemize}
\item Few libraries
\begin{itemize}
\item Math: \verb~math~, \verb~random~
\item Internet access: \verb~urllib2~, \verb~smtplib~
\item System, Command line arguments: \verb~sys~
\item Operating system interface: \verb~os~
\item regular expressions: \verb~re~
\item compression: \verb~gzip~, \verb~zipfile~, \verb~tarfile~
\end{itemize}
\item More information
\begin{itemize}
\item \href{http://docs.python.org/library}{http://docs.python.org/library}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Summary}
\label{sec-15}
In this tutorial, we have learnt to,
\begin{itemize}
\item Run scripts from command line,
\item Import modules by specifying the module name followed by
an asterisk.
\item Import only the required functions from modules by specifying
the function name.
\item Use python standard library.
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Evaluation}
\label{sec-16}
\begin{enumerate}
\item Which among this is correct ?
\begin{itemize}
\item from scipy import plot
\item from numpy import plot
\item from matplotlib import plot
\item from pylab import plot
\end{itemize}
\vspace{2pt}
\item Which among these libraries is part of python standard library ?
\begin{itemize}
\item Mayavi
\item scipy
\item matplotlib
\item urllib2
\end{itemize}
\vspace{2pt}
\item Functions ``xlim()'' and ``ylim()'' can be imported to the current
name-space as,
\begin{itemize}
\item from pylab import xlim, ylim
\item import pylab
\item from scipy import xlim, ylim
\item import scipy
\end{itemize}
\end{enumerate}
\end{frame}
\begin{frame}
\frametitle{Solutions}
\label{sec-17}
\begin{enumerate}
\item from pylab import plot
\vspace{12pt}
\item urllib2
\vspace{12pt}
\item from pylab import xlim, ylim
\end{enumerate}
\end{frame}
\begin{frame}
\begin{block}{}
\begin{center}
\textcolor{blue}{\Large THANK YOU!}
\end{center}
\end{block}
\begin{block}{}
\begin{center}
For more Information, visit our website\\
\url{http://fossee.in/}
\end{center}
\end{block}
\end{frame}
\end{document}
|