<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python-3.4.3%2FC4%2FWriting-Python-Scripts%2FEnglish-timed</id>
		<title>Python-3.4.3/C4/Writing-Python-Scripts/English-timed - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python-3.4.3%2FC4%2FWriting-Python-Scripts%2FEnglish-timed"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python-3.4.3/C4/Writing-Python-Scripts/English-timed&amp;action=history"/>
		<updated>2026-04-13T08:33:14Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python-3.4.3/C4/Writing-Python-Scripts/English-timed&amp;diff=47710&amp;oldid=prev</id>
		<title>PoojaMoolya: Created page with &quot;{| border=1 |'''Time''' |'''Narration'''  |- |00:01 |Welcome to the spoken tutorial on '''Writing Python scripts.'''  |- |00:06 |In this tutorial we will learn,  What is '''im...&quot;</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python-3.4.3/C4/Writing-Python-Scripts/English-timed&amp;diff=47710&amp;oldid=prev"/>
				<updated>2019-06-10T05:26:28Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;{| border=1 |&amp;#039;&amp;#039;&amp;#039;Time&amp;#039;&amp;#039;&amp;#039; |&amp;#039;&amp;#039;&amp;#039;Narration&amp;#039;&amp;#039;&amp;#039;  |- |00:01 |Welcome to the spoken tutorial on &amp;#039;&amp;#039;&amp;#039;Writing Python scripts.&amp;#039;&amp;#039;&amp;#039;  |- |00:06 |In this tutorial we will learn,  What is &amp;#039;&amp;#039;&amp;#039;im...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
|'''Time'''&lt;br /&gt;
|'''Narration'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:01&lt;br /&gt;
|Welcome to the spoken tutorial on '''Writing Python scripts.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:06&lt;br /&gt;
|In this tutorial we will learn,  What is '''importing''' ?&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:12&lt;br /&gt;
|Write your own '''Python modules''' and &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:15&lt;br /&gt;
|Understand the double underscore '''name''' double underscore double equal to within double quotes double underscore '''main double underscore idiom'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:25&lt;br /&gt;
|To record this tutorial, I am using&lt;br /&gt;
&lt;br /&gt;
'''Ubuntu Linux 16.04''' operating system&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:34&lt;br /&gt;
| '''Python 3.4.3 '''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:37&lt;br /&gt;
|'''IPython 5.1.0''' and '''Gedit text editor'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:44&lt;br /&gt;
|To practise this tutorial, you should know how to  '''Run''' basic '''Python commands''' on the '''IPython console '''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:53&lt;br /&gt;
|And Use '''Python modules.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|00:56&lt;br /&gt;
|If not, see the relevant '''Python''' tutorials on this website.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:02&lt;br /&gt;
|We can write '''Python modules''' to bundle '''functions.''' &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:07&lt;br /&gt;
|We can then make use of these '''functions''' by importing '''modules''' to other '''scripts.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:14&lt;br /&gt;
|Let us first write a '''function''' and save it in a '''script.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:19&lt;br /&gt;
|Open any '''text editor''' and type the below code.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:24&lt;br /&gt;
|This '''Python module''' has a '''function''' to compute '''gcd''' of two numbers. Please take care of the indentation.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:33&lt;br /&gt;
|We have included a '''text case''' in the '''script'''. This '''text case''' will check the '''GCD function''' everytime the '''script''' is run. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:43&lt;br /&gt;
|Let us save the file as '''gcd''' underscore '''script.py''' in the current '''working directory.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:50&lt;br /&gt;
|Open a new '''terminal'''. Type, '''ipython3''' and press '''Enter.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|01:59&lt;br /&gt;
|From here onwards remember to press the '''Enter''' key after typing every '''command''' on the '''terminal.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:06&lt;br /&gt;
|Now we will run the '''script'''. Type, percentage '''run gcd''' underscore '''script dot py'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:16&lt;br /&gt;
|We get an output which says '''“Everything is OK”'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:21&lt;br /&gt;
|It means that the '''test case''' checking '''gcd''' inside brackets '''40''' comma '''12''' equals to '''4''' is passed. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:30&lt;br /&gt;
|What if we want to use the '''gcd function''' in some other '''scripts'''?&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:35&lt;br /&gt;
|This is possible since every '''Python''' file can be used as a '''module.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:41&lt;br /&gt;
|But first, we will understand what happens when we '''import''' a '''module.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:47&lt;br /&gt;
| Type, '''import sys'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:51&lt;br /&gt;
|Now type '''sys.path'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|02:55&lt;br /&gt;
|We can see a list of locations. This indicates that '''Python''' searches for a '''module''' when it encounters an '''import statement.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:05&lt;br /&gt;
|The '''standard modules''' are built into '''Python''' itself.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:09&lt;br /&gt;
|Otherwise it is found in system locations like slash '''usr''' slash '''lib''' slash '''python3.4''' slash&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:20&lt;br /&gt;
|The first item in the '''list''' is an empty '''string.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:24&lt;br /&gt;
|It means the current '''working directory''' is also searched.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:29&lt;br /&gt;
|We can '''import''' a '''module''' present in the current '''working directory'''. Type, '''import gcd''' underscore '''script'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:39&lt;br /&gt;
|Since '''gcd''' underscore '''script dot py''' is in the current '''working directory, import''' will work directly.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:48&lt;br /&gt;
|Type the '''print statement''' as shown here.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|03:53&lt;br /&gt;
|We get the '''gcd''' of 187 and 391 as output, which is 17.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:00&lt;br /&gt;
|We can also see the output '''“Everything is OK”''' that we added as '''test code. '''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:07&lt;br /&gt;
|This is also executed when we imported '''gcd''' underscore '''script.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:13&lt;br /&gt;
|The '''test code''' is added to check the '''gcd function.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:18&lt;br /&gt;
|'''Test code''' should only be executed when we '''run''' the '''Python script''' independently. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:24&lt;br /&gt;
|To execute the '''test code''' when the '''module''' is imported to other '''scripts''', we can use double underscore '''name''' double underscore '''variable.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:34&lt;br /&gt;
|Hereafter I will call this as '''name variable'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:38&lt;br /&gt;
|First, we shall look at how to use the '''variable''' and then understand how it works.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:45&lt;br /&gt;
| Now we will add this '''variable''' in the '''script''' '''gcd''' underscore '''script dot py'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|04:52&lt;br /&gt;
|Type the following after '''return a statement'''&lt;br /&gt;
&lt;br /&gt;
''' if''' double underscore '''name''' double underscore double equal to inside double quotes double underscore '''main''' double underscore colon &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:07&lt;br /&gt;
|Indent the code properly.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:10&lt;br /&gt;
|Save the file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:12&lt;br /&gt;
|Let us '''run''' the '''code'''. Type, percentage '''run gcd''' underscore '''script.py'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:21&lt;br /&gt;
|We can see that the test is executed successfully and we get output as '''Everything is OK'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:28&lt;br /&gt;
|Now we will check the changes by '''importing''' the '''module. '''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:33&lt;br /&gt;
|Type, '''import gcd_script'''&lt;br /&gt;
&lt;br /&gt;
We didn’t see any changes.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:41&lt;br /&gt;
|Note that, once a '''module''' is imported, it cannot be '''imported''' again in an existing '''IPython console.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:49&lt;br /&gt;
|So we will exit the existing '''IPython console''' by typing '''exit.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|05:56&lt;br /&gt;
|Open another '''terminal'''. Type '''ipython3'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:04&lt;br /&gt;
|Now we will '''import gcd''' underscore '''script.py'''. Type,''' import gcd''' underscore '''script'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:14&lt;br /&gt;
|We can see that now the '''test code''' is not executed. '''Everything is OK''' is not displayed as output.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:22&lt;br /&gt;
|The '''name variable''' is local to every '''module'''. It is equal to '''main''' only when the file is run as a '''script.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:31&lt;br /&gt;
|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.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:43&lt;br /&gt;
|Switch back to the '''terminal.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:46&lt;br /&gt;
|Type the following '''code''' which checks whether two numbers are relatively prime. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|06:54&lt;br /&gt;
|We have '''imported gcd function''' from '''gcd''' underscore '''script.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:00&lt;br /&gt;
|And use it in our calculations without the '''test code''' being executed.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:06&lt;br /&gt;
|Two numbers are relatively prime when their '''gcd''' is equal to one.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:12&lt;br /&gt;
|So we get output as '''Yes, 11 and 3 are relatively prime.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:18&lt;br /&gt;
|Every '''Python''' file can be''' run''' in two ways:  As an independent stand-alone '''script ''' or &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:26&lt;br /&gt;
|As a '''Python module''' which can be imported by other '''Python scripts''' or '''modules.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:33&lt;br /&gt;
|This brings us to the end of this tutorial. Let us summarize.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:37&lt;br /&gt;
| In this tutorial, we have learnt to,  '''Import a module.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:44&lt;br /&gt;
| Use a '''script''' as a '''module '''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:47&lt;br /&gt;
|Write '''test condition''' using the '''name variable''' and&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:52&lt;br /&gt;
|Restart '''IPython3''' if a changed '''module''' is to be imported again&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|07:57&lt;br /&gt;
|Here are some self assessment questions for you to solve  &lt;br /&gt;
&lt;br /&gt;
First. Which of the following '''variables''' contains the locations to search for '''Python modules'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|08:08&lt;br /&gt;
| Second. A '''module''' should contain only one '''function'''. True or False&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|08:14&lt;br /&gt;
|And the answers.  First. '''sys.path''' contains the locations to search for '''Python modules.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|08:22&lt;br /&gt;
|Second. '''False'''. A '''module''' can contain a wide range of '''functions.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|08:28&lt;br /&gt;
|Please post your timed queries in this forum.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|08:32&lt;br /&gt;
|Please post your general queries on Python in this forum.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|08:37&lt;br /&gt;
|FOSSEE team coordinates the TBC project.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|08:41&lt;br /&gt;
|Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|08:51&lt;br /&gt;
|This is Priya from IIT Bombay signing off. Thanks for watching.&lt;/div&gt;</summary>
		<author><name>PoojaMoolya</name></author>	</entry>

	</feed>