Python-3.4.3/C4/Using-Python-Modules/English
Title of script: Using Python Modules
Author: Puneeth, Aditya Palaparthy, Thirumalesh H S,Arun KP
Keywords: Python, Ipython, scripts, import, plot, modules, video tutorial
Visual Cue | Narration |
Show Slide
containing title |
Welcome to the spoken tutorial on Using Python Modules. |
Show Slide
Learning Objectives
|
In this tutorial, you will learn to-
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Prerequisite slide |
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Show Slide
|
First we will learn what is a module.
|
Open text editor
|
Now let us see how to run a Python script from command line.
print inside brackets inside double quotes Hello World
|
Save the script as hello.py | Save this script as hello.py in the current working directory. |
Open terminal and navigate to directory where hello.py was saved | Now let us open the terminal.
|
Type,
python3 hello.py |
Now type, python3 hello.py and press Enter.
|
From here onwards, remember to press the Enter key after typing every command on the terminal. | |
Open the four_plot.py file in text editor and show | Next we will see how to import modules and use them in Python scripts.
|
Highlight the 4 plot command | This code will plot for x, -x, sin of x and xsin of x |
Show slide
Four plot Problem |
When we run this code, we will see the final plot as shown here. |
Type,
python3 four_plot.py |
Now let us run the file four underscore plot.py as a Python script.
python3 four underscore plot.py
function linspace() is not available in the current namespace.
|
Switch to four_plot.py | Let us go back to the file four underscore plot.py. |
Add the line as first line in four_plot.py and save
Highlight linspace |
Add this line as the first line in the script,
|
Save the file | Press Ctrl + S to save the file. |
Type,
python3 four_plot.py |
Now let us run the script again.
|
Open four_plot.py in text editor | Let us edit the four underscore plot.py file again.
from matplotlib.pyplot import * |
Now save the file. | |
Save the file | plot is a function which is available in the matplotlib.pyplot.
|
python3 four_plot.py
|
We will run the script again.
|
Close the window | Let me close this window. |
Show Slide
Import functions
|
We can import only functions which are required from a module as,
instead of, from numpy import * |
Show Slide
Import functions |
In the same way we can import only required functions from matplotlib.pyplot
instead of, from matplotlib.pyplot import * |
slide :
Import Functions |
|
Switch to script 'four_plot.py'
|
Now we will add only required functions from numpy and matplotlib.pyplot
|
Save the file | Let us save the file. |
python3 four_plot.py | We will run the code again in the terminal.
|
There is another way of fixing errors.
| |
Highlight import matplotlib.pyplot as plt
numpy.pi in line 3 and line 10, 11
plt.legend in line 8 plt.annotate in line 9 plt.xlim, plt.ylim in line 10 and 11 plt.show() in line 12
|
Open the file another underscore fix.py
plt.plot() plt.legend() plt.annotate() plt.xlim plt.ylim plt.show()
|
Show Slide
|
Pause the video here.
|
show text for codefiles
|
Open the file sine.py for the solution.
|
Type,
python3 sine.py |
Let us run the code and see the output.
|
Type,
ipython3 |
We can run Python scripts in IPython interpreter also.
|
Type,
%run four_plot.py |
Navigate to the directory where four underscore plot.py is saved and type
|
Show Slide
Python standard library |
Python has a very rich standard library of modules.
|
Show Slide
Python standard library |
Few more libraries
For more information refer to the below link. |
Show Slide
Summary
|
This brings us to the end of this tutorial. Let us summarize.
In this tutorial, we have learnt to,
|
Show Slide
|
Here are some self assessment questions for you to solve
|
Show Slide
|
And the answers,
|
Show Slide Forum | Please post your timed queries in this forum. |
Show Slide Fossee Forum | Please post your general queries on Python in this forum. |
Slide Textbook Companion | FOSSEE team coordinates the TBC project. |
Show Slide Acknowledgment
|
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is Priya from IIT Bombay signing off.
Thanks for watching. |