Python/C4/Using-python-modules/English-timed

From Script | Spoken-Tutorial
Revision as of 17:42, 28 March 2013 by Sneha (Talk | contribs)

Jump to: navigation, search
Visual Cue Narration
0:01 Hello Friends and Welcome to the spoken tutorial on 'Using Python Modules'.
0:06 At the end of this tutorial, you will be able to ,
  1. Execute python scripts from command line.
  2. Use import in scripts.
  3. Import scipy and pylab modules.
  4. Use python standard modules and 3rd party modules.
0:20 Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Embellishing a plot" and "Saving plots".
0:32 Let us create a simple python script to print hello world.
0:36 Open your text editor and type the following,
0:41 print within double quotes Hello world exclamation

print

1:02 Now save this script as hello.py ,
1:11 Start the ipython interpreter
1:14 Open the terminal and type ipython
1:20 In the previous tutorials,we have seen how to run a script using the IPython interpreter using percentage run
1:29 So type percentage run hypen i hello.py
1:40 but this is not the correct way of running a python script.
1:45 The correct method is to run it using the Python interpreter.
1:50 Open the terminal and navigate to the directory where hello.py is,
1:57 now run the Python script as,python hello.py
2:12 It executed the script and we got the output Hello World!
2:20 The syntax is python space filename
2:24 Now, we have a four plot problem where we have plotted four plots in a single figure.
2:34 Let us run that script from command line.
2:40 Type python four underscore plot.py
2:50 Oops! even though it was supposed to work, it didn't.
2:55 It gave an error linspace() is not defined, which means that the function linspace() is not available in the current name-space.
3:02 But if you try to run the same script using %run -i four underscore plot.py in your IPython interpreter started with the option hypen pylab it will work, because the hypen pylab option does some work for us by importing the required modules to our name-space when ipython interpreter starts.
3:25 And thus we don't have to explicitly import modules.
3:28 So now let us try to fix the problem and run the script in command line,
3:33 add this line as the first line in the script,
3:43 from scipy import star
4:12 Now let us run the script again,
4:15 Type python four underscore plot.py
4:25 Now it gave another error -- plot not defined,
4:32 let us edit the file again and add this line as the second line in our script and save it,
4:38 So add the line as second line in four underscore plot.py and save
4:47 from pylab import star
5:05 And now, run the script,
5:07 So type python four underscore plot.py
5:19 Yes! it worked.
5:21 So what did we do?
5:24 We actually imported the required modules using the keyword import .
5:29 It could also be done as by using, from scipy import linspace instead of, from scipy import *
5:39 So in practice it is always good to use function names instead of asterisk or star.
5:45 If we use asterisk to import from a particular module then it will replace any existing functions with the same name in our name-space.
5:56 So let us modify four underscore plot.py as, Hence we delete the first two lines of our code which we had added and add these lines
6:08 Type from scipy import linspace
     from scipy import linspace comma  pi comma  sin 
     from pylab import plot comma  legend comma  annotate
     from pylab import xlim comma  ylim comma  title comma  show
7:08 Now let us try running the code again as,python four underscore plot.py and hit enter
7:19 It works! In this method we actually imported the functions to the current name-space.
7:24 There is one more way of doing it.
7:26 And that is,
7:35 Notice that we use scipy.pi instead of just pi as in the previous method, and the functions are called as pylab.plot()and pylab.annotate() and not as plot() and annotate( .
7:55 Pause the video here, try out the following exercise and resume the video.
8:01 Write a script to plot a sine wave from minus two pi to two pi.
8:09 It can solved as,
8:13 The first line we import the required functions linspace() , sin() and constant pi from the module scipy.
8:24 The second and third line we import the functions plot() , legend() , show() , title() , xlabel() and ylabel() .
8:34 And the rest the code to generate the plot.
8:43 We can run it as,python sine.py
8:50 python sine.py
8:56 As we can see, we our sine plot.
9:01 Let us move further in our topic.
9:06 Until now we have been learning about importing modules, now what is a module?
9:11 A module is simply a file containing Python definitions and statements.
9:18 Definitions from a module can be imported into other modules or into the main module.
9:24 Python has a very rich standard library of modules.
9:29 It is very extensive, offering a wide range of facilities.
9:33 Some of the standard modules are,
9:36 for Math: math, random for Internet access: urllib2, smtplib for System, Command line arguments: sys for Operating system interface: os for regular expressions: re for compression: g zip, zip file, tar file And there are lot more.
10:13 Find more information at Python Library reference, http://docs.python.org/library/
10:25 There are a lot of other modules like pylab, scipy, Mayavi, etc which are not part of the standard python library.
10:32 This brings us to the end of this tutorial.
10:35 In this tutorial, we have learn't to, 1. Run scripts from command line,
10:39 2. Import modules by specifying the module name followed by an asterisk.
10:45 3. Import only the required functions from modules by specifying the function name.
10:50 4. Use python standard library.
10:54 Here are some self assessment questions for you to solve
10:58 1. Which among this is correct ?
    • from scipy import plot
    • from numpy import plot
    • from matplotlib import plot
    • from pylab import plot
11:11 2. Which among these libraries is part of python standard library ?
    • Mayavi
    • scipy
    • matplotlib
    • urllib2
11:23 3. Functions xlim() and ylim() can be imported to the current name-space as,
    • from pylab import xlim comma ylim
    • import pylab
    • from scipy import xlim comma ylim
    • import scipy
11:44 And the answers,
11:49 1. The option from pylab import plot is the correct one, since plot is a function of module module.
11:59 2. urllib2 is a part of the python standard library.
12:06 3. Functions xlim() and ylim() can be imported to the current name-space as, from pylab import xlim comma ylim .
12:16 Hope you have enjoyed this tutorial and found it useful.
12:19 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha