Difference between revisions of "Python-3.4.3/C4/Writing-Python-Scripts/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''': '''Writing Python scripts ''' '''Author: Aditya Palaparthy, Arun KP''' '''Keywords: Python, Ipython, python script, import module, name variable, vide...")
 
Line 23: Line 23:
 
| 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 we will learn,  
 
| 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 we will learn,  
  
* What is importing  
+
* What is '''importing'''
* Write your own Python modules and
+
* Write your own '''Python modules''' and
 
* Understand the ''double underscore '''''name''' ''double underscore double equal to within double quotes'' ''double underscore '''''main ''double underscore '''''idiom
 
* Understand the ''double underscore '''''name''' ''double underscore double equal to within double quotes'' ''double underscore '''''main ''double underscore '''''idiom
 
 
  
 
|-
 
|-
Line 39: Line 37:
 
* '''IPython 5.1.0''' and
 
* '''IPython 5.1.0''' and
 
* '''Gedit text editor'''
 
* '''Gedit text editor'''
 
 
  
 
|-
 
|-
Line 48: Line 44:
 
| 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  
  
* run basic '''Python '''commands on the '''IPython console '''and
+
* '''Run''' basic '''Python commands''' on the '''IPython console '''and
* use''' python modules.'''
+
* Use''' Python modules.'''
  
 
If not, see the relevant '''Python '''tutorials on this website.
 
If not, see the relevant '''Python '''tutorials on this website.
Line 60: Line 56:
  
  
| 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 write '''Python modules''' to bundle functions.
+
| 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 write '''Python modules''' to bundle '''functions'''.
  
  
Line 67: Line 63:
 
|-
 
|-
 
| 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;"|  
| 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 first write a function and save it in a script.
+
| 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 first write a '''function''' and save it in a '''script'''.
  
 
|-
 
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open an editor and type the following code  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open an '''editor''' and type the following code  
  
 
'''def gcd(a, b):'''
 
'''def gcd(a, b):'''
Line 87: Line 83:
  
 
'''print ("The GCD function is wrong")'''
 
'''print ("The GCD function is wrong")'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open any text editor and type the below code.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open any '''text editor''' and type the below code.
  
  
Line 109: Line 105:
  
 
'''print ("The GCD function is wrong")'''
 
'''print ("The GCD function is wrong")'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| This Python module has a function to compute '''gcd''' of two numbers.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| This '''Python module''' has a '''function''' to compute '''gcd''' of two numbers.
  
  
Line 115: Line 111:
  
  
We have included a test case in the script.
+
We have included a '''test case''' in the '''script'''.
  
  
This '''test case''' will check the '''gcd''' '''function''' every time the script is run.  
+
This '''test case''' will check the '''gcd function''' every time the '''script''' is run.  
  
 
|-
 
|-
| 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 gcd_script.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 '''gcd_script.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 save the file as '''gcd''''' underscore '''''script.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;"| Let us save the file as '''gcd''''' underscore '''''script.py''' in the current '''working directory'''.
  
 
|-
 
|-
Line 131: Line 127:
  
 
'''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;"| Open a new 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 a new '''terminal'''.
  
  
Line 139: Line 135:
  
  
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'''.
  
 
|-
 
|-
Line 150: Line 148:
  
 
“'''Everything is OK'''”
 
“'''Everything is OK'''”
| 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 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;"| Now we will run the '''script'''.
  
  
Line 162: Line 160:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the gcd_script.py
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the gcd_script.py
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| It means that the test case checking '''gcd '''''inside brackets '''''40 '''''comma''''' 12 '''''equals to''''' 4 '''is passed.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| It means that the '''test case''' checking '''gcd '''''inside brackets '''''40 '''''comma''''' 12 '''''equals to''''' 4 '''is passed.
  
  
What if we want to use the '''gcd''' '''function''' in some other '''scripts'''.
+
What if we want to use the '''gcd function''' in some other '''scripts'''?
  
  
This is possible since every '''python''' file can be used as a '''module'''.
+
This is possible since every '''Python''' file can be used as a '''module'''.
  
 
|-
 
|-
Line 199: Line 197:
  
  
This indicates that python searches for a module when it encounters an '''import''' statement.
+
This indicates that '''Python''' searches for a '''module''' when it encounters an '''import statement'''.
  
  
The standard modules are built into python itself.
+
The '''standard modules''' are built into '''Python''' itself.
  
  
Line 211: Line 209:
  
  
It means the '''current working directory''' is also searched.  
+
It means the current '''working directory''' is also searched.  
  
 
|-
 
|-
Line 228: Line 226:
  
 
Highlight '''gcd of 187 and 391 is 17'''
 
Highlight '''gcd of 187 and 391 is 17'''
| 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''' a module present 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;"| We can '''import''' a '''module''' present in the current '''working directory.'''
  
  
Line 236: Line 234:
  
  
Since '''gcd '''''underscore''''' script.py '''is in the '''current working directory''', import will work directly.
+
Since '''gcd '''''underscore''''' script.py '''is in the current '''working directory, import''' will work directly.
  
  
Type the '''print''' statement as shown here.
+
Type the '''print statement''' as shown here.
  
  
We get the gcd of 187 and 391 as output, which is 17
+
We get the '''gcd''' of 187 and 391 as output, which is 17.
  
 
|-
 
|-
Line 255: Line 253:
  
  
The test code is added to check the '''gcd''' function.
+
The '''test code''' is added to check the '''gcd function'''.
  
 
|-
 
|-
Line 264: Line 262:
  
 
Highlight '''__name__'''
 
Highlight '''__name__'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Test code should only be executed when we '''run''' the python '''script''' independently.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Test code''' should only be executed when we '''run''' the '''Python script''' independently.
  
  
To execute the test code when the module is imported to other scripts, we can use ''double underscore '''''name '''''double underscore '''''variable'''.  
+
To execute the '''test code''' when the '''module''' is imported to other '''scripts''', we can use ''double underscore '''''name '''''double underscore '''''variable'''.  
  
  
Hereafter I will call this as '''name''' variable.
+
Hereafter I will call this as '''name variable'''.
  
 
|-
 
|-
Line 279: Line 277:
  
 
after the line '''return a'''  
 
after the line '''return a'''  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First, we shall look at how to use the variable and then understand how it works.  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First, we shall look at how to use the '''variable''' and then understand how it works.  
  
  
Now we will add this variable in the script '''gcd '''''underscore''''' script.py'''
+
Now we will add this '''variable''' in the '''script gcd '''''underscore''''' script.py'''
  
  
Type the following after '''return a''' statement,
+
Type the following after '''return a statement'''  
  
 
'''if '''''double underscore '''''name '''''double underscore double equal to inside double quotes double underscore '''''main '''''double underscore colon''
 
'''if '''''double underscore '''''name '''''double underscore double equal to inside double quotes double underscore '''''main '''''double underscore colon''
Line 305: Line 303:
  
 
'''Everything is OK'''
 
