Difference between revisions of "Python-3.4.3/C4/Using-Python-Modules/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''': '''Using Python Modules ''' '''Author: Puneeth, Aditya Palaparthy, Thirumalesh H S,Arun KP''' '''Keywords: Python, Ipython, scripts, import, plot, mod...")
 
 
(One intermediate revision by one other user not shown)
Line 19: Line 19:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Show Slide  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Show Slide  
  
Objectives  
+
Learning Objectives  
  
  
Line 25: Line 25:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, you will learn to-  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In this tutorial, you will learn to-  
  
* '''Execute''' '''python''' '''scripts''' from '''command''' '''line'''
+
* Execute '''Python scripts''' from '''command line'''
 
* Use '''import''' in '''scripts '''and
 
* Use '''import''' in '''scripts '''and
* '''Import numpy '''and''' matplotlib.pyplot modules'''
+
* Import '''numpy '''and''' matplotlib.pyplot modules'''
 
+
 
+
  
 
|-
 
|-
Line 41: Line 39:
 
* '''IPython 5.1.0 '''and
 
* '''IPython 5.1.0 '''and
 
* '''Gedit text editor'''
 
* '''Gedit text editor'''
 
 
  
 
|-
 
|-
Line 50: Line 46:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to  
  
* use '''plot''' interactively
+
* Use '''plot''' interactively
* '''embellish''' and save a '''plot'''
+
* Embellish and save a '''plot'''
  
 
If not, see the relevant '''Python''' tutorials on this website.
 
If not, see the relevant '''Python''' tutorials on this website.
Line 63: Line 59:
  
  
A '''module''' is a file containing '''Python''' '''definitions''' and '''statements'''.  
+
A '''module''' is a file containing '''Python definitions''' and '''statements'''.  
  
  
Line 69: Line 65:
  
  
'''Definitions''' from a '''module''' can be '''imported''' into other '''modules''' or to the '''main''' '''module'''.  
+
'''Definitions''' from a '''module''' can be '''imported''' into other '''modules''' or to the '''main module'''.  
  
 
|-
 
|-
Line 79: Line 75:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us see how to run a Python script from command line.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us see how to run a '''Python script''' from '''command line'''.
  
  
Open any '''text''' '''editor''' and type,  
+
Open any '''text editor''' and type,  
  
 
'''print''''' inside brackets inside double quotes '''''Hello World'''
 
'''print''''' inside brackets inside double quotes '''''Hello World'''
  
  
We have created a simple '''python''' script to print '''hello''' '''world'''.
+
We have created a simple '''Python script''' to print '''Hello World'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the script as '''hello.py'''  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the script as '''hello.py'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save this '''script''' as '''hello.py''' in the current working directory.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save this '''script''' as '''hello.py''' in the current '''working directory'''.
  
 
|-
 
|-
Line 109: Line 105:
 
It executes the '''script''' and gives the output as '''Hello World.'''
 
It executes the '''script''' and gives the output as '''Hello World.'''
  
 
+
|-
From here onwards, remember to press the Enter key after typing every command on the terminal.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"|
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| From here onwards, remember to press the '''Enter''' key after typing every '''command''' on the '''terminal'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open the '''four_plot.py''' '''file''' in '''text''' '''editor''' and show  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open the '''four_plot.py''' '''file''' in '''text''' '''editor''' and show  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will see how to '''import''' modules and use them in python scripts.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will see how to '''import modules''' and use them in '''Python scripts.'''
  
  
Let me open the file''' four''''' underscore '''''plot.py''' in the text editor.
+
Let me open the file''' four''''' underscore '''''plot.py''' in the '''text editor.'''
  
  
Line 127: Line 124:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Highlight the 4 plot command
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Highlight the 4 plot command
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| This code will plot for '''x, -x, sin of x '''and''' xsin of x'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| This '''code''' will '''plot''' for '''x, -x, sin of x '''and''' xsin of x'''
  
 
|-
 
|-
Line 133: Line 130:
  
 
Four plot Problem
 
Four plot Problem
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.053cm;padding-right:0.191cm;"| When we run this''' '''code, we will see the final plot as shown here.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.053cm;padding-right:0.191cm;"| When we '''run''' this '''code''', we will see the final '''plot''' as shown here.
  
 
|-
 
|-
Line 139: Line 136:
  
 
'''python3''' '''four_plot.py'''  
 
'''python3''' '''four_plot.py'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us '''run''' the file '''four '''''underscore''''' plot.py''' as a '''python''' script.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us '''run''' the file '''four '''''underscore''''' plot.py''' as a '''Python script'''.  
  
  
Line 168: Line 165:
  
 
Highlight '''linspace'''
 
Highlight '''linspace'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Add this line as the first line in the script,  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Add this line as the first line in the '''script''',  
  
  
Line 174: Line 171:
  
  
When we use''' asterisk''' in '''imports, '''all the '''functions''' and '''constants''' are imported from '''numpy''' module.  
+
When we use''' asterisk''' in '''imports, '''all the '''functions''' and '''constants''' are imported from '''numpy module'''.  
  
  
'''linspace''' is a function available in '''numpy.'''
+
'''linspace''' is a '''function''' available in '''numpy.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the file
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the file
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Press Ctrl + S to save the file.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Press '''Ctrl + S''' to save the file.
  
 
|-
 
