| Show Slide 1
Containing title, name of the production team along with the logo of MHRD
|Hello friends and welcome to the tutorial on "Writing Python scripts".|
| Show Slide 2
| At the end of this tutorial, you will be able to,
| Show Slide 3
|Before beginning this tutorial,we would suggest you to complete the tutorial on "Using Python modules".|
|Often we will have to reuse the code that we have written. We do that by writing functions. Functions are bundled into packages and are imported as and when required in other scripts.|
| Open an editor and start typing out the following code
def gcd(a, b): while b: a, b = b, a%b return a
|Let us first write a function that computes the gcd of two numbers and save it in a script. Open an editor and type the code. Please take care of the indentation.|
| Add the following lines to the script
if gcd(40, 12) == 4: print "Everything OK" else: print "The GCD function is wrong"
|We shall write a test function in the script that tests the gcd function every time the script is run.|
| Show Slide 4
Containing GCD code Keep open for sometime and then continue
|Save the script as gcd_script.py||Let us save the file as gcd_script.py in /home/fossee/gcd_script.py|
| Open a terminal
|We shall run the script by typing|
| We can see that the script is executed and everything is fine.
What if we want to use the gcd function in some of our other scripts. This is also possible since every python file can be used as a module.
But first, we shall understand what happens when you import a module.
| Open another terminal and type ipython
import sys sys.path
|Open IPython and type|
| This is a list of locations where python searches for a module when it encounters an import statement.
Hence, when we just did import sys, python searches for a file named sys.py or a folder named sys in all these locations one by one, until it finds one.
We can place our script in any one of these locations and can import it.
The first item in the list is an empty string which means the current working directory is also searched.
Alternatively, we can also import the module if we are working in same directory where the script exists.
|Close the current terminal|
| Switch to the first terminal
|Since we are in /home/fossee, we can simply do|
| We can see that the gcd_script is imported. But the test code that we added at the end of the file is also executed.
But we want the test code to be executed only when the file is run as a python script and not when it is imported.
This is possible by using __name__ variable.
First, we shall look at how to use the idiom and then understand how it works.
| Show Slide 5
, __name__ variable
|Go to the file and add this line as the beginning of the code and indent the code accordingly.|
| Switch to gcd_script.py and add this line after the
line return a
Definition list ends without a blank line; unexpected unindent.
if __name__ == "__main__":
| Switch back to the terminal
|Let us first run the code.|
|import gcd_script|| We can see that the test runs successfully.
Now we shall import the file
| We see that now the test code is not executed.
The __name__ variable is local to every module and it is equal to __main__ only when the file is run as a script.
Hence, all the code that goes in to the if block, if __name__ == "__main__": is executed only when the file is run as a python script.
| Show Slide 6
| This brings us to the end of the tutorial. In this tutorial, we have learnt to,
| Show Slide 7
Self assessment questions slide
| Here are some self assessment questions for you to solve
Enumerated list ends without a blank line; unexpected unindent.
def show(x): print x show("Hello World") if __name__ == "__main__": show("Hello Test") How do you use the ``show`` function after doing ``import utils`` - utils.show("hey") - show("hey") - utils.py.show("hey") - utils.py.show.py("hey")
| Show Slide 8
Solution of self assessment questions on slide
| And the answers,
3. After doing import utils, we can use the function show() as,
| Show Slide 9
|Hope you have enjoyed this tutorial and found it useful. Thank you!|