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

From Script | Spoken-Tutorial
Revision as of 16:28, 20 February 2017 by PoojaMoolya (Talk | contribs)

Jump to: navigation, search
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,

Assign default values to arguments, when defining functions. Define and call functions with keyword arguments. 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 Call functions using keyword arguments.
09:55 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 All arguments of a function cannot have default values. - True or False?
10:14 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 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 False.
11:15 All arguments of a Python function can have default values.
11:21 False.
11:23 All parameters with default arguments should be defined at the end.
11:27 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