Difference between revisions of "Python/C4/Advanced-features-of-functions/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 'advanced features of functions'. |- | 0:06 | At the end of this tutorial, you will b…')
 
Line 28: Line 28:
 
|-
 
|-
 
| 0:43
 
| 0:43
| Let's use the <tt>round</tt> 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.  
  
 
|-
 
|-
Line 41: Line 41:
 
|-
 
|-
 
| 1:10
 
| 1:10
| Both the first expression and the second are calls to the <tt>round</tt> 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.  
  
 
|-
 
|-
Line 90: Line 90:
 
|-
 
|-
 
| 3:18
 
| 3:18
| Let us first call the function with two arguments, one for <tt>greet</tt> and other for <tt>name</tt>.
+
| Let us first call the function with two arguments, one for greet and other for name .
  
 
|-
 
|-
Line 110: Line 110:
 
|-
 
|-
 
| 3:53
 
| 3:53
| "Hello" is treated as the <tt>greet</tt> and we get "Hello World" as the output.  
+
| "Hello" is treated as the greet and we get "Hello World" as the output.  
  
 
|-
 
|-
 
| 3:59
 
| 3:59
| "World" is the default value for the argument <tt>name</tt>.
+
| "World" is the default value for the argument name .
  
 
|-
 
|-
Line 122: Line 122:
 
|-
 
|-
 
| 4:07
 
| 4:07
| '''Redefine the function <tt>welcome</tt>, by interchanging it's''' arguments.  
+
| '''Redefine the function welcome , by interchanging it's''' arguments.  
  
 
|-
 
|-
 
| 4:11
 
| 4:11
| Place the <tt>name</tt> argument with it's default value of "World" before the <tt>greet</tt> argument.
+
| Place the name argument with it's default value of "World" before the greet argument.
  
 
|-
 
|-
Line 139: Line 139:
 
|-
 
|-
 
| 4:36
 
| 4:36
| We get an error that reads <tt>SyntaxError: non-default argument follows default argument</tt>.Then ipython control line one.
+
| We get an error that reads SyntaxError: non-default argument follows default argument .Then ipython control line one.
  
 
|-
 
|-
Line 151: Line 151:
 
|-
 
|-
 
| 4:58
 
| 4:58
| '''See the definition of linspace using <tt>question mark</tt> 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.
  
 
|-
 
|-
Line 163: Line 163:
 
|-
 
|-
 
| 5:17
 
| 5:17
| <nowiki>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
 
| 5:26
| Please read the content on your terminal. <pause></nowiki>
+
| Please read the content on your terminal.
  
 
|-
 
|-
Line 175: Line 175:
 
|-
 
|-
 
| 5:35
 
| 5:35
| '''Redefine the function <tt>welcome</tt> with a default value of'''
+
| '''Redefine the function welcome with a default value of'''
  
 
|-
 
|-
 
| 5:41
 
| 5:41
| "Hello" to the <tt>greet</tt> argument.  
+
| "Hello" to the greet argument.  
  
 
|-
 
|-
Line 198: Line 198:
 
|-
 
|-
 
| 6:17
 
| 6:17
| As we can see, we get the output as <tt>Hello World</tt>.
+
| As we can see, we get the output as Hello World .
  
 
|-
 
|-
Line 222: Line 222:
 
|-
 
|-
 
| 6:52
 
| 6:52
| <tt>loc</tt>, <tt>linewidth</tt>, <tt>xy</tt> and <tt>labels</tt> are being called with keyword arguments.
+
| loc , linewidth , xy and labels< are being called with keyword arguments.
  
 
|-
 
|-
 
| 7:00
 
| 7:00
| Let us try and understand this better using the <tt>welcome</tt> 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.  
  
 
|-
 
|-
Line 246: Line 246:
 
|-
 
|-
 
| 7:42
 
| 7:42
| So, "Hi" is the value of the argument <tt>greet</tt> and name is passed the value "Guido".
+
| So, "Hi" is the value of the argument greet and name is passed the value "Guido".
  
 
|-
 
|-
Line 262: Line 262:
 
|-
 
|-
 
| 8:17
 
| 8:17
| This call returns an error that reads, <tt>non-keyword arg after keyword arg</tt>. 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
 
| 8:30
| That brings us to the end of what we wanted to learn about <tt>keyword</tt> arguments.
+
| That brings us to the end of what we wanted to learn about keyword arguments.
  
 
|-
 
|-
Line 282: Line 282:
 
|-
 
|-
 
| 9:11
 
| 9:11
| Apart from the standard library there are other libraries like <tt>pylab</tt>, <tt>scipy</tt>, 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.
  
 
|-
 
|-
Line 334: Line 334:
 
|-
 
|-
 
| 10:21
 
| 10:21
|  def seperator within bracket count=40 comma  char comma  show=False colon
+
|  def separator within bracket count=40 comma  char comma  show=False colon
 
   if show colon
 
   if show colon
 
   print char  star  count
 
   print char  star  count
Line 349: Line 349:
 
|-
 
|-
 
| 10:47
 
| 10:47
|  '''only keyword arguments can be in any order, but should be called'''<br/> at the beginning.
+
|  '''only keyword arguments can be in any order, but should be called''' at the beginning.
  
 
|-
 
|-

Revision as of 16:57, 19 March 2013

Visual Cue Narration
0:01 Hello friends and Welcome to the tutorial on 'advanced features of functions'.
0: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.
0:23 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with functions".
0:32 Let us Start the ipython interpreter
0:36 ipython hypen pylab in command
0:43 Let's use the round function as an example to understand what a default value of an argument means.
0:49 Let's type the following expressions in the terminal.
0:52 Type round within bracket 2.484 and hit enter.

Then type round within bracket 2.484 comma 2

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

(After hitting enter type)

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

welcome within bracket within double quotes Hello comma within double quotes James

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

7:37 When no keyword is specified, the arguments are allotted based on their position.
7:42 So, "Hi" is the value of the argument greet and name is passed the value "Guido".
7:48 If we type in the terminal welcome within bracket name= within double quotes Guido comma greet= within double quotes Hey exclamation
8:02 When keyword arguments are used, the arguments can be called in any order.
8:07 And if we call our function as,welcome within bracket name="Guido" comma "Hey"
8: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.
8:30 That brings us to the end of what we wanted to learn about keyword arguments.
8:37 Before defining a function of your own, make sure that you check the standard library, for a similar function.
8:43 Python is popularly called a "Batteries included" language, for the huge library that comes along with it.
8:55 Math functions - abs, sin, .... Plot functions - plot, bar, pie ... Boolean functions - and, or, not ...
9:11 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 pylab has
9:23 plot, bar, contour, boxplot, errorbar, log, polar, quiver, semilog
9:32 scipy (modules)has
9:35 fftpack, stats, linalg, ndimage, signal, optimize, integrate
9:46 This brings us to the end of this tutorial.
9:48 In this tutorial, we have learnt to, 1. Define functions with default arguments.
9:53 2. Call functions using keyword arguments.
9: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, but should be called at the end.
11:10 And 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