'''Everything is OK'''
| 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.  
+
| 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'''.  
  
  
Line 313: Line 311:
  
  
We can see that the test is executed successfully and we get output as '''Everything is OK'''
+
We can see that the '''test''' is executed successfully and we get output as '''Everything is OK'''
  
 
|-
 
|-
Line 322: Line 320:
  
 
'''exit'''
 
'''exit'''
| 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 check the changes by importing the module.
+
| 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 check the changes by '''importing''' the '''module'''.
  
  
Line 328: Line 326:
  
  
Note that, once a module is imported, it cannot be imported again in an existing '''IPython console. '''
+
Note that, once a '''module''' is imported, it cannot be '''imported''' again in an existing '''IPython console. '''
  
  
Line 335: Line 333:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open terminal and type '''ipython3'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open terminal and type '''ipython3'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open another terminal. Type '''ipython3'''.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open another '''terminal'''. Type '''ipython3'''.
  
 
|-
 
|-
Line 341: Line 339:
  
 
'''import gcd_script'''
 
'''import gcd_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 we will import '''gcd '''''underscore''''' script.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 we will '''import gcd '''''underscore''''' script.py.'''
  
  
Line 349: Line 347:
  
  
We can see that now the test code is not executed.
+
We can see that now the '''test code''' is not executed.
  
  
'''Everything is OK''' is not displayed as output.''' '''
+
'''Everything is OK''' is not displayed as output.  
  
 
|-
 
|-
Line 362: Line 360:
  
  
It is equal to''' main''' only when the file is run as a script.  
+
It is equal to''' main''' only when the file is run as a '''script'''.  
  
  
Hence, all the code under the block, '''if name '''''double equal to within double quotes''''' main '''is executed only when the file is run as a '''python''' '''script'''.  
+
Hence, all the '''code''' under the '''block, if name '''''double equal to within double quotes''''' main '''is executed only when the file is run 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;"| Switch terminal
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch back to 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;"| Switch back to the '''terminal'''.
  
 
|-
 
|-
Line 392: Line 390:
  
 
Highlight '''Yes, 11 and 3 are relatively prime'''
 
Highlight '''Yes, 11 and 3 are relatively prime'''
| 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;"| Type the following code which checks whether two numbers are relatively prime.
+
| 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;"| Type the following '''code''' which checks whether two numbers are relatively prime.
  
  
Line 398: Line 396:
  
  
And use it in our calculations without the test code being executed.
+
And use it in our calculations without the '''test code''' being executed.
  
  
Two numbers are relatively prime when their gcd is equal to one.
+
Two numbers are relatively prime when their '''gcd''' is equal to one.
  
  
Line 408: Line 406:
 
|-
 
|-
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Slide
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Slide
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Every Python file can be run in two ways:
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Every '''Python''' file can be '''run''' in two ways:
 
+
* As an independent stand-alone script or
+
* As a Python module which can be imported by other Python scripts or modules.
+
 
+
  
 +
* As an independent stand-alone '''script''' or
 +
* As a '''Python module''' which can be imported by other '''Python scripts''' or '''modules'''.
  
 
|-
 
|-
Line 425: Line 421:
  
  
In this tutorial, we have learnt to,''' '''
+
In this tutorial, we have learnt to,  
 
+
* Import a module
+
* Use a script as a module
+
* Write test condition using the name variable
+
* Restart IPython3 if a changed module is to be imported again.
+
 
+
  
 +
* '''Import''' a '''module'''
 +
* Use a '''script''' as a '''module'''
 +
* Write '''test condition''' using the '''name variable'''
 +
* Restart '''IPython3''' if a changed '''module''' is to be imported again.
  
 
|-
 
|-
Line 440: Line 434:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve  
  
# Which of the following variables contains the locations to search for''' '''python modules
+
# Which of the following '''variables''' contains the locations to search for '''Python modules'''  
# A module should contain only one function. True or False  
+
# A '''module''' should contain only one '''function'''. True or False  
 
+
 
+
  
 
|-
 
|-
Line 450: Line 442:
  
 
Solutions  
 
Solutions  
 +
| 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.
  
 
+
# '''sys.path''' contains the locations to search for '''Python modules'''.  
 
+
# '''False'''. A '''module''' can contain a wide range of '''functions'''.  
| 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,''' '''
+
 
+
# '''sys.path''' contains the locations to search for python modules.  
+
# '''False'''. A module can contain a wide range of functions.  
+
 
+
 
+
  
 
|-
 
|-

Revision as of 08:39, 23 October 2018

Title of script: Writing Python scripts

Author: Aditya Palaparthy, Arun KP

Keywords: Python, Ipython, python script, import module, name variable, video tutorial


Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Writing Python scripts.
Show Slide

Objectives


In this tutorial we will learn,
  • What is importing
  • Write your own Python modules and
  • Understand the double underscore name double underscore double equal to within double quotes double underscore main double underscore idiom
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
  • Run basic Python commands on the IPython console and
  • Use Python modules.

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

Show Slide

Python Modules


We can write Python modules to bundle functions.


We can then make use of these functions by importing modules to other scripts.

Let us first write a function and save it in a script.
Open an editor and type the following code

def gcd(a, b):

while b:

a, b = b, a%b

return a

if gcd(40, 12) == 4:

print ("Everything is OK")

else:

print ("The GCD function is wrong")

Open any text editor and type the below code.



Highlight def gcd(a, b):

while b:

a, b = b, a%b

return a


Highlight if gcd(40, 12) == 4:

print ("Everything is OK")

else:

print ("The GCD function is wrong")

This Python module has a function to compute gcd of two numbers.


Please take care of the indentation.


We have included a test case in the script.


This test case will check the gcd function every time the script is run.

Save the script as gcd_script.py


Let us save the file as gcd underscore script.py in the current working directory.
Type

ipython3

Open a new terminal.


Type,

ipython3 and press Enter.


From here onwards remember to press the Enter key after typing every command on the terminal.
Type,

%run gcd_script.py


Highlight the output

Everything is OK

Now we will run the script.


Type,

percentage run gcd underscore script.py


We get an output which says “Everything is OK”.

Switch to the gcd_script.py It means that the test case checking gcd inside brackets 40 comma 12 equals to 4 is passed.


What if we want to use the gcd function in some other scripts?


This is possible since every Python file can be used as a module.

Type,

import sys


But first, we will understand what happens when we import a module.


Type,

import sys

Type, sys.path Now type sys.path
Highlight the output


Highlight the empty string


We can see a list of locations.


This indicates that Python searches for a module when it encounters an import statement.


The standard modules are built into Python itself.


Otherwise it is found in system locations like slash usr slash lib slash python3.4 slash


The first item in the list is an empty string.


It means the current working directory is also searched.

Type,

import gcd_script


Highlight Everything is OK


Type,

print("gcd of 187 and 391 is", gcd_script.gcd(187, 391))


Highlight gcd of 187 and 391 is 17

We can import a module present in the current working directory.


Type,

import gcd underscore script


Since gcd underscore script.py is in the current working directory, import will work directly.


Type the print statement as shown here.


We get the gcd of 187 and 391 as output, which is 17.

Highlight Everything is OK


We can also see the output “Everything is OK” that we added as test code.


This is also executed when we imported gcd underscore script.


The test code is added to check the gcd function.

Slide:

__name__ variable.


Highlight __name__

Test code should only be executed when we run the Python script independently.


To execute the test code when the module is imported to other scripts, we can use double underscore name double underscore variable.


Hereafter I will call this as name variable.

Switch to gcd_script.py


Add if __name__ == "__main__":

after the line return a

First, we shall look at how to use the variable and then understand how it works.


Now we will add this variable in the script gcd underscore script.py


Type the following after return a statement

if double underscore name double underscore double equal to inside double quotes double underscore main double underscore colon


Indent the code properly.

Save the file Save the file.
Type

%run gcd_script.py


Highlight

Everything is OK

Let us run the code.


Type,

percentage run gcd underscore script.py


We can see that the test is executed successfully and we get output as Everything is OK

Type, import gcd_script


Type,

exit

Now we will check the changes by importing the module.


We didn’t see any changes.


Note that, once a module is imported, it cannot be imported again in an existing IPython console.


So we will exit the existing IPython console by typing exit.

Open terminal and type ipython3 Open another terminal. Type ipython3.
Type

import gcd_script

Now we will import gcd underscore script.py.


Type,

import gcd underscore script


We can see that now the test code is not executed.


Everything is OK is not displayed as output.

Open gcd_script.py and highlight the if __name__ == "__main__":


block in the file gcd_script.py

The name variable is local to every module.


It is equal to main only when the file is run as a script.


Hence, all the code under the block, if name double equal to within double quotes main is executed only when the file is run as a Python script.

Switch terminal Switch back to the terminal.
Type,

from gcd_script import gcd


def check_relative_prime(a,b):

if gcd(a,b)==1:

print("Yes, %d and %d are relatively prime" %(a,b))

else:

print("No, %d and %d are not relatively prime" %(a,b))


check_relative_prime(11,3)


Highlight Yes, 11 and 3 are relatively prime

Type the following code which checks whether two numbers are relatively prime.


We have imported gcd function from gcd underscore script.


And use it in our calculations without the test code being executed.


Two numbers are relatively prime when their gcd is equal to one.


So we get output as Yes, 11 and 3 are relatively prime.

Slide Every Python file can be run in two ways:
  • As an independent stand-alone script or
  • As a Python module which can be imported by other Python scripts or modules.
Show Slide

Summary slide


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


In this tutorial, we have learnt to,

  • Import a module
  • Use a script as a module
  • Write test condition using the name variable
  • Restart IPython3 if a changed module is to be imported again.
Show Slide

Evaluation

Here are some self assessment questions for you to solve
  1. Which of the following variables contains the locations to search for Python modules
  2. A module should contain only one function. True or False
Show Slide


Solutions

And the answers.
  1. sys.path contains the locations to search for Python modules.
  2. False. A module can contain a wide range of functions.
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

Acknowledgement

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Show Slide Thank You This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst