Python-3.4.3/C4/Writing-Python-Scripts/English
Title of script: Writing Python scripts
Author: Aditya Palaparthy, Arun KP
Keywords: Python, Ipython, python script, import module, name variable, video tutorial
|
|
Show Slide title | Welcome to the spoken tutorial on Writing Python scripts. |
Show Slide
Objectives
|
In this tutorial we will learn,
|
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
Python Modules
|
We can write Python modules to bundle functions.
|
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
print ("Everything is OK") else: print ("The GCD function is wrong") |
This Python module has a function to compute gcd of two numbers.
|
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.
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
“Everything is OK” |
Now we will run the script.
percentage run gcd underscore script.py
|
Switch to the gcd_script.py | It means that the test case checking gcd inside brackets 40 comma 12 equals to 4 is passed.
|
Type,
import sys
|
But first, we will understand what happens when we import a module.
import sys |
Type, sys.path | Now type sys.path |
Highlight the output
|
We can see a list of locations.
|
Type,
import gcd_script
print("gcd of 187 and 391 is", gcd_script.gcd(187, 391))
|
We can import a module present in the current working directory.
import gcd underscore script
|
Highlight Everything is OK
|
We can also see the output “Everything is OK” that we added as test code.
|
Slide:
__name__ variable.
|
Test code should only be executed when we run the Python script independently.
|
Switch to gcd_script.py
after the line return a |
First, we shall look at how to use the variable and then understand how it works.
if double underscore name double underscore double equal to inside double quotes double underscore main double underscore colon
|
Save the file | Save the file. |
Type
%run gcd_script.py
Everything is OK |
Let us run the code.
percentage run gcd underscore script.py
|
Type, import gcd_script
exit |
Now we will check the changes by importing the module.
|
Open terminal and type ipython3 | Open another terminal. Type ipython3. |
Type
import gcd_script |
Now we will import gcd underscore script.py.
import gcd underscore script
|
Open gcd_script.py and highlight the if __name__ == "__main__":
|
The name variable is local to every module.
|
Switch terminal | Switch back to the terminal. |
Type,
from gcd_script import gcd
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))
|
Type the following code which checks whether two numbers are relatively prime.
|
Slide | Every Python file can be run in two ways:
|
Show Slide
Summary slide
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Evaluation |
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
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. |