Python-3.4.3/C4/Using-Python-Modules/English-timed

From Script | Spoken-Tutorial
Revision as of 15:43, 10 June 2019 by Pratik kamble (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Using Python Modules.
00:06 In this tutorial, you will learn to-

Execute Python scripts from command line

Use import in scripts and

Import numpy and matplotlib.pyplot modules

00:22 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

Python 3.4.3

IPython 5.1.0 and

Gedit text editor

00:40 To practise this tutorial, you should know how to

Use plot interactively

Embellish and save a plot

00:50 If not, see the relevant Python tutorials on this website.
00:55 First we will learn what is a module.
00:58 A module is a file containing Python definitions and statements.
01:04 Modules are used to break down large programs into small manageable and organized files.
01:12 Definitions from a module can be imported into other modules or to the main module.
01:19 Now let us see how to run a Python script from command line.
01:24 Open any text editor and type, print inside brackets inside double quotes Hello World
01:33 We have created a simple Python script to print Hello World.
01:38 Save this script as hello.py in the current working directory.
01:44 Now let us open the terminal.

Then navigate to the directory where hello.py is saved.

01:52 Now type, python3 hello.py and press Enter.

It executes the script and gives the output as Hello World.

02:04 From here onwards, remember to press the Enter key after typing every command on the terminal.
02:11 Next we will see how to import modules and use them in Python scripts.
02:17 Let me open the file four underscore plot.py in the text editor.
02:23 This file is available in the Code files link of this tutorial. You can download and use it.
02:31 This code will plot for x, -x, sin of x and xsin of x
02:38 When we run this code, we will see the final plot as shown here.
02:43 Now let us run the file four underscore plot.py as a Python script.
02:49 Type, python3 four underscore plot.py
02:55 It gives an error linspace() is not defined.
02:59 It means that, function linspace() is not available in the current namespace.
03:05 A namespace is a system for making all the names in a program to be unique.
03:11 Let us go back to the file four underscore plot.py.
03:16 Add this line as the first line in the script, from numpy import asterisk
03:24 When we use asterisk in imports, all the functions and constants are imported from numpy module.
03:32 linspace is a function available in numpy.
03:36 Press Ctrl + S to save the file.
03:40 Now let us run the script again. Now it gave another NameError: name ‘plot’ is not defined.
03:49 Let us edit the four underscore plot.py file again.
03:54 Add the following as the second line in our script. from matplotlib.pyplot import *
04:04 Now save the file.
04:06 plot is a function which is available in the matplotlib.pyplot.
04:12 We will run the script again. We get the output now.
04:19 We imported all the required modules using the keyword import.
04:24 Let me close this window.
04:28 We can import only functions which are required from a module as, from numpy import linspace, pi, sin
04:37 instead of, from numpy import *
04:44 In the same way we can import only required functions from matplotlib.pyplot

instead of, from matplotlib.pyplot import *

04:59 It is always good to use function names instead of asterisk.
05:03 If we use asterisk to import from a particular module, all the functions will be imported.
05:11 It replaces some existing functions with the same name in the namespace.
05:17 Now we will add only required functions from numpy and matplotlib.pyplot
05:25 Let us save the file.
05:27 We will run the code again in the terminal.
05:32 Here, we got the plots for x, -x, sin of x and xsin of x in a single figure.
05:40 In this method we imported all the required functions to the current namespace.
05:46 There is another way of fixing errors. Let us see that.
05:51 Open the file another underscore fix.py
05:56 This file is also available in the Code files link of this tutorial.
06:01 Notice that we are going to use the name plt instead of matplotlib.pyplot.
06:08 plt is used as alias to the module matplotlib.pyplot
06:14 Now, we use numpy.pi instead of just pi as we did in four underscore plot.py.
06:22 Plot functions are called as

plt.plot()

plt.legend()

plt.annotate()

plt.xlim

plt.ylim

and plt.show()

06:43 The advantage is that function names in imported modules do not get added to the current namespace.
06:51 In order to use a function in an imported module, we need to mention module-name.function-name
06:59 Pause the video here.Try this exercise and then resume the video.
07:05 Write a python script to plot a sine wave from -2pi to 2pi.
07:12 Open the file sine.py for the solution. This file is also available in the Code files link. In the first line we import the required functions.
07:27 The functions linspace, sin and constant pi are imported from the module numpy.
07:34 We import the functions plot, legend, show, title, xlabel and ylabel from matplotlib.pyplot.
07:44 And the rest of the code is to generate the plot.
07:48 Let us run the code and see the output.
07:52 In the terminal, type python3 sine.py
07:58 Now we can see our sine plot. Close the terminal.Let us move further in our topic.
08:08 We can run Python scripts in IPython interpreter also.
08:13 Open another terminal and start the ipython interpreter by typing ipython3
08:22 Navigate to the directory where four underscore plot.py is saved and type percentage run four underscore plot.py
08:35 As before we can see the output with 4 plots.
08:40 Python has a very rich standard library of modules.
08:45 Some of the standard modules are,

for Math: math, random

for Internet access: urllib2, smtplib

for System and Command line arguments: sys

09:01 Few more libraries

for Operating system interface: os

for regular expression: re

for compression: gzip, zipfile, tarfile

For more information refer to the below link.

09:20 This brings us to the end of this tutorial. Let us summarize.
09:26 In this tutorial, we have learnt to,

Run scripts from command line

Import modules by specifying the module name followed by an asterisk

09:38 Import only the required functions from modules by specifying the function name

Use Python standard library.

09:47 Here are some self assessment questions for you to solve

1. Which among the below is the most correct?

2. How the functions xlim() and ylim() can be imported to the current namespace?

10:02 And the answers,

1. The option from matplotlib.pyplot import plot is the most correct one.

Because plot is a function of matplotlib.pyplot module.

10:18 2. Functions xlim() and ylim() can be imported to the current namespace as, from matplotlib.pyplot import xlim, ylim
10:32 Please post your timed queries in this forum.
10:36 Please post your general queries on Python in this forum.
10:41 FOSSEE team coordinates the TBC project.
10:45 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.For more details, visit this website.
10:56 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Pratik kamble