Difference between revisions of "Python/C4/Writing-python-scripts/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !Visual Cue !Narration |- | 0:01 | Hello friends and welcome to the tutorial on "Writing Python scripts". |- | 0:05 | At the end of this tutorial, you will be able t…')
 
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Visual Cue
+
|'''Time'''
!Narration
+
|'''Narration'''
 +
 
 
|-
 
|-
| 0:01
+
| 00:01
 
| Hello friends and welcome to the tutorial on "Writing Python scripts".
 
| Hello friends and welcome to the tutorial on "Writing Python scripts".
  
 
|-
 
|-
| 0:05
+
| 00:05
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
 
+
Understand what is importing.
# Understand what is importing.
+
Write your own Python modules.
# Write your own Python modules.
+
Understand the   __name__ == in double quotes  __main__  underscore   idiom
# Understand the <tt> __name__ == in double quotes  __main__  underscore </tt> idiom
+
  
 
|-
 
|-
| 0:19
+
| 00:19
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Using Python modules".
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Using Python modules".
  
 
|-
 
|-
| 0:25
+
| 00:25
 
| Often we will have to reuse the code that we have written.  
 
| Often we will have to reuse the code that we have written.  
  
 
|-
 
|-
| 0:29
+
| 00:29
 
| We do that by writing functions.  
 
| We do that by writing functions.  
  
 
|-
 
|-
| 0:31
+
| 00:31
 
| Functions are bundled into packages and are imported as and when required in other scripts.
 
| Functions are bundled into packages and are imported as and when required in other scripts.
  
 
|-
 
|-
| 0:37
+
| 00:37
 
| Let us first write a function that computes the gcd of two numbers and save it in a script.  
 
| Let us first write a function that computes the gcd of two numbers and save it in a script.  
  
 
|-
 
|-
| 0:44
+
| 00:44
 
| Open an editor and type the code.  
 
| Open an editor and type the code.  
  
 
|-
 
|-
| 0:47
+
| 00:47
 
| Please take care of the indentation.
 
| Please take care of the indentation.
  
 
|-
 
|-
|0:51
+
|00:51
|So type in terminal gcd underscore script.py then type def gcd within bracket  a comma  b colon
+
|So type in terminal gcd underscore script.py then type def gcd within bracket  a comma  b colon while b colon  a comma  b = b comma  a percentage b return a
 
+
while b colon
+
  a comma  b = b comma  a percentage b
+
 
+
    return a
+
  
 
|-
 
|-
| 1:49
+
| 01:49
 
| We shall write a test function in the script that tests the gcd function every time the script is run.
 
| We shall write a test function in the script that tests the gcd function every time the script is run.
  
 
|-
 
|-
|1:56
+
|01:56
 
|So type
 
|So type
if gcd within bracket 40 comma  12 == 4 colon
+
if gcd within bracket 40 comma  12 == 4 colon, print  within double quotes Everything OK else colon print  within double quotes The GCD function is wrong
    print  within double quotes Everything OK
+
else colon
+
    print  within double quotes The GCD function is wrong
+
 
+
  
 
|-
 
|-
| 2:53
+
| 02:53
| Let us save the file as script.py in <tt> slash home slash fossee slash gcd script.py</tt>
+
| Let us save the file as script.py in   slash home slash fossee slash gcd script.py  
  
 
|-
 
|-
| 3:05
+
| 03:05
 
| We shall run the script by typing in the terminal
 
| We shall run the script by typing in the terminal
  
 
|-
 
|-
|3:11
+
|03:11
 
|python  slash home slash fossee slash gcd underscore script.py
 
|python  slash home slash fossee slash gcd underscore script.py
  
 
|-
 
|-
| 3:25
+
| 03:25
 
| We can see that the script is executed and everything is fine.
 
| We can see that the script is executed and everything is fine.
  
 
|-
 
|-
| 3:29
+
| 03:29
 
| What if we want to use the gcd function in some of our other scripts.  
 
| What if we want to use the gcd function in some of our other scripts.  
  
 
|-
 
|-
| 3:35
+
| 03:35
 
| This is also possible since every python file can be used as a module.
 
| This is also possible since every python file can be used as a module.
  
 
|-
 
|-
| 3:39
+
|03:39
 
| But first, we shall understand what happens when you import a module.
 
| But first, we shall understand what happens when you import a module.
  
 
|-
 
|-
|3:43
+
|03:43
 
|So type ipython in terminal.  
 
|So type ipython in terminal.  
  
 
|-
 
|-
| 3:52
+
| 03:52
| Open IPython and type import sys (hit enter)
+
| Open IPython and type import sys (hit enter). sys.path
                        sys.path
+
  
 
|-
 
|-
| 4:15
+
| 04:15
 
| This is a list of locations where python searches for a module when it encounters an import statement.
 
| This is a list of locations where python searches for a module when it encounters an import statement.
  
 
|-
 
|-
| 4:20
+
| 04:20
| Hence, when we just did <tt>import sys</tt>, python searches for a file named sys.py or a folder named sys in all these locations one by one, until it finds one.
+
| 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.
  
 
|-
 
|-
| 4:34
+
| 04:34
 
| We can place our script in any one of these locations and can import it.
 
| We can place our script in any one of these locations and can import it.
  
 
|-
 
|-
| 4:38
+
|04:38
 
| The first item in the list is an empty string which means the current working directory is also searched.
 
| The first item in the list is an empty string which means the current working directory is also searched.
  
 
|-
 
|-
| 4:45
+
| 04:45
 
| Alternatively, we can also import the module if we are working in same directory where the script exists.
 
| Alternatively, we can also import the module if we are working in same directory where the script exists.
  
 
|-
 
|-
|4:53
+
|04:53
 
| Since we are in  slash home slash fossee, we can simply do
 
| Since we are in  slash home slash fossee, we can simply do
  
 
|-
 
|-
|4:59
+
|04:59
 
|By typing on the terminal import gcd underscore script.py and hit enter.
 
|By typing on the terminal import gcd underscore script.py and hit enter.
 
   
 
   
 
|-
 
|-
| 5:12
+
| 05:12
 
| We can see that the gcd underscore script is imported.  
 
| We can see that the gcd underscore script is imported.  
  
 
|-
 
|-
| 5:18
+
| 05:18
 
| But the test code that we added at the end of the file is also executed.
 
| But the test code that we added at the end of the file is also executed.
  
 
|-
 
|-
| 5:23
+
| 05:23
 
| But we want the test code to be executed only when the file is run as a python script or not when it is imported.
 
| But we want the test code to be executed only when the file is run as a python script or not when it is imported.
  
 
|-
 
|-
| 5:30
+
| 05:30
| This is possible by using <tt> underscore  underscore name underscore  underscore </tt> variable.
+
| This is possible by using underscore  underscore name underscore  underscore variable.
  
 
|-
 
|-
| 5:34
+
| 05:34
 
| First, we shall look at how to use the idiom and then understand how it works.
 
| First, we shall look at how to use the idiom and then understand how it works.
  
 
|-
 
|-
| 5:41
+
| 05:41
 
| Go to the file and add this line as the beginning of the code and indent the code accordingly.
 
| Go to the file and add this line as the beginning of the code and indent the code accordingly.
  
 
|-
 
|-
|5:47
+
|05:47
 
|So type if  underscore  underscore name underscore  underscore  ==  within double quotes  underscore  underscore main underscore  underscore  colon
 
|So type if  underscore  underscore name underscore  underscore  ==  within double quotes  underscore  underscore main underscore  underscore  colon
  
 
|-
 
|-
| 6:30
+
| 06:30
 
| Let us first run the code.
 
| Let us first run the code.
  
 
|-
 
|-
|6:33
+
|06:33
 
|So type in terminal python gcd underscore script.py
 
|So type in terminal python gcd underscore script.py
  
 
|-
 
|-
|6:45
+
|06:45
 
|We can see that
 
|We can see that
  
 
|-
 
|-
|6:48
+
|06:48
 
|There is an error coming up, its showing indentation error.
 
|There is an error coming up, its showing indentation error.
  
 
|-
 
|-
|6:57
+
|06:57
 
|So we have to edit the file and make indentation correct.
 
|So we have to edit the file and make indentation correct.
  
 
|-
 
|-
| 7:20
+
| 07:20
 
| We can see that the test runs successfully.
 
| We can see that the test runs successfully.
  
 
|-
 
|-
| 7:24
+
| 07:24
 
| Now we shall import the file gcd underscore script
 
| Now we shall import the file gcd underscore script
  
 
|-
 
|-
|7:29
+
|07:29
 
|So type import gcd underscore script
 
|So type import gcd underscore script
  
 
|-
 
|-
| 7:39
+
| 07:39
| We see that now the test code is not executed.
+
| We see that now the test code is not executed.The underscore  underscore name underscore  underscore   variable is local to every module and it is equal to   underscore  underscore main underscore  underscore   only when the file is run as a script.
 
+
The <tt> underscore  underscore name underscore  underscore </tt> variable is local to every module and it is equal to <tt> underscore  underscore main underscore  underscore </tt> only when the file is run as a script.
+
  
 
|-
 
|-
| 7:54
+
| 07:54
| Hence, all the code that goes in to the if block, <tt>if name ==  within double quotes  main colon</tt> is executed only when the file is run as a python script.
+
| Hence, all the code that goes in to the if block, if name ==  within double quotes  main colon is executed only when the file is run as a python script.
  
 
|-
 
|-
|8:08
+
|08:08
 
| This brings us to the end of the tutorial.  
 
| This brings us to the end of the tutorial.  
  
 
|-
 
|-
| 8:11
+
| 08:11
 
| In this tutorial, we have learnt to, 1. Know what happens when we import a module.
 
| In this tutorial, we have learnt to, 1. Know what happens when we import a module.
  
 
|-
 
|-
| 8:16
+
|08:16
 
| 2.Use a script as a module.
 
| 2.Use a script as a module.
  
 
|-
 
|-
| 8:18
+
| 08:18
| 3. Write test functions using the <tt> name </tt> idiom.
+
| 3. Write test functions using the name   idiom.
  
 
|-
 
|-
|8:22
+
|08:22
 
| Here are some self assessment questions for you to solve
 
| Here are some self assessment questions for you to solve
  
 
|-
 
|-
| 8:26
+
| 08:26
| 1. '''Which of the following variables contains the locations to search for'''<br/> python modules
+
| Which of the following variables contains the locations to search for''' python modules
** sys.pythonpath
+
sys.pythonpath, sys.path, os.pythonpath, os.path'''
** sys.path
+
** os.pythonpath
+
** os.path
+
  
 
|-
 
|-
| 8:38
+
| 08:38
| 2. A module should contain only functions. - True - False
+
| A module should contain only functions. - True - False
  
 
|-
 
|-
| 8:43
+
| 08:43
| 3. The script <tt>utils.py</tt> is in one of locations of PYTHONPATH and contains the following code and the code is given.
+
| The script utils.py is in one of locations of PYTHONPATH and contains the following code and the code is given.
  
 
|-
 
|-
| 9:00
+
| 09:00
 
| And now look at the answers,
 
| And now look at the answers,
  
 
|-
 
|-
| 9:04
+
|09:04
| 1. <tt>sys.path</tt> contains the locations to search for python modules.
+
| sys.path contains the locations to search for python modules.
  
 
|-
 
|-
| 9:10
+
| 09:10
| 2. False is the answer.
+
| False is the answer.
  
 
|-
 
|-
| 9:13
+
| 09:13
 
| A module which contain a range of functions.
 
| A module which contain a range of functions.
  
 
|-
 
|-
| 9:19
+
| 09:19
| 3. After doing <tt>import utils</tt>, we can use the function <tt>show()</tt> as,
+
| After doing import utils , we can use the function show() as, utils.show within bracket double quotes hey
 
+
utils.show within bracket double quotes hey
+
  
 
|-
 
|-
| 9:32
+
| 09:32
 
| So we hope you have enjoyed this tutorial and found it useful.  
 
| So we hope you have enjoyed this tutorial and found it useful.  
  
 
|-
 
|-
| 9:35
+
| 09:35
 
| Thank you!
 
| Thank you!
  
 
|}
 
|}

Latest revision as of 12:56, 27 March 2017

Time Narration
00:01 Hello friends and welcome to the tutorial on "Writing Python scripts".
00:05 At the end of this tutorial, you will be able to,

Understand what is importing. Write your own Python modules. Understand the __name__ == in double quotes __main__ underscore idiom

00:19 Before beginning this tutorial,we would suggest you to complete the tutorial on "Using Python modules".
00:25 Often we will have to reuse the code that we have written.
00:29 We do that by writing functions.
00:31 Functions are bundled into packages and are imported as and when required in other scripts.
00:37 Let us first write a function that computes the gcd of two numbers and save it in a script.
00:44 Open an editor and type the code.
00:47 Please take care of the indentation.
00:51 So type in terminal gcd underscore script.py then type def gcd within bracket a comma b colon while b colon a comma b = b comma a percentage b return a
01:49 We shall write a test function in the script that tests the gcd function every time the script is run.
01:56 So type

if gcd within bracket 40 comma 12 == 4 colon, print within double quotes Everything OK else colon print within double quotes The GCD function is wrong

02:53 Let us save the file as script.py in slash home slash fossee slash gcd script.py
03:05 We shall run the script by typing in the terminal
03:11 python slash home slash fossee slash gcd underscore script.py
03:25 We can see that the script is executed and everything is fine.
03:29 What if we want to use the gcd function in some of our other scripts.
03:35 This is also possible since every python file can be used as a module.
03:39 But first, we shall understand what happens when you import a module.
03:43 So type ipython in terminal.
03:52 Open IPython and type import sys (hit enter). sys.path
04:15 This is a list of locations where python searches for a module when it encounters an import statement.
04:20 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.
04:34 We can place our script in any one of these locations and can import it.
04:38 The first item in the list is an empty string which means the current working directory is also searched.
04:45 Alternatively, we can also import the module if we are working in same directory where the script exists.
04:53 Since we are in slash home slash fossee, we can simply do
04:59 By typing on the terminal import gcd underscore script.py and hit enter.
05:12 We can see that the gcd underscore script is imported.
05:18 But the test code that we added at the end of the file is also executed.
05:23 But we want the test code to be executed only when the file is run as a python script or not when it is imported.
05:30 This is possible by using underscore underscore name underscore underscore variable.
05:34 First, we shall look at how to use the idiom and then understand how it works.
05:41 Go to the file and add this line as the beginning of the code and indent the code accordingly.
05:47 So type if underscore underscore name underscore underscore == within double quotes underscore underscore main underscore underscore colon
06:30 Let us first run the code.
06:33 So type in terminal python gcd underscore script.py
06:45 We can see that
06:48 There is an error coming up, its showing indentation error.
06:57 So we have to edit the file and make indentation correct.
07:20 We can see that the test runs successfully.
07:24 Now we shall import the file gcd underscore script
07:29 So type import gcd underscore script
07:39 We see that now the test code is not executed.The underscore underscore name underscore underscore variable is local to every module and it is equal to underscore underscore main underscore underscore only when the file is run as a script.
07:54 Hence, all the code that goes in to the if block, if name == within double quotes main colon is executed only when the file is run as a python script.
08:08 This brings us to the end of the tutorial.
08:11 In this tutorial, we have learnt to, 1. Know what happens when we import a module.
08:16 2.Use a script as a module.
08:18 3. Write test functions using the name idiom.
08:22 Here are some self assessment questions for you to solve
08:26 Which of the following variables contains the locations to search for python modules

sys.pythonpath, sys.path, os.pythonpath, os.path

08:38 A module should contain only functions. - True - False
08:43 The script utils.py is in one of locations of PYTHONPATH and contains the following code and the code is given.
09:00 And now look at the answers,
09:04 sys.path contains the locations to search for python modules.
09:10 False is the answer.
09:13 A module which contain a range of functions.
09:19 After doing import utils , we can use the function show() as, utils.show within bracket double quotes hey
09:32 So we hope you have enjoyed this tutorial and found it useful.
09:35 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha