Difference between revisions of "Python/C4/Using-python-modules/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 9: Line 9:
 
|-
 
|-
 
| 00:06
 
| 00:06
| At the end of this tutorial, you will be able to ,
+
| At the end of this tutorial, you will be able to ,Execute python scripts from command line.
 
+
Use import in scripts.
# Execute python scripts from command line.
+
Import scipy and pylab modules.
# Use import in scripts.
+
Use python standard modules and 3rd party modules.
# Import scipy and pylab modules.
+
# Use python standard modules and 3rd party modules.
+
  
 
|-
 
|-
Line 30: Line 28:
 
|-
 
|-
 
|00:41
 
|00:41
|print  within double quotes Hello world exclamation
+
|print  within double quotes Hello world exclamation print
print
+
  
 
|-
 
|-
Line 179: Line 176:
 
|-
 
|-
 
|06:08
 
|06:08
|Type from scipy import linspace  
+
|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
      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
+
  
 
|-
 
|-
Line 306: Line 300:
 
|-
 
|-
 
| 10:58
 
| 10:58
| 1. Which among this is correct ?
+
| Which among this is correct ?
** from scipy import plot
+
from scipy import plot
** from numpy import plot
+
from numpy import plot
** from matplotlib import plot
+
from matplotlib import plot
** from pylab import plot
+
from pylab import plot
  
 
|-
 
|-
 
| 11:11
 
| 11:11
| 2. Which among these libraries is part of python standard library ?
+
|Which among these libraries is part of python standard library ?
** Mayavi
+
Mayavi
** scipy
+
scipy
** matplotlib
+
matplotlib
** urllib2
+
urllib2
  
 
|-
 
|-
 
| 11:23
 
| 11:23
| 3. Functions <tt>xlim()</tt> and <tt>ylim()</tt> can be imported to the current name-space as,
+
| Functions <tt>xlim()</tt> and <tt>ylim()</tt> can be imported to the current name-space as,
** from pylab import xlim comma  ylim
+
from pylab import xlim comma  ylim
** import pylab
+
import pylab
** from scipy import xlim comma  ylim
+
from scipy import xlim comma  ylim
** import scipy
+
import scipy
  
 
|-
 
|-
Line 334: Line 328:
 
|-
 
|-
 
| 11:49
 
| 11:49
| 1. The option from pylab import plot  is the correct one, since plot is a function of module module.
+
|The option from pylab import plot  is the correct one, since plot is a function of module module.
  
 
|-
 
|-
 
| 11:59
 
| 11:59
| 2. urllib2  is a part of the python standard library.
+
|urllib2  is a part of the python standard library.
  
 
|-
 
|-
 
| 12:06
 
| 12:06
| 3. Functions xlim()  and ylim()  can be imported to the current name-space as, from pylab import xlim comma  ylim .
+
|Functions xlim()  and ylim()  can be imported to the current name-space as, from pylab import xlim comma  ylim .
 
 
 
|-
 
|-

Latest revision as of 16:16, 20 February 2017

Time Narration
00:01 Hello Friends and Welcome to the spoken tutorial on 'Using Python Modules'.
00:06 At the end of this tutorial, you will be able to ,Execute python scripts from command line.

Use import in scripts. Import scipy and pylab modules. Use python standard modules and 3rd party modules.

00:20 Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Embellishing a plot" and "Saving plots".
00:32 Let us create a simple python script to print hello world.
00:36 Open your text editor and type the following,
00:41 print within double quotes Hello world exclamation print
01:02 Now save this script as hello.py ,
01:11 Start the ipython interpreter
01:14 Open the terminal and type ipython
01:20 In the previous tutorials,we have seen how to run a script using the IPython interpreter using percentage run
01:29 So type percentage run hypen i hello.py
01:40 but this is not the correct way of running a python script.
01:45 The correct method is to run it using the Python interpreter.
01:50 Open the terminal and navigate to the directory where hello.py is,
01:57 now run the Python script as,python hello.py
02:12 It executed the script and we got the output Hello World!
02:20 The syntax is python space filename
02:24 Now, we have a four plot problem where we have plotted four plots in a single figure.
02:34 Let us run that script from command line.
02:40 Type python four underscore plot.py
02:50 Oops! even though it was supposed to work, it didn't.
02:55 It gave an error linspace() is not defined, which means that the function linspace() is not available in the current name-space.
03: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.
03:25 And thus we don't have to explicitly import modules.
03:28 So now let us try to fix the problem and run the script in command line,
03:33 add this line as the first line in the script,
03:43 from scipy import star
04:12 Now let us run the script again,
04:15 Type python four underscore plot.py
04:25 Now it gave another error -- plot not defined,
04:32 let us edit the file again and add this line as the second line in our script and save it,
04:38 So add the line as second line in four underscore plot.py and save
04:47 from pylab import star
05:05 And now, run the script,
05:07 So type python four underscore plot.py
05:19 Yes! it worked.
05:21 So what did we do?
05:24 We actually imported the required modules using the keyword import .
05:29 It could also be done as by using, from scipy import linspace instead of, from scipy import *
05:39 So in practice it is always good to use function names instead of asterisk or star.
05: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.
05: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
06: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
07:08 Now let us try running the code again as,python four underscore plot.py and hit enter
07:19 It works! In this method we actually imported the functions to the current name-space.
07:24 There is one more way of doing it.
07:26 And that is,
07: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( .
07:55 Pause the video here, try out the following exercise and resume the video.
08:01 Write a script to plot a sine wave from minus two pi to two pi.
08:09 It can solved as,
08:13 The first line we import the required functions linspace() , sin() and constant pi from the module scipy.
08:24 The second and third line we import the functions plot() , legend() , show() , title() , xlabel() and ylabel() .
08:34 And the rest the code to generate the plot.
08:43 We can run it as,python sine.py
08:50 python sine.py
08:56 As we can see, we our sine plot.
09:01 Let us move further in our topic.
09:06 Until now we have been learning about importing modules, now what is a module?
09:11 A module is simply a file containing Python definitions and statements.
09:18 Definitions from a module can be imported into other modules or into the main module.
09:24 Python has a very rich standard library of modules.
09:29 It is very extensive, offering a wide range of facilities.
09:33 Some of the standard modules are,
09: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 Which among this is correct ?

from scipy import plot from numpy import plot from matplotlib import plot from pylab import plot

11:11 Which among these libraries is part of python standard library ?

Mayavi scipy matplotlib urllib2

11:23 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 The option from pylab import plot is the correct one, since plot is a function of module module.
11:59 urllib2 is a part of the python standard library.
12:06 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