Difference between revisions of "Python/C4/Advanced-features-of-functions/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
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 'advanced features of functions'.
 
| Hello friends and Welcome to the tutorial on 'advanced features of functions'.
  
 
|-
 
|-
| 0:06
+
| 00:06
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
  
Line 15: Line 16:
  
 
|-
 
|-
| 0:23
+
| 00:23
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with functions".
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with functions".
  
 
|-
 
|-
| 0:32
+
| 00:32
 
| Let us Start the ipython interpreter in command  
 
| Let us Start the ipython interpreter in command  
  
 
|-
 
|-
|0:36
+
|00:36
 
|ipython hypen pylab in command
 
|ipython hypen pylab in command
  
 
|-
 
|-
| 0:43
+
| 00:43
 
| Let's use the  round  function as an example to understand what a default value of an argument means.  
 
| Let's use the  round  function as an example to understand what a default value of an argument means.  
  
 
|-
 
|-
| 0:49
+
| 00:49
 
| Let's type the following expressions in the terminal.
 
| Let's type the following expressions in the terminal.
  
 
|-
 
|-
|0:52
+
|00:52
 
|Type round within bracket 2.484 and hit enter.
 
|Type round within bracket 2.484 and hit enter.
 
Then type round  2.484 comma  2
 
Then type round  2.484 comma  2
  
 
|-
 
|-
| 1:10
+
| 01:10
 
| Both the first expression and the second are calls to the round  function, but the first calls it with only one argument and the second calls it with two arguments.  
 
| Both the first expression and the second are calls to the round  function, but the first calls it with only one argument and the second calls it with two arguments.  
  
 
|-
 
|-
| 1:22
+
| 01:22
 
| By observing the output, we can guess that the first one is equivalent to call with the second argument being 0. 0 .
 
| By observing the output, we can guess that the first one is equivalent to call with the second argument being 0. 0 .
  
 
|-
 
|-
| 1:30
+
| 01:30
 
| '''s.strip() # strips on spaces.'''
 
| '''s.strip() # strips on spaces.'''
  
 
|-
 
|-
| 1:35
+
| 01:35
 
| s.strip within single quote at the rate  # strips the string of 'at the rate' symbols.
 
| s.strip within single quote at the rate  # strips the string of 'at the rate' symbols.
  
 
|-
 
|-
| 1:47
+
| 01:47
 
| Thus it can be said that here, blank space is the default argument.
 
| Thus it can be said that here, blank space is the default argument.
  
 
|-
 
|-
| 1:52
+
| 01:52
 
| plot within bracket x comma  y # plots with x versus y using default line style. plot within bracket x comma  y comma  in single quote o # plots x versus y with circle markers.
 
| plot within bracket x comma  y # plots with x versus y using default line style. plot within bracket x comma  y comma  in single quote o # plots x versus y with circle markers.
  
 
|-
 
|-
| 2:14
+
| 02:14
 
| Hence, here when third argument is not provided, it shows default line style.
 
| Hence, here when third argument is not provided, it shows default line style.
  
 
|-
 
|-
| 2:20
+
| 02:20
 
| linspace within bracket 0 comma  2 star pi comma  100 # returns 100 points between 0 and 2pi linspace within bracket 0 comma  2 star pi # returns 50 points between 0 and 2pi
 
| linspace within bracket 0 comma  2 star pi comma  100 # returns 100 points between 0 and 2pi linspace within bracket 0 comma  2 star pi # returns 50 points between 0 and 2pi
  
 
|-
 
|-
| 2:37
+
| 02:37
 
| Hence, the default for the third argument is 50.
 
| Hence, the default for the third argument is 50.
  
 
|-
 
|-
| 2:42
+
| 02:42
 
| Let's now define a simple function that uses default arguments.  
 
| Let's now define a simple function that uses default arguments.  
  
 
|-
 
|-
| 2:46
+
| 02:46
 
| We define a simple function that prints a welcome message to a person, given a greeting and his/her name.
 
| We define a simple function that prints a welcome message to a person, given a greeting and his/her name.
  
 
|-
 
|-
|2:54
+
|02:54
 
|So type in the terminal def welcome within bracket greet comma  name= within double quotes World colon (hit enter)
 
|So type in the terminal def welcome within bracket greet comma  name= within double quotes World colon (hit enter)
 
                         print greet comma  name (hit enter)
 
                         print greet comma  name (hit enter)
  
 
|-
 
|-
| 3:18
+
| 03:18
 
| Let us first call the function with two arguments, one for  greet  and other for  name .
 
| Let us first call the function with two arguments, one for  greet  and other for  name .
  
 
|-
 
|-
|3:25
+
|03:25
 
|So type welcome  and in bracket  within double quotes Hi comma  within double quotes Guido
 
|So type welcome  and in bracket  within double quotes Hi comma  within double quotes Guido
  
 
|-
 
|-
| 3:35
+
| 03:35
 
| We get the expected welcome message, "Hi Guido".
 
| We get the expected welcome message, "Hi Guido".
  
 
|-
 
|-
| 3:41
+
| 03:41
 
| Now let us call the function with just one argument "Hello".
 
| Now let us call the function with just one argument "Hello".
  
 
|-
 
|-
|3:45
+
|03:45
 
|So type welcome within bracket  within double quotes Hello
 
|So type welcome within bracket  within double quotes Hello
  
 
|-
 
|-
| 3:53
+
| 03:53
 
| "Hello" is treated as the greet  and we get "Hello World" as the output.  
 
| "Hello" is treated as the greet  and we get "Hello World" as the output.  
  
 
|-
 
|-
| 3:59
+
| 03:59
 
| "World" is the default value for the argument  name .
 
| "World" is the default value for the argument  name .
  
 
|-
 
|-
| 4:02
+
| 04:02
 
| Pause the video here,  try out the following exercise and resume the video.
 
| Pause the video here,  try out the following exercise and resume the video.
  
 
|-
 
|-
| 4:07
+
| 04:07
 
| '''Redefine the function  welcome , by interchanging it's''' arguments.  
 
| '''Redefine the function  welcome , by interchanging it's''' arguments.  
  
 
|-
 
|-
| 4:11
+
| 04:11
 
| Place the name  argument with it's default value of "World" before the  greet  argument.
 
| Place the name  argument with it's default value of "World" before the  greet  argument.
  
 
|-
 
|-
| 4:17
+
|04:17
 
| Switch to the terminal for solution
 
| Switch to the terminal for solution
  
 
|-
 
|-
|4:20
+
|04:20
 
|def welcome within bracket name= within double quotes World comma  greet colon (hit enter)
 
|def welcome within bracket name= within double quotes World comma  greet colon (hit enter)
 
     print greet comma  name (hit enter)
 
     print greet comma  name (hit enter)
  
 
|-
 
|-
| 4:36
+
| 04:36
 
| We get an error that reads SyntaxError: non-default argument follows default argument .Then ipython control line one.
 
| We get an error that reads SyntaxError: non-default argument follows default argument .Then ipython control line one.
  
 
|-
 
|-
| 4:47
+
| 04:47
 
| When defining a function all the argument with default values should come at the end.
 
| When defining a function all the argument with default values should come at the end.
  
 
|-
 
|-
| 4:52
+
| 04:52
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 4:58
+
| 04:58
 
| '''See the definition of linspace using  question mark  and make a note of all''' the arguments with default values are towards the end.
 
| '''See the definition of linspace using  question mark  and make a note of all''' the arguments with default values are towards the end.
  
 
|-
 
|-
| 5:09
+
| 05:09
 
| Switch to the terminal for solution
 
| Switch to the terminal for solution
  
 
|-
 
|-
|5:12
+
|05:12
 
|linspace question mark hit enter
 
|linspace question mark hit enter
  
 
|-
 
|-
| 5:17
+
| 05:17
 
| As we go on hitting the enter key, we the the number of arguments this command has.  
 
| As we go on hitting the enter key, we the the number of arguments this command has.  
  
 
|-
 
|-
| 5:26
+
| 05:26
 
| Please read the content on your terminal.   
 
| Please read the content on your terminal.   
  
 
|-
 
|-
| 5:30
+
| 05:30
 
| Again,Pause the video here,try out the following exercise and resume the video.
 
| Again,Pause the video here,try out the following exercise and resume the video.
  
 
|-
 
|-
| 5:35
+
| 05:35
 
| '''Redefine the function  welcome  with a default value of'''
 
| '''Redefine the function  welcome  with a default value of'''
  
 
|-
 
|-
| 5:41
+
| 05:41
 
| "Hello" to the  greet  argument.  
 
| "Hello" to the  greet  argument.  
  
 
|-
 
|-
| 5:44
+
| 05:44
 
| Then, call the function without any arguments.
 
| Then, call the function without any arguments.
  
 
|-
 
|-
| 5:48
+
| 05:48
 
| Switch to the terminal for solution
 
| Switch to the terminal for solution
  
 
|-
 
|-
|5:51
+
|05:51
 
|Type def welcome within bracket greet= within double quotes Hello comma  name= in double quotes World colon
 
|Type def welcome within bracket greet= within double quotes Hello comma  name= in double quotes World colon
 
     print greet comma  name
 
     print greet comma  name
Line 197: Line 198:
  
 
|-
 
|-
| 6:17
+
| 06:17
 
| As we can see, we get the output as  Hello World .
 
| As we can see, we get the output as  Hello World .
  
 
|-
 
|-
| 6:20
+
| 06:20
 
| Let us now learn what keyword arguments or named arguments are.
 
| Let us now learn what keyword arguments or named arguments are.
  
 
|-
 
|-
| 6:26
+
| 06:26
 
| We shall refer to them as keyword arguments, henceforth.
 
| We shall refer to them as keyword arguments, henceforth.
  
 
|-
 
|-
| 6:31
+
| 06:31
 
| When you are calling functions in Python, you don't need to remember the order in which to pass the arguments.  
 
| When you are calling functions in Python, you don't need to remember the order in which to pass the arguments.  
  
 
|-
 
|-
| 6:38
+
| 06:38
 
| Instead, you need the name of the argument to pass it a value.  
 
| Instead, you need the name of the argument to pass it a value.  
  
 
|-
 
|-
| 6:44
+
| 06:44
 
| This slide shows a few function calls that use keyword arguments.  
 
| This slide shows a few function calls that use keyword arguments.  
  
 
|-
 
|-
| 6:52
+
| 06:52
 
| loc , linewidth ,  xy  and  labels<  are being called with keyword arguments.
 
| loc , linewidth ,  xy  and  labels<  are being called with keyword arguments.
  
 
|-
 
|-
| 7:00
+
| 07:00
 
| Let us try and understand this better using the  welcome  function that we have been using all along.  
 
| Let us try and understand this better using the  welcome  function that we have been using all along.  
  
 
|-
 
|-
| 7:07
+
| 07:07
 
| Let us call it in different ways and observe the output to see how keyword arguments work.
 
| Let us call it in different ways and observe the output to see how keyword arguments work.
  
 
|-
 
|-
|7:14
+
|07:14
 
|So type in terminal type welcome()
 
|So type in terminal type welcome()
  
Line 241: Line 242:
  
 
|-
 
|-
| 7:37
+
| 07:37
 
| When no keyword is specified, the arguments are allotted based on their position.  
 
| When no keyword is specified, the arguments are allotted based on their position.  
  
 
|-
 
|-
| 7:42
+
| 07:42
 
| So, "Hi" is the value of the argument greet  and name is passed the value "Guido".
 
| So, "Hi" is the value of the argument greet  and name is passed the value "Guido".
  
 
|-
 
|-
| 7:48
+
| 07:48
 
| If we type on the terminal welcome within bracket name= within double quotes Guido comma  greet= within double quotes Hey exclamation  
 
| If we type on the terminal welcome within bracket name= within double quotes Guido comma  greet= within double quotes Hey exclamation  
  
 
|-
 
|-
|8:02
+
|08:02
 
| When keyword arguments are used, the arguments can be called in any order.  
 
| When keyword arguments are used, the arguments can be called in any order.  
  
 
|-
 
|-
| 8:07
+
| 08:07
 
| And if we call our function as,welcome within bracket name="Guido" comma  "Hey"  
 
| And if we call our function as,welcome within bracket name="Guido" comma  "Hey"  
  
 
|-
 
|-
| 8:17
+
| 08:17
 
| This call returns an error that reads, non-keyword arg after keyword arg . Python expects all the keyword to be present towards the end.
 
| This call returns an error that reads, non-keyword arg after keyword arg . Python expects all the keyword to be present towards the end.
  
 
|-
 
|-
| 8:30
+
| 08:30
 
| That brings us to the end of what we wanted to learn about  keyword  arguments.
 
| That brings us to the end of what we wanted to learn about  keyword  arguments.
  
 
|-
 
|-
| 8:37
+
| 08:37
 
| Before defining a function of your own, make sure that you check the standard library, for a similar function.  
 
| Before defining a function of your own, make sure that you check the standard library, for a similar function.  
  
 
|-
 
|-
| 8:43
+
| 08:43
 
| Python is popularly called a "Batteries included" language, for the huge library that comes along with it.
 
| Python is popularly called a "Batteries included" language, for the huge library that comes along with it.
  
 
|-
 
|-
| 8:55
+
| 08:55
 
| Math functions - abs, sin, .... Plot functions - plot, bar, pie ... Boolean functions - and, or, not ...
 
| Math functions - abs, sin, .... Plot functions - plot, bar, pie ... Boolean functions - and, or, not ...
  
 
|-
 
|-
| 9:11
+
| 09:11
 
| Apart from the standard library there are other libraries like  pylab ,  scipy , etc which have a huge collection of functions for scientific purposes.
 
| Apart from the standard library there are other libraries like  pylab ,  scipy , etc which have a huge collection of functions for scientific purposes.
  
 
|-
 
|-
| 9:22
+
| 09:22
 
| '''pylab''' has
 
| '''pylab''' has
  
 
|-
 
|-
| 9:23
+
| 09:23
 
| plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog
 
| plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog
  
 
|-
 
|-
| 9:32
+
| 09:32
 
| '''scipy (modules)'''has
 
| '''scipy (modules)'''has
  
 
|-
 
|-
| 9:35
+
| 09:35
 
| fftpack, stats, linalg, ndimage, signal, optimize, integrate
 
| fftpack, stats, linalg, ndimage, signal, optimize, integrate
  
 
|-
 
|-
|9:46
+
|09:46
 
| This brings us to the end of this tutorial.  
 
| This brings us to the end of this tutorial.  
  
 
|-
 
|-
| 9:48
+
| 09:48
 
| In this tutorial, we have learnt to, 1. Define functions with default arguments.
 
| In this tutorial, we have learnt to, 1. Define functions with default arguments.
  
 
|-
 
|-
| 9:53
+
| 09:53
 
| 2. Call functions using keyword arguments.
 
| 2. Call functions using keyword arguments.
  
 
|-
 
|-
| 9:55
+
| 09:55
 
| 3. Use the range of functions available in the Python standard library and the Scientific Computing related packages.
 
| 3. Use the range of functions available in the Python standard library and the Scientific Computing related packages.
  

Revision as of 15:52, 10 July 2014

Time Narration
00:01 Hello friends and Welcome to the tutorial on 'advanced features of functions'.
00:06 At the end of this tutorial, you will be able to,
  1. Assign default values to arguments, when defining functions.
  2. Define and call functions with keyword arguments.
  3. Learn some of the built-in functions available in Python standard library and the scientific computing libraries.
00:23 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with functions".
00:32 Let us Start the ipython interpreter in command
00:36 ipython hypen pylab in command
00:43 Let's use the round function as an example to understand what a default value of an argument means.
00:49 Let's type the following expressions in the terminal.
00:52 Type round within bracket 2.484 and hit enter.

Then type round 2.484 comma 2

01:10 Both the first expression and the second are calls to the round function, but the first calls it with only one argument and the second calls it with two arguments.
01:22 By observing the output, we can guess that the first one is equivalent to call with the second argument being 0. 0 .
01:30 s.strip() # strips on spaces.
01:35 s.strip within single quote at the rate # strips the string of 'at the rate' symbols.
01:47 Thus it can be said that here, blank space is the default argument.
01:52 plot within bracket x comma y # plots with x versus y using default line style. plot within bracket x comma y comma in single quote o # plots x versus y with circle markers.
02:14 Hence, here when third argument is not provided, it shows default line style.
02:20 linspace within bracket 0 comma 2 star pi comma 100 # returns 100 points between 0 and 2pi linspace within bracket 0 comma 2 star pi # returns 50 points between 0 and 2pi
02:37 Hence, the default for the third argument is 50.
02:42 Let's now define a simple function that uses default arguments.
02:46 We define a simple function that prints a welcome message to a person, given a greeting and his/her name.
02:54 So type in the terminal def welcome within bracket greet comma name= within double quotes World colon (hit enter)
                        print greet comma  name (hit enter)
03:18 Let us first call the function with two arguments, one for greet and other for name .
03:25 So type welcome and in bracket within double quotes Hi comma within double quotes Guido
03:35 We get the expected welcome message, "Hi Guido".
03:41 Now let us call the function with just one argument "Hello".
03:45 So type welcome within bracket within double quotes Hello
03:53 "Hello" is treated as the greet and we get "Hello World" as the output.
03:59 "World" is the default value for the argument name .
04:02 Pause the video here, try out the following exercise and resume the video.
04:07 Redefine the function welcome , by interchanging it's arguments.
04:11 Place the name argument with it's default value of "World" before the greet argument.
04:17 Switch to the terminal for solution
04:20 def welcome within bracket name= within double quotes World comma greet colon (hit enter)
   print greet comma  name (hit enter)
04:36 We get an error that reads SyntaxError: non-default argument follows default argument .Then ipython control line one.
04:47 When defining a function all the argument with default values should come at the end.
04:52 Pause the video here, try out the following exercise and resume the video.
04:58 See the definition of linspace using question mark and make a note of all the arguments with default values are towards the end.
05:09 Switch to the terminal for solution
05:12 linspace question mark hit enter
05:17 As we go on hitting the enter key, we the the number of arguments this command has.
05:26 Please read the content on your terminal.
05:30 Again,Pause the video here,try out the following exercise and resume the video.
05:35 Redefine the function welcome with a default value of
05:41 "Hello" to the greet argument.
05:44 Then, call the function without any arguments.
05:48 Switch to the terminal for solution
05:51 Type def welcome within bracket greet= within double quotes Hello comma name= in double quotes World colon
   print greet comma  name

(After hitting enter type)

   welcome()
06:17 As we can see, we get the output as Hello World .
06:20 Let us now learn what keyword arguments or named arguments are.
06:26 We shall refer to them as keyword arguments, henceforth.
06:31 When you are calling functions in Python, you don't need to remember the order in which to pass the arguments.
06:38 Instead, you need the name of the argument to pass it a value.
06:44 This slide shows a few function calls that use keyword arguments.
06:52 loc , linewidth , xy and labels< are being called with keyword arguments.
07:00 Let us try and understand this better using the welcome function that we have been using all along.
07:07 Let us call it in different ways and observe the output to see how keyword arguments work.
07:14 So type in terminal type welcome()

welcome within bracket within double quotes Hello comma double quotes James

welcome within bracket within double quotes Hi comma name= within double quotes Guido

07:37 When no keyword is specified, the arguments are allotted based on their position.
07:42 So, "Hi" is the value of the argument greet and name is passed the value "Guido".
07:48 If we type on the terminal welcome within bracket name= within double quotes Guido comma greet= within double quotes Hey exclamation
08:02 When keyword arguments are used, the arguments can be called in any order.
08:07 And if we call our function as,welcome within bracket name="Guido" comma "Hey"
08:17 This call returns an error that reads, non-keyword arg after keyword arg . Python expects all the keyword to be present towards the end.
08:30 That brings us to the end of what we wanted to learn about keyword arguments.
08:37 Before defining a function of your own, make sure that you check the standard library, for a similar function.
08:43 Python is popularly called a "Batteries included" language, for the huge library that comes along with it.
08:55 Math functions - abs, sin, .... Plot functions - plot, bar, pie ... Boolean functions - and, or, not ...
09:11 Apart from the standard library there are other libraries like pylab , scipy , etc which have a huge collection of functions for scientific purposes.
09:22 pylab has
09:23 plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog
09:32 scipy (modules)has
09:35 fftpack, stats, linalg, ndimage, signal, optimize, integrate
09:46 This brings us to the end of this tutorial.
09:48 In this tutorial, we have learnt to, 1. Define functions with default arguments.
09:53 2. Call functions using keyword arguments.
09:55 3. Use the range of functions available in the Python standard library and the Scientific Computing related packages.
10:04 Here are some self assessment questions for you to solve
10:08 1. All arguments of a function cannot have default values. - True or False?
10:14 2. The following is a valid function definition.
10:17 True or False?
10:21 def separator within bracket count=40 comma char comma show=False colon
 if show colon
 print char  star  count
 return char  star  count
10:36 3. When calling a function, the arguments should always be in the order in which they are defined.
10:45 the arguments can be in any order.
10:47 only keyword arguments can be in any order, but should be called at the beginning.
10:56 only keyword arguments can be in any order, and should be called at the end.
11:10 So now we can look at the answers,
11:13 1.False.
11:15 All arguments of a Python function can have default values.
11:21 2. False.
11:23 All parameters with default arguments should be defined at the end.
11:27 3. When calling a function,only keyword arguments can be in any order, but should be called at the end.
11:35 Hope you have enjoyed this tutorial and found it useful.
11:39 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha