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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !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 ab…')
 
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Visual Cue
+
|'''Time'''
!Narration
+
|'''Narration'''
 +
 
 
|-
 
|-
|0:01
+
|00:01
 
| Hello Friends and Welcome to the spoken tutorial on 'Using Python Modules'.
 
| Hello Friends and Welcome to the spoken tutorial on 'Using Python Modules'.
  
 
|-
 
|-
| 0: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.
+
  
 
|-
 
|-
| 0:20
+
| 00:20
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Embellishing a plot" and "Saving plots".
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Using plot interactively", "Embellishing a plot" and "Saving plots".
  
 
|-
 
|-
| 0:32
+
| 00:32
 
| Let us create a simple python script to print hello world.  
 
| Let us create a simple python script to print hello world.  
  
 
|-
 
|-
| 0:36
+
| 00:36
 
| Open your text editor and type the following,
 
| Open your text editor and type the following,
  
 
|-
 
|-
|0:41
+
|00:41
|print  within double quotes Hello world exclamation
+
|print  within double quotes Hello world exclamation print
print
+
  
 
|-
 
|-
| 1:02
+
| 01:02
| Now save this script as <tt>hello.py</tt>,
+
| Now save this script as hello.py ,
  
 
|-
 
|-
| 1:11
+
| 01:11
 
| Start the ipython interpreter
 
| Start the ipython interpreter
  
 
|-
 
|-
|1:14
+
|01:14
 
|Open the terminal and type ipython
 
|Open the terminal and type ipython
  
 
|-
 
|-
| 1:20
+
| 01:20
| In the previous tutorials,we have seen how to run a script using the IPython interpreter using <tt>percentage run</tt>
+
| In the previous tutorials,we have seen how to run a script using the IPython interpreter using percentage run  
  
 
|-
 
|-
|1:29
+
|01:29
 
|So type  percentage run hypen i hello.py  
 
|So type  percentage run hypen i hello.py  
  
 
|-
 
|-
| 1:40
+
| 01:40
 
| but this is not the correct way of running a python script.
 
| but this is not the correct way of running a python script.
  
 
|-
 
|-
| 1:45
+
| 01:45
 
| The correct method is to run it using the Python interpreter.  
 
| The correct method is to run it using the Python interpreter.  
  
 
|-
 
|-
| 1:50
+
| 01:50
 
| Open the terminal and navigate to the directory where hello.py is,
 
| Open the terminal and navigate to the directory where hello.py is,
  
 
|-
 
|-
| 1:57
+
|01:57
 
| now run the Python script as,python hello.py  
 
| now run the Python script as,python hello.py  
  
 
|-
 
|-
| 2:12
+
| 02:12
| It executed the script and we got the output <tt>Hello World!</tt>.
+
| It executed the script and we got the output Hello World!  
  
 
|-
 
|-
2:20
+
02:20
| The syntax is <tt>python space filename</tt>.
+
| The syntax is python space filename  
  
 
|-
 
|-
| 2:24
+
| 02:24
 
| Now, we have a four plot problem where we have plotted four plots in a single figure.  
 
| Now, we have a four plot problem where we have plotted four plots in a single figure.  
  
 
|-
 
|-
| 2:34
+
| 02:34
 
| Let us run that script from command line.
 
| Let us run that script from command line.
  
 
|-
 
|-
|2:40
+
|02:40
 
|Type python four underscore plot.py  
 
|Type python four underscore plot.py  
  
 
|-
 
|-
| 2:50
+
| 02:50
 
| Oops! even though it was supposed to work, it didn't.  
 
| Oops! even though it was supposed to work, it didn't.  
  
 
|-
 
|-
| 2:55
+
| 02:55
| It gave an error <tt>linspace()</tt> is not defined, which means that the function <tt>linspace()</tt> is not available in the current name-space.
+
| It gave an error linspace() is not defined, which means that the function linspace() is not available in the current name-space.
  
 
|-
 
|-
| 3:02  
+
| 03:02  
| But if you try to run the same script using <tt>%run -i four underscore plot.py</tt> in your IPython interpreter started with the option <tt> hypen pylab</tt> it will work, because the <tt> hypen pylab</tt> option does some work for us by importing the required modules to our name-space when ipython interpreter starts.  
+
| 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
+
| 03:25
 
| And thus we don't have to explicitly import modules.
 
| And thus we don't have to explicitly import modules.
  
 
|-
 
|-
| 3:28
+
| 03:28
 
| So now let us try to fix the problem and run the script in command line,
 
| So now let us try to fix the problem and run the script in command line,
  
 
|-
 
|-
|3:33
+
|03:33
 
| add this line as the first line in the script,
 
| add this line as the first line in the script,
  
 
|-
 
|-
|3:43
+
|03:43
 
|from scipy import star
 
|from scipy import star
  
 
|-
 
|-
| 4:12
+
| 04:12
 
| Now let us run the script again,
 
| Now let us run the script again,
  
 
|-
 
|-
|4:15
+
|04:15
 
|Type python four underscore plot.py  
 
|Type python four underscore plot.py  
  
 
|-
 
|-
| 4:25
+
| 04:25
 
| Now it gave another error -- plot not defined,
 
| Now it gave another error -- plot not defined,
  
 
|-
 
|-
| 4:32
+
| 04:32
 
| let us edit the file again and add this line as the second line in our script and save it,
 
| let us edit the file again and add this line as the second line in our script and save it,
  
 
|-
 
|-
|4:38
+
|04:38
 
|So add the line as second line in four underscore plot.py and save  
 
|So add the line as second line in four underscore plot.py and save  
  
 
|-
 
|-
|4:47
+
|04:47
 
|from pylab import star
 
|from pylab import star
  
 
|-
 
|-
| 5:05
+
| 05:05
 
| And now, run the script,
 
| And now, run the script,
  
 
|-
 
|-
|5:07
+
|05:07
 
|So type python four underscore plot.py
 
|So type python four underscore plot.py
  
 
|-
 
|-
| 5:19
+
|05:19
 
| Yes! it worked.  
 
| Yes! it worked.  
  
 
|-
 
|-
| 5:21
+
| 05:21
 
| So what did we do?
 
| So what did we do?
  
 
|-
 
|-
| 5:24
+
|05:24
| We actually imported the required modules using the keyword <tt>import</tt>.  
+
| We actually imported the required modules using the keyword import .  
  
 
|-
 
|-
| 5:29
+
| 05:29
 
| It could also be done as by using, from scipy import linspace instead of, from scipy import *
 
| It could also be done as by using, from scipy import linspace instead of, from scipy import *
  
 
|-
 
|-
| 5:39
+
| 05:39
 
| So in practice it is always good to use function names instead of asterisk or star.  
 
| So in practice it is always good to use function names instead of asterisk or star.  
  
 
|-
 
|-
| 5:45
+
| 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.
 
| 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
+
| 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
 
| 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
+
|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
+
  
 
|-
 
|-
| 7:08
+
| 07:08
 
| Now let us try running the code again as,python four underscore plot.py and hit enter
 
| Now let us try running the code again as,python four underscore plot.py and hit enter
  
 
|-
 
|-
| 7:19
+
| 07:19
 
| It works! In this method we actually imported the functions to the current name-space.  
 
| It works! In this method we actually imported the functions to the current name-space.  
  
 
|-
 
|-
| 7:24
+
| 07:24
 
| There is one more way of doing it.  
 
| There is one more way of doing it.  
  
 
|-
 
|-
| 7:26
+
| 07:26
 
| And that is,
 
| And that is,
  
 
|-
 
|-
|7:35
+
|07:35
| Notice that we use <tt>scipy.pi</tt> instead of just <tt>pi</tt> as in the previous method, and the functions are called as <tt>pylab.plot()</tt> and <tt>pylab.annotate()</tt> and not as <tt>plot()</tt> and <tt>annotate()</tt>.
+
| 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
+
| 07:55
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 8:01
+
| 08:01
 
| Write a script to plot a sine wave from minus two pi to two pi.  
 
| Write a script to plot a sine wave from minus two pi to two pi.  
  
 
|-
 
|-
| 8:09
+
| 08:09
| <nowiki><Pause> It can solved as,</nowiki>
+
| It can solved as,  
  
 
|-
 
|-
| 8:13
+
| 08:13
| The first line we import the required functions <tt>linspace()</tt> , <tt>sin()</tt> and constant <tt>pi</tt> from the module scipy.  
+
| The first line we import the required functions linspace() , sin() and constant pi from the module scipy.  
  
 
|-
 
|-
| 8:24
+
| 08:24
| The second and third line we import the functions <tt>plot()</tt>, <tt>legend()</tt>, <tt>show()</tt>, <tt>title()</tt>, <tt>xlabel()</tt> and <tt>ylabel()</tt>.  
+
| The second and third line we import the functions plot() , legend() , show() , title() , xlabel() and ylabel() .  
  
 
|-
 
|-
| 8:34
+
| 08:34
 
| And the rest the code to generate the plot.
 
| And the rest the code to generate the plot.
  
 
|-
 
|-
| 8:43
+
| 08:43
 
| We can run it as,python sine.py
 
| We can run it as,python sine.py
  
 
|-
 
|-
|8:50
+
|08:50
 
|python sine.py
 
|python sine.py
  
 
|-
 
|-
| 8:56
+
| 08:56
 
| As we can see, we our sine plot.  
 
| As we can see, we our sine plot.  
  
 
|-
 
|-
| 9:01
+
| 09:01
 
| Let us move further in our topic.
 
| Let us move further in our topic.
  
 
|-
 
|-
| 9:06
+
| 09:06
 
| Until now we have been learning about importing modules, now what is a module?
 
| Until now we have been learning about importing modules, now what is a module?
  
 
|-
 
|-
| 9:11
+
| 09:11
 
| A module is simply a file containing Python definitions and statements.  
 
| A module is simply a file containing Python definitions and statements.  
  
 
|-
 
|-
| 9:18
+
| 09:18
 
| Definitions from a module can be imported into other modules or into the main module.
 
| Definitions from a module can be imported into other modules or into the main module.
  
 
|-
 
|-
| 9:24
+
| 09:24
 
| Python has a very rich standard library of modules.  
 
| Python has a very rich standard library of modules.  
  
 
|-
 
|-
| 9:29
+
| 09:29
 
| It is very extensive, offering a wide range of facilities.  
 
| It is very extensive, offering a wide range of facilities.  
  
 
|-
 
|-
| 9:33
+
| 09:33
 
| Some of the standard modules are,
 
| Some of the standard modules are,
  
 
|-
 
|-
| 9:36
+
| 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: gzip, zipfile, tarfile And there are lot more.
+
| 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
 
| 10:13
| Find more information at Python Library reference, <tt>http://docs.python.org/library/</tt>
+
| Find more information at Python Library reference, http://docs.python.org/library/
  
 
|-
 
|-
Line 285: Line 280:
 
|-
 
|-
 
| 10:35
 
| 10:35
| In this tutorial, we have learnt to, 1. Run scripts from command line,
+
| In this tutorial, we have learn't to, 1. Run scripts from command line,
  
 
|-
 
|-
Line 305: 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 333: Line 328:
 
|-
 
|-
 
| 11:49
 
| 11:49
| 1. The option <tt>from pylab import plot</tt> 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. <tt>urllib2</tt> is a part of the python standard library.
+
|urllib2 is a part of the python standard library.
  
 
|-
 
|-
 
| 12:06
 
| 12:06
| 3. Functions <tt>xlim()</tt> and <tt>ylim()</tt> can be imported to the current name-space as, <tt>from pylab import xlim comma  ylim</tt>.
+
|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