Difference between revisions of "Python-3.4.3/C4/Using-Python-Modules/English"
(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...") |
Nancyvarkey (Talk | contribs) |
||
(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''' |
* Use '''import''' in '''scripts '''and | * Use '''import''' in '''scripts '''and | ||
− | * ''' | + | * 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 |
− | * | + | * 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 | + | 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 | + | '''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 | + | 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 ''' | + | 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''' | + | | 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 | + | | 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 ''' | + | | 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''' | + | 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: | + | 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 *''' | ||
− | + | |- | |
− | + | | 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;"| | + | | 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 | + | | 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 | + | 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 | + | | 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 | + | | 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 ''' | + | This file is also available in the '''Code files''' link of this tutorial. |
Line 328: | Line 329: | ||
− | '''plt '''is used as alias to the | + | '''plt '''is used as alias to the '''module matplotlib.pyplot''' |
− | Now, we use '''numpy.pi '''instead | + | Now, we use '''numpy.pi '''instead of just''' pi '''as we did in four'' underscore ''plot.py. |
− | '''Plot functions '''are | + | '''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 | + | Write a '''python script''' to '''plot''' a '''sine wave''' from''' -2pi '''to''' 2pi'''. |
|- | |- | ||
Line 375: | Line 376: | ||
− | This file is also available in the ''' | + | This file is also available in the '''Code files''' link. |
Line 381: | Line 382: | ||
− | The | + | 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 | + | 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''' | + | 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 | + | | 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 | + | * Run '''scripts''' from '''command line''' |
− | * '''Import | + | * '''Import modules''' by specifying the '''module''' name followed by an '''asterisk''' |
− | * '''Import''' only the required '''functions''' from '''modules''' by specifying the '''function | + | * '''Import''' only the required '''functions''' from '''modules''' by specifying the '''function''' name |
− | * Use ''' | + | * 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-
|
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. |