|-
Line 187: Line 184:
  
 
'''python3 four_plot.py '''
 
'''python3 four_plot.py '''
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us run the script again,
+
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now let us run the '''script''' again.
  
  
Now it gave another '''NameError:''' '''name''' ‘'''plot’ is not defined.'''
+
Now it gave another '''NameError: name ‘plot’ is not defined.'''
  
 
|-
 
|-
Line 197: Line 194:
  
  
Add the following as the second line in our script.
+
Add the following as the second line in our '''script'''.
  
 
'''from matplotlib.pyplot import *'''
 
'''from matplotlib.pyplot import *'''
  
 
+
|-
'''plot''' is a function which is available in the '''matplotlib.pyplot. '''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"|
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now save the file.  
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the file
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Save the file
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now save the file.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''plot''' is a '''function''' which is available in the '''matplotlib.pyplot. '''
 +
  
 
|-
 
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''python3''' '''four_plot.py'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''python3 four_plot.py'''
  
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We will run the '''script.'''  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We will run the '''script''' again.
  
  
Line 219: Line 218:
  
  
We '''imported''' all the required '''modules''' using the '''keyword''' '''import'''.
+
We '''imported''' all the required '''modules''' using the '''keyword import'''.
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Close the window
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Close the window
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let me close the window.
+
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let me close this window.
  
 
|-
 
|-
Line 232: Line 231:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can import only functions which are required from a module as,
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can import only '''functions''' which are required from a '''module''' as,
  
  
Line 241: Line 240:
 
'''from numpy import *'''  
 
'''from numpy import *'''  
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Show Slide
  
In the same way we can import only required functions from '''matplotlib.pyplot'''
+
Import functions
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In the same way we can import only required '''functions''' from '''matplotlib.pyplot'''
  
 
instead of,
 
instead of,
Line 252: Line 254:
  
 
Import Functions
 
Import Functions
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| * It is always good to use '''function''' '''names''' instead of '''asterisk'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"|  
 +
* It is always good to use '''function''' names instead of '''asterisk'''.  
 
* If we use '''asterisk''' to '''import''' from a particular '''module,''' all the '''functions '''will be imported.
 
* If we use '''asterisk''' to '''import''' from a particular '''module,''' all the '''functions '''will be imported.
 
* It replaces some existing '''functions''' with the same name in the '''namespace'''.
 
* It replaces some existing '''functions''' with the same name in the '''namespace'''.
 
 
  
 
|-
 
|-
Line 272: Line 273:
  
 
'''from matplotlib.pyplot import xlim, ylim, title, show'''
 
'''from matplotlib.pyplot import xlim, ylim, title, show'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now we will add only required functions from '''numpy''' and '''matplotlib.pyplot'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now we will add only required '''functions''' from '''numpy''' and '''matplotlib.pyplot'''
  
  
Line 283: Line 284:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''python3''' '''four_plot.py'''  
 
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''python3''' '''four_plot.py'''  
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We will run the code again in the terminal.
+
| style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We will run the code again in the '''terminal'''.
  
  
Here, we got the '''plots '''for''' x,-x,sin of x '''and''' xsin of x''' in a single '''figure'''.  
+
Here, we got the '''plots '''for''' x, -x, sin of x '''and''' xsin of x''' in a single '''figure'''.  
  
  
In this method we '''imported''' the required '''functions''' to the current '''namespace'''.  
+
In this method we '''imported''' all the required '''functions''' to the current '''namespace'''.  
  
 
|-
 
|-
Line 322: Line 323:
  
  
This file is also available in the '''code files''' link of this tutorial.
+
This file is also available in the '''Code files''' link of this tutorial.
  
  
Line 328: Line 329:
  
  
'''plt '''is used as alias to the module '''matplotlib.pyplot'''
+
'''plt '''is used as alias to the '''module matplotlib.pyplot'''
  
  
Now, we use '''numpy.pi '''instead''' '''of''' '''just''' pi '''as we did in four'' underscore ''plot.py.
+
Now, we use '''numpy.pi '''instead of just''' pi '''as we did in four'' underscore ''plot.py.
  
  
'''Plot functions '''are''' '''called''' '''as''' '''
+
'''Plot functions '''are called as  
  
 
'''plt.plot()'''
 
'''plt.plot()'''
Line 349: Line 350:
  
  
The advantage is that function names in imported modules do not get added to the current '''namespace'''.
+
The advantage is that '''function''' names in '''imported modules''' do not get added to the current '''namespace'''.
  
  
In order to use a function in an imported module, we need to mention '''module-name.function-name'''
+
In order to use a '''function''' in an '''imported module''', we need to mention '''module-name.function-name'''
  
 
|-
 
|-
Line 365: Line 366:
  
  
Write a '''python script''' to '''plot''' a '''sine''' '''wave''' from''' -2pi '''to''' 2pi'''.  
+
Write a '''python script''' to '''plot''' a '''sine wave''' from''' -2pi '''to''' 2pi'''.  
  
 
|-
 
|-
Line 375: Line 376:
  
  
This file is also available in the '''code files''' link.
+
This file is also available in the '''Code files''' link.
  
  
Line 381: Line 382:
  
  
The functions''' linspace, sin '''and''' constant pi''' are imported from the''' module numpy'''.
+
The '''functions linspace, sin '''and''' constant pi''' are imported from the''' module numpy'''.
  
  
Line 393: Line 394:
  
 
'''python3''' '''sine.py'''
 
'''python3''' '''sine.py'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us run the code and see the output.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us run the '''code''' and see the output.
  
  
In the terminal, type
+
In the '''terminal''', type
  
  
Line 402: Line 403:
  
  
Now we can see our '''sine''' '''plot'''.  
+
Now we can see our '''sine plot'''.  
  
  
Close the terminal.
+
Close the '''terminal'''.
  
  
Line 414: Line 415:
  
 
'''ipython3'''
 
'''ipython3'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can run Python scripts in '''IPython interpreter '''also'''.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can run '''Python scripts''' in '''IPython interpreter '''also.
  
  
Open another terminal and start the '''ipython''' interpreter by typing
+
Open another '''terminal''' and start the '''ipython interpreter''' by typing
  
  
Line 432: Line 433:
  
  
As before we can see the output with 4 plots.
+
As before we can see the output with 4 '''plots'''.
  
 
|-
 
|-
Line 438: Line 439:
  
 
'''Python standard library'''  
 
'''Python standard library'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Python''' has a very rich '''standard''' '''library''' of '''modules'''.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Python''' has a very rich '''standard library''' of '''modules'''.  
  
  
Line 446: Line 447:
 
* for''' Internet access: urllib2, smtplib'''
 
* for''' Internet access: urllib2, smtplib'''
 
* for''' System and Command line arguments: sys '''
 
* for''' System and Command line arguments: sys '''
 
 
  
 
|-
 
|-
Line 453: Line 452:
  
 
'''Python standard library'''  
 
'''Python standard library'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Few more libraries
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Few more '''libraries'''
  
 
* for''' Operating system interface: os '''
 
* for''' Operating system interface: os '''
Line 473: Line 472:
  
  
* Run '''scripts''' from '''command''' '''line'''  
+
* Run '''scripts''' from '''command line'''  
* '''Import''' '''modules''' by specifying the '''module''' '''name''' followed by an '''asterisk'''  
+
* '''Import modules''' by specifying the '''module''' name followed by an '''asterisk'''  
* '''Import''' only the required '''functions''' from '''modules''' by specifying the '''function''' '''name'''
+
* '''Import''' only the required '''functions''' from '''modules''' by specifying the '''function''' name
* Use '''python''' '''standard''' '''library'''.  
+
* Use '''Python standard library'''.  
 
+
 
+
  
 
|-
 
|-
Line 492: Line 489:
 
# Which among the below is the most correct?
 
# Which among the below is the most correct?
 
# How the functions '''xlim() '''and '''ylim'''() can be imported to the current '''namespace'''?  
 
# How the functions '''xlim() '''and '''ylim'''() can be imported to the current '''namespace'''?  
 
 
  
 
|-
 
|-
Line 505: Line 500:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,  
  
# The option '''from matplotlib.pyplot import plot''' is the most correct one. Because '''plot''' is a '''function''' of '''matplotlib.pyplot''' '''module'''.  
+
# The option '''from matplotlib.pyplot import plot''' is the most correct one.  
 +
#:Because '''plot''' is a '''function''' of '''matplotlib.pyplot''' '''module'''.  
 
# Functions '''xlim'''() and '''ylim'''() can be '''imported''' to the current '''namespace''' as, '''from matplotlib.pyplot import xlim, ylim'''  
 
# Functions '''xlim'''() and '''ylim'''() can be '''imported''' to the current '''namespace''' as, '''from matplotlib.pyplot import xlim, ylim'''  
 
 
  
 
|-
 
|-

Latest revision as of 08:05, 23 October 2018

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-
  • Execute Python scripts from command line
  • Use import in scripts and
  • Import numpy and matplotlib.pyplot modules
Show Slide

System Specifications

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
Show Slide

Prerequisite slide

To practise this tutorial, you should know how to
  • Use plot interactively
  • Embellish and save a plot

If not, see the relevant Python tutorials on this website.

Show Slide


What is a module?

First we will learn what is a module.


A module is a file containing Python definitions and statements.


Modules are used to break down large programs into small manageable and organized files.


Definitions from a module can be imported into other modules or to the main module.

Open text editor


Type, print ("Hello World")


Now let us see how to run a Python script from command line.


Open any text editor and type,

print inside brackets inside double quotes Hello World


We have created a simple Python script to print 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.


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

Type,

python3 hello.py

Now type, python3 hello.py and press Enter.


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

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.


Let me open the file four underscore plot.py in the text editor.


This file is available in the Code files link of this tutorial.


You can download and use it.

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.


Type,

python3 four underscore plot.py


It gives an error linspace() is not defined.


It means that,

function linspace() is not available in the current namespace.


A namespace is a system for making all the names in a program to be unique.

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


from numpy import *

Highlight linspace

Add this line as the first line in the script,


from numpy import asterisk


When we use asterisk in imports, all the functions and constants are imported from numpy module.


linspace is a function available in numpy.

Save the file Press Ctrl + S to save the file.
Type,

python3 four_plot.py

Now let us run the script again.


Now it gave another NameError: name ‘plot’ is not defined.

Open four_plot.py in text editor Let us edit the four underscore plot.py file again.


Add the following as the second line in our script.

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.


We get the output now.


We imported all the required modules using the keyword import.

Close the window Let me close this window.
Show Slide

Import functions


We can import only functions which are required from a module as,


from numpy import linspace, pi, sin

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

  • It is always good to use function names instead of asterisk.
  • If we use asterisk to import from a particular module, all the functions will be imported.
  • It replaces some existing functions with the same name in the namespace.
Switch to script 'four_plot.py'


delete the first two lines and add the following


from numpy import linspace, pi, sin


from matplotlib.pyplot import plot, legend, annotate


from matplotlib.pyplot import xlim, ylim, title, show

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.


Here, we got the plots for x, -x, sin of x and xsin of x in a single figure.


In this method we imported all the required functions to the current namespace.

There is another way of fixing errors.


Let us see that.

Highlight import matplotlib.pyplot as plt


Highlight

numpy.pi in line 3 and line 10, 11


plt.plot in line 4

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


This file is also available in the Code files link of this tutorial.


Notice that we are going to use the name plt instead of matplotlib.pyplot.


plt is used as alias to the module matplotlib.pyplot


Now, we use numpy.pi instead of just pi as we did in four underscore plot.py.


Plot functions are called as

plt.plot()

plt.legend()

plt.annotate()

plt.xlim

plt.ylim

plt.show()


The advantage is that function names in imported modules do not get added to the current namespace.


In order to use a function in an imported module, we need to mention module-name.function-name

Show Slide


Exercise 1

Pause the video here.


Try this exercise and then resume the video.


Write a python script to plot a sine wave from -2pi to 2pi.

show text for codefiles


Open sine.py and show it

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.


The functions linspace, sin and constant pi are imported from the module numpy.


We import the functions plot, legend, show, title, xlabel and ylabel from matplotlib.pyplot.


And the rest of the code is to generate the plot.

Type,

python3 sine.py

Let us run the code and see the output.


In the terminal, type


python3 sine.py


Now we can see our sine plot.


Close the terminal.


Let us move further in our topic.

Type,

ipython3

We can run Python scripts in IPython interpreter also.


Open another terminal and start the ipython interpreter by typing


ipython3

Type,

%run four_plot.py

Navigate to the directory where four underscore plot.py is saved and type


percentage run four underscore plot.py


As before we can see the output with 4 plots.

Show Slide

Python standard library

Python has a very rich standard library of modules.


Some of the standard modules are,

  • for Math: math, random
  • for Internet access: urllib2, smtplib
  • for System and Command line arguments: sys
Show Slide

Python standard library

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.

Show Slide

Summary


This brings us to the end of this tutorial. Let us summarize.

In this tutorial, we have learnt to,


  • Run scripts from command line
  • Import modules by specifying the module name followed by an asterisk
  • Import only the required functions from modules by specifying the function name
  • Use Python standard library.
Show Slide


Evaluation


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?
Show Slide


Solutions


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.
  2. Functions xlim() and ylim() can be imported to the current namespace as, from matplotlib.pyplot import xlim, ylim
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.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst