Python-3.4.3/C4/Advanced-Features-of-Functions/English
|
|
Show Slide title | Welcome to the spoken tutorial on Advanced Features of Functions. |
Show Slide
Objectives
|
In this tutorial, we will learn to-
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisite slide |
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Slide:
|
First let us see about default arguments in Python.
|
Open the terminal |
Let us start ipython.
|
Type ipython3 and press Enter.
|
Type ipython3 and press Enter.
|
Type,
def welcome(greet,name="World"): print (greet, name)
|
Let us define a function Welcome.
In a function, all the arguments with default values should come after non-default arguments. |
Type,
welcome("Hi", "Chandru") |
Let us first call the function welcome with two arguments
Type, welcome inside brackets inside double quotes Hi comma inside double quotes Chandru |
Point to the output | We get the expected welcome message, Hi Chandru. |
Type,
welcome("Hello")
|
Now let us call the function with one argument only.
welcome inside brackets inside double quotes Hello
|
Type,
def welcome(greet,age = 23, name="World"): print (greet, name,age)
welcome("Hello")
|
Let us add another parameter age with default value as 23 as shown.
|
Pause the video.
| |
Show Slide
|
Redefine the function welcome, by interchanging its arguments.
|
Switch terminal | Switch to the terminal for the solution. |
Type,
def welcome(name="World", greet): print (greet, name)
|
Type as shown.
|
Pause the video.
| |
Show Slide
|
Redefine the function welcome with a default value of "Hello" to the greet argument.
|
Switch terminal | Switch to the terminal for the solution. |
Type,
def welcome(greet="Hello", name="World"): print (greet, name)
|
Type as shown.
|
Show Slide
keyword arguments
|
Next let us see what are keyword arguments.
|
Type,
def marks(first,second,third): print("first: %d second: %d and third: %d" %(first,second,third))
marks(34,23,45)
|
Let us define a function with name marks which takes three marks as arguments.
|
Type,
marks(34,45,23) |
To confirm this, we will try with different values.
marks inside brackets 34 comma 45 comma 23
|
Type,
marks(34,23,third=45)
|
Now let us pass two values without keyword and other one with keyword.
marks inside brackets 34 comma 23 comma third is equal to 45
|
Type,
marks(34,second=23,45)
|
Now type,
marks inside brackets 34 comma second is equal to 23 comma 45
|
Type,
marks(second=34,first=23,third=45) |
We can pass all the parameters as keyword arguments.
marks inside brackets second is equal to 34 comma first is equal to 23 comma third is equal to 45
“first: 23 second: 34 and third: 45” |
Type,
def marks(*, first, second, third): print("first: %d second: %d and third: %d" %(first,second,third))
marks(second=34,first=23,third=45)
|
Next we will learn to define a function to take only keyword arguments.
marks inside brackets second is equal to 34 comma first is equal to 23 comma third is equal to 45
|
Type,
marks(45, 34, 23)
|
Again we will try to call the function without keyword arguments.
marks inside brackets 45 comma 34 comma 23
This way we can enforce usage of keyword only arguments without positional arguments. |
Slide:
|
Next let us learn to use arbitrary arguments.
|
Type,
def family(*names): print(names)
family("Duryodhana", "Dushasana") |
We can define a function to accept any number of positional arguments.
family inside brackets inside double quotes Duryodhana comma inside double quotes Dushasana
|
Type,
family("Duryodhana", "Dushasana", "Dushla", "Jalsandha")
|
Now type as shown.
|
Type,
def person(**attributes): print(attributes)
person(name="John",age=34,height=182)
|
We can also define a function to receive arbitrary number of keyword arguments.
|
Type,
person(name="Lisa",age=27,height=162,weight=58)
|
Now call the person function as shown.
|
Show Slide
Built-in functions |
Python also provides built-in functions.
Some are
You can visit this link to get the full list of built-in functions and their usage. |
Show Slide
Summary slide
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve.
|
Show Slide
|
And the answers,
|
Show Slide Forum | Please post your timed queries in this forum. |
Show Slide
Fossee Forum |
Please post your general queries on Python in this forum. |
Slide Textbook Companion | FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgement |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Show Slide Thank You | This is Priya from IIT Bombay signing off. Thanks for watching. |