Python/C4/Advanced-features-of-functions/English-timed

From Script | Spoken-Tutorial
Revision as of 17:44, 2 January 2013 by Minal (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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. <pause>
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 seperator 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