Python-3.4.3/C4/Writing-Python-Scripts/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Writing Python scripts.
00:06 In this tutorial we will learn, What is importing ?
00:12 Write your own Python modules and
00:15 Understand the double underscore name double underscore double equal to within double quotes double underscore main double underscore idiom
00:25 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

00:34 Python 3.4.3
00:37 IPython 5.1.0 and Gedit text editor
00:44 To practise this tutorial, you should know how to Run basic Python commands on the IPython console
00:53 And Use Python modules.
00:56 If not, see the relevant Python tutorials on this website.
01:02 We can write Python modules to bundle functions.
01:07 We can then make use of these functions by importing modules to other scripts.
01:14 Let us first write a function and save it in a script.
01:19 Open any text editor and type the below code.
01:24 This Python module has a function to compute gcd of two numbers. Please take care of the indentation.
01:33 We have included a text case in the script. This text case will check the GCD function everytime the script is run.
01:43 Let us save the file as gcd underscore script.py in the current working directory.
01:50 Open a new terminal. Type, ipython3 and press Enter.
01:59 From here onwards remember to press the Enter key after typing every command on the terminal.
02:06 Now we will run the script. Type, percentage run gcd underscore script dot py
02:16 We get an output which says “Everything is OK”.
02:21 It means that the test case checking gcd inside brackets 40 comma 12 equals to 4 is passed.
02:30 What if we want to use the gcd function in some other scripts?
02:35 This is possible since every Python file can be used as a module.
02:41 But first, we will understand what happens when we import a module.
02:47 Type, import sys
02:51 Now type sys.path
02:55 We can see a list of locations. This indicates that Python searches for a module when it encounters an import statement.
03:05 The standard modules are built into Python itself.
03:09 Otherwise it is found in system locations like slash usr slash lib slash python3.4 slash
03:20 The first item in the list is an empty string.
03:24 It means the current working directory is also searched.
03:29 We can import a module present in the current working directory. Type, import gcd underscore script
03:39 Since gcd underscore script dot py is in the current working directory, import will work directly.
03:48 Type the print statement as shown here.
03:53 We get the gcd of 187 and 391 as output, which is 17.
04:00 We can also see the output “Everything is OK” that we added as test code.
04:07 This is also executed when we imported gcd underscore script.
04:13 The test code is added to check the gcd function.
04:18 Test code should only be executed when we run the Python script independently.
04:24 To execute the test code when the module is imported to other scripts, we can use double underscore name double underscore variable.
04:34 Hereafter I will call this as name variable.
04:38 First, we shall look at how to use the variable and then understand how it works.
04:45 Now we will add this variable in the script gcd underscore script dot py
04:52 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

05:07 Indent the code properly.
05:10 Save the file.
05:12 Let us run the code. Type, percentage run gcd underscore script.py
05:21 We can see that the test is executed successfully and we get output as Everything is OK
05:28 Now we will check the changes by importing the module.
05:33 Type, import gcd_script

We didn’t see any changes.

05:41 Note that, once a module is imported, it cannot be imported again in an existing IPython console.
05:49 So we will exit the existing IPython console by typing exit.
05:56 Open another terminal. Type ipython3.
06:04 Now we will import gcd underscore script.py. Type, import gcd underscore script
06:14 We can see that now the test code is not executed. Everything is OK is not displayed as output.
06:22 The name variable is local to every module. It is equal to main only when the file is run as a script.
06:31 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.
06:43 Switch back to the terminal.
06:46 Type the following code which checks whether two numbers are relatively prime.
06:54 We have imported gcd function from gcd underscore script.
07:00 And use it in our calculations without the test code being executed.
07:06 Two numbers are relatively prime when their gcd is equal to one.
07:12 So we get output as Yes, 11 and 3 are relatively prime.
07:18 Every Python file can be run in two ways: As an independent stand-alone script or
07:26 As a Python module which can be imported by other Python scripts or modules.
07:33 This brings us to the end of this tutorial. Let us summarize.
07:37 In this tutorial, we have learnt to, Import a module.
07:44 Use a script as a module
07:47 Write test condition using the name variable and
07:52 Restart IPython3 if a changed module is to be imported again
07:57 Here are some self assessment questions for you to solve

First. Which of the following variables contains the locations to search for Python modules

08:08 Second. A module should contain only one function. True or False
08:14 And the answers. First. sys.path contains the locations to search for Python modules.
08:22 Second. False. A module can contain a wide range of functions.
08:28 Please post your timed queries in this forum.
08:32 Please post your general queries on Python in this forum.
08:37 FOSSEE team coordinates the TBC project.
08:41 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.
08:51 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya