Python-3.4.3/C4/Advanced-Features-of-Functions/English-timed

From Script | Spoken-Tutorial
Revision as of 18:01, 4 June 2019 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time
Narration
00:01 Welcome to the spoken tutorial on Advanced Features of Functions.
00:07 In this tutorial, we will learn to- Assign default values to arguments, when defining functions
00:15 Define and call functions with keyword arguments
00:20 Define and call functions with arbitrary arguments
00:25 Learn some of the built-in functions available in Python standard library.
00:31 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

00:38 Python 3.4.3 and IPython 5.1.0
00:45 To practise this tutorial, you should know how to

run basic Python commands on the IPython console

use functions.

00:55 If not, see the relevant Python tutorials on this website.
01:00 First let us see about default arguments in Python.
01:05 Function arguments can have default values in Python.
01:10 When we call a function without a value for an argument, its default value is used if available
01:18 Otherwise it will give error.
01:21 Let us start ipython.

Open the terminal.

01:26 Type ipython3 and press Enter.
01:30 From here onwards, remember to press the Enter key after typing every command on the terminal.
01:37 Let us define a function Welcome.
01:40 Type the code as shown.
01:44 Here, welcome is the function name

greet is the argument with no default values and the name argument has a default value World.

01:55 In a function, all the arguments with default values should come after non-default arguments.
02:03 Let us first call the function welcome with two arguments
02:08 Type, welcome inside brackets inside double quotes Hi comma inside double quotes Chandru
02:17 We get the expected welcome message, Hi Chandru.
02:21 Now let us call the function with one argument only.
02:25 Type, welcome inside brackets inside double quotes Hello
02:31 We get the output as Hello World.
02:35 Here, “Hello” is passed to the parameter greet and
02:39 “World” is the default value of the name parameter.
02:44 Let us add another parameter age with default value as 23 as shown.
02:51 Now type, welcome inside brackets inside double quotes Hello
02:58 Here, welcome function still works since we have provided default value for age.
03:06 Default values allow us to add new parameters to an existing function.
03:12 It will not break the existing usage of the function.
03:16 Pause the video.

Try this exercise and then resume the video.

03:22 Redefine the function welcome, by interchanging its arguments.
03:27 Place the name argument with its default value of "World" before the greet argument.
03:34 Switch to the terminal for the solution.
03:38 Type as shown.
03:42 We get an error that reads SyntaxError: non-default argument follows default argument.
03:50 When defining a function all the arguments with default values should come at the end.
03:57 Pause the video.

Try this exercise and then resume the video.

04:03 Redefine the function welcome with a default value of "Hello" to the greet argument.
04:10 Then, call the function without any arguments.
04:14 Switch to the terminal for the solution.
04:18 Type as shown.
04:21 Then type, welcome open and close brackets
04:26 As we can see, we get the output as Hello World.
04:31 Default values of both parameters are used since function is called without passing any value.
04:39 Next let us see what are keyword arguments.
04:43 We no need to remember the order of arguments while calling functions by passing keyword arguments.
04:51 Instead, we can use name of the argument to pass a value to it.
04:57 Let us define a function with name marks which takes three marks as arguments.
05:04 Type as shown.

Then we will call the marks function without specifying the keywords.

05:11 Type, marks inside brackets 34 comma 23 comma 45
05:18 We get the output as “first: 34 second: 23 and third: 45
05:24 Here the values 34, 23 and 45 are passed according to the position.
05:31 To confirm this, we will try with different values.
05:35 Type, marks inside brackets 34 comma 45 comma 23
05:42 We can see that the printed values are changed since they are passed according to the position.
05:49 Now let us pass two values without keyword and other one with keyword.
05:55 Type, marks inside brackets 34 comma 23 comma third is equal to 45
06:04 Here first two values are passed according to the position and the third as keyword argument.
06:12 But, the keyword arguments should be specified at the end.
06:17 Now type, marks inside brackets 34 comma second is equal to 23 comma 45
06:26 We get the SyntaxError, positional argument follows keyword argument.
06:32 This is because here the keyword argument is not specified at the end.
06:38 We can pass all the parameters as keyword arguments.
06:43 Type, marks inside brackets second is equal to 34 comma first is equal to 23 comma third is equal to 45
06:54 Here even though the order of keyword is changed, we get the output as:

first: 23 second: 34 and third: 45

07:03 Next we will learn to define a function to take only keyword arguments.
07:09 Type as shown.
07:12 Then to call the function, type,

marks inside brackets second is equal to 34 comma first is equal to 23 comma third is equal to 45

07:25 Note the asterisk symbol at the starting of parameters.
07:29 It restricts the function to accept keyword only arguments.
07:34 Again we will try to call the function without keyword arguments.
07:39 Type, marks inside brackets 45 comma 34 comma 23
07:46 It gives a TypeError as marks() takes 0 positional arguments but 3 were given.
07:53 This way we can enforce usage of keyword only arguments without positional arguments.
08:00 Next let us learn to use arbitrary arguments.
08:05 We may not always know in advance the number of arguments that will be passed into a function.
08:12 Use an asterisk(*) before an argument name to denote arbitrary number of arguments.
08:19 We can define a function to accept any number of positional arguments.
08:25 Type as shown.

Then type, family inside brackets inside double quotes Duryodhana comma inside double quotes Dushasana

08:38 Here, we have called the function with multiple arguments.
08:43 These arguments get wrapped up into a tuple while passed into the function.
08:49 Now type as shown.
08:53 As you can see, we can call the function family with more values passed to the names argument.
09:01 We have passed 2 values in the first call and 4 values in the second call to the function family.
09:09 In both cases these values are assigned to the parameter names.
09:15 We can also define a function to receive arbitrary number of keyword arguments.


09:21 Type as shown.

Then to call the function type as shown.

09:29 The person function prints a dictionary of keyword arguments passed to it.
09:35 Note the double asterisk symbol at the beginning of the parameter names.
09:40 It enables to pass zero or more keyword arguments.
09:45 Note: Single asterisk (*) symbol is used to accept arbitrary number of positional arguments.
09:53 And the double asterisk (**) symbol is used to accept arbitrary number of keyword arguments.
10:01 Now call the person function as shown.
10:06 Here we have passed 3 keyword arguments in the first case and 4 in the second case.
10:14 The function prints a dictionary of keyword arguments with values.
10:20 Python also provides built-in functions.
10:22 Some are abs()

any()

10:29 dir()

help()

10:33 You can visit this link to get the full list of built-in functions and their usage.
10:39 This brings us to the end of this tutorial. Let us summarize.
10:45 In this tutorial, we have learned to define functions with, Default arguments, keyword arguments and Arbitrary arguments
10:55 Here are some self assessment questions for you to solve.


11:00 All the arguments of a function cannot have default values. - True or False?
11:08 The following is a valid function definition. True or False?
11:15 While calling a function, which one is correct in the following.
11:20 And the answers,

First. False. All the arguments of a Python function can have default values.

11:28 Second. False. All the parameters with default arguments should be defined at the end.
11:35 Third. While calling a function, only keyword arguments can be in any order, but should be called at the end.
11:44 Please post your timed queries in this forum.
11:48 Please post your general queries on Python in this forum.
11:53 FOSSEE team coordinates the TBC project.
11:57 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

12:06 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya