15 Welcome to the spoken tutorial on using python modules. |
15 Welcome to the spoken tutorial on using python modules. |
16 |
16 |
17 {{{ switch to next slide, outline slide }}} |
17 {{{ switch to next slide, outline slide }}} |
18 |
18 |
19 In this tutorial, we will see how to run python scripts from command |
19 In this tutorial, we will see how to run python scripts from command |
20 line, importing modules, importing scipy and pylab modules. |
20 line, importing modules, importing scipy and pylab modules. And also |
|
21 see the Python standard library. |
21 |
22 |
22 {{{ switch to next slide on executing python scripts from command line }}} |
23 {{{ switch to next slide on executing python scripts from command line }}} |
23 |
24 |
24 Let us create a simple python script to print hello world. Open your |
25 Let us create a simple python script to print hello world. Open your |
25 text editor and type the following, |
26 text editor and type the following, |
46 The correct method is to run it using the Python interpreter. Open the |
47 The correct method is to run it using the Python interpreter. Open the |
47 terminal and navigate to the directory where hello.py is, |
48 terminal and navigate to the directory where hello.py is, |
48 |
49 |
49 {{{ open terminal and navigate to directory where hello.py was saved }}} |
50 {{{ open terminal and navigate to directory where hello.py was saved }}} |
50 |
51 |
|
52 {{{ switch to next slide }}} |
|
53 |
51 now run the Python script as, |
54 now run the Python script as, |
52 :: |
55 :: |
53 |
56 |
54 python hello.py |
57 python hello.py |
55 |
58 |
56 It executed the script and we got the output ``Hello World!``. |
59 It executed the script and we got the output ``Hello World!``. |
57 |
60 |
58 {{{ highlight ``python filename`` syntax on slide while narrating }}} |
61 {{{ highlight ``python filename`` syntax on slide while narrating }}} |
59 |
62 |
60 The syntax is python space filename. |
63 The syntax is python space filename. |
|
64 |
|
65 {{{ switch to next slide, four plot problem }}} |
61 |
66 |
62 Now recall the four plot problem where we plotted four plots in a single |
67 Now recall the four plot problem where we plotted four plots in a single |
63 figure. Let us run that script from command line. |
68 figure. Let us run that script from command line. |
64 |
69 |
65 If you don't have the script, |
70 If you don't have the script, |
112 |
122 |
113 Yes! it worked. So what did we do? |
123 Yes! it worked. So what did we do? |
114 |
124 |
115 We actually imported the required modules using the keyword ``import``. |
125 We actually imported the required modules using the keyword ``import``. |
116 It could have also be done as, |
126 It could have also be done as, |
|
127 |
|
128 {{{ switch to next slide, better way of fixing }}} |
117 |
129 |
118 {{{ highlight the following in slide and say it loud }}} |
130 {{{ highlight the following in slide and say it loud }}} |
119 :: |
131 :: |
120 |
132 |
121 from scipy import linspace |
133 from scipy import linspace |
128 So in practice it is always good to use function names instead of |
140 So in practice it is always good to use function names instead of |
129 asterisk or star. As if we use asterisk to import from a particular |
141 asterisk or star. As if we use asterisk to import from a particular |
130 module then it will replace any existing functions with the same name |
142 module then it will replace any existing functions with the same name |
131 in our name-space. |
143 in our name-space. |
132 |
144 |
|
145 {{{ switch to next slide, Instead of ``*`` }}} |
|
146 |
133 So let us modify four_plot.py as, |
147 So let us modify four_plot.py as, |
134 {{{ delete the first two lines and add the following }}} |
148 {{{ delete the first two lines and add the following }}} |
135 :: |
149 :: |
136 |
150 |
137 from scipy import linspace, pi, sin |
151 from scipy import linspace, pi, sin |
138 from pylab import plot, legend, annotate, title, show |
152 from pylab import plot, legend, annotate |
139 from pylab import xlim, ylim |
153 from pylab import xlim, ylim, title, show |
|
154 |
|
155 Now let us try running the code again as, |
|
156 :: |
|
157 |
|
158 python four_plot.py |
|
159 |
|
160 It works! In this method we actually imported the functions to the |
|
161 current name-space, and there is another method of doing it. And that |
|
162 is, |
140 |
163 |
141 {{{ switch to next slide }}} |
164 {{{ switch to next slide }}} |
142 it could also be done as, |
|
143 |
|
144 .. import scipy |
|
145 .. import pylab |
|
146 .. x = scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500) |
|
147 .. pylab.plot(x, x, 'b') |
|
148 .. pylab.plot(x, -x, 'b') |
|
149 .. pylab.plot(x, scipy.sin(x), 'g', linewidth=2) |
|
150 .. pylab.plot(x, x*scipy.sin(x), 'r', linewidth=3) |
|
151 .. pylab.legend(['x', '-x', 'sin(x)', 'xsin(x)']) |
|
152 .. pylab.annotate('origin', xy = (0, 0)) |
|
153 .. pylab.xlim(-5*scipy.pi, 5*scipy.pi) |
|
154 .. pylab.ylim(-5*scipy.pi, 5*scipy.pi) |
|
155 |
|
156 |
165 |
157 Notice that we use ``scipy.pi`` instead of just ``pi`` as in the |
166 Notice that we use ``scipy.pi`` instead of just ``pi`` as in the |
158 previous method, and the functions are called as ``pylab.plot()`` and |
167 previous method, and the functions are called as ``pylab.plot()`` and |
159 ``pylab.annotate()`` and not as ``plot()`` and ``annotate()``. |
168 ``pylab.annotate()`` and not as ``plot()`` and ``annotate()``. |
160 |
169 |
209 ``http://docs.python.org/library/`` |
218 ``http://docs.python.org/library/`` |
210 |
219 |
211 The modules pylab, scipy, Mayavi are not part of the standard python |
220 The modules pylab, scipy, Mayavi are not part of the standard python |
212 library. |
221 library. |
213 |
222 |
214 {{{ switch to next slide, recap }}} |
223 {{{ switch to next slide, summary }}} |
215 |
224 |
216 This brings us to the end of this tutorial, in this tutorial we |
225 This brings us to the end of this tutorial, in this tutorial we |
217 learned running scripts from command line, learned about modules, saw |
226 learned running scripts from command line, learned about modules, saw |
218 the python standard library. |
227 the python standard library. |
219 |
228 |