Difference between revisions of "Python-3.4.3/C4/Advanced-Features-of-Functions/English"
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| <ce...") |
Nancyvarkey (Talk | contribs) |
||
Line 20: | Line 20: | ||
* Define and call '''functions''' with '''keyword''' '''arguments''' | * Define and call '''functions''' with '''keyword''' '''arguments''' | ||
* Define and call '''functions''' with '''arbitrary''' '''arguments''' | * Define and call '''functions''' with '''arbitrary''' '''arguments''' | ||
− | * Learn some of the '''built-in functions''' available in '''Python | + | * Learn some of the '''built-in functions''' available in '''Python standard library'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 33: | Line 31: | ||
* '''Python 3.4.3''' | * '''Python 3.4.3''' | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 52: | Line 48: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First let us see about default arguments in Python. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| First let us see about default '''arguments''' in '''Python'''. |
− | * Function arguments can have default values in Python. | + | * '''Function arguments''' can have default values in '''Python'''. |
− | * When we call a function without a value for an argument, its default value is used if available | + | * When we call a '''function''' without a value for an '''argument''', its default value is used if available |
* Otherwise it will give error. | * Otherwise it will give error. | ||
− | |||
− | |||
|- | |- | ||
Line 76: | Line 70: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | ||
− | Type '''ipython3 '''and | + | Type '''ipython3 '''and press''' Enter'''. |
Line 83: | Line 77: | ||
− | From here onwards, remember to press the '''Enter''' key after typing every command on the '''terminal'''. | + | From here onwards, remember to press the '''Enter''' key after typing every '''command''' on the '''terminal'''. |
|- | |- | ||
Line 97: | Line 91: | ||
Highlight name | Highlight name | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us define a | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us define a '''function Welcome'''. |
Line 106: | Line 100: | ||
* '''welcome''' is the '''function''' name | * '''welcome''' is the '''function''' name | ||
− | * '''greet''' is the argument with no default values and | + | * '''greet''' is the '''argument''' with no default values and |
− | * the '''name''' | + | * the '''name argument''' has a default value '''World'''. |
− | In a '''function,''' all the '''arguments''' with | + | In a '''function,''' all the '''arguments''' with default values should come after non-default '''arguments'''. |
|- | |- | ||
Line 115: | Line 109: | ||
'''welcome("Hi", "Chandru") ''' | '''welcome("Hi", "Chandru") ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us first call the '''function | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us first call the '''function welcome''' with two '''arguments''', one for '''greet''' and other for '''name'''. |
Line 148: | Line 142: | ||
* '''“Hello”''' is passed to the parameter '''greet '''and | * '''“Hello”''' is passed to the parameter '''greet '''and | ||
* '''“World” '''is the default value of the '''name''' parameter. | * '''“World” '''is the default value of the '''name''' parameter. | ||
− | |||
− | |||
|- | |- | ||
Line 171: | Line 163: | ||
− | Here, '''welcome | + | Here, '''welcome function''' still works since we have provided default value for '''age'''. |
− | Default values allow us to add new parameters to an existing function. | + | Default values allow us to add new '''parameters''' to an existing '''function'''. |
− | It will not break the existing usage of the function. | + | It will not break the existing usage of the '''function'''. |
Line 194: | Line 186: | ||
Exercise 1 | Exercise 1 | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Redefine the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Redefine the '''function welcome''', by interchanging its '''arguments'''. |
− | Place the '''name''' | + | Place the '''name argument''' with its default value of "'''World'''" before the '''greet argument'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution. |
|- | |- | ||
Line 218: | Line 210: | ||
− | When defining a '''function''' all the '''arguments''' with | + | When defining a '''function''' all the '''arguments''' with default values should come at the end. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 235: | Line 224: | ||
Exercise 2 | Exercise 2 | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Redefine the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Redefine the '''function welcome''' with a default value of "'''Hello'''" to the '''greet argument'''. |
− | Then, call the function without any arguments. | + | Then, call the '''function''' without any '''arguments'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Switch to the '''terminal''' for the solution. |
|- | |- | ||
Line 267: | Line 256: | ||
− | Default values of both parameters are used since function is called without passing any value. | + | Default values of both '''parameters''' are used since '''function''' is called without passing any value. |
|- | |- | ||
Line 279: | Line 268: | ||
− | * We no need to remember the order of '''arguments '''while calling | + | * We no need to remember the order of '''arguments '''while calling '''functions''' by passing''' keyword arguments'''. |
− | * Instead, we can use '''name''' of the '''argument''' to pass a '''value '''to it | + | * Instead, we can use '''name''' of the '''argument''' to pass a '''value '''to it. |
− | + | ||
− | + | ||
|- | |- | ||
Line 298: | Line 285: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us define a function with name '''marks '''which takes three marks as '''arguments'''. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us define a '''function''' with name '''marks '''which takes three marks as '''arguments'''. |
Line 304: | Line 291: | ||
− | Then we will call the '''marks ''' | + | Then we will call the '''marks function''' without specifying the '''keywords'''. |
Line 347: | Line 334: | ||
− | But, the '''keyword''' | + | But, the '''keyword arguments''' should be specified at the end. |
|- | |- | ||
Line 370: | Line 357: | ||
'''marks(second=34,first=23,third=45)''' | '''marks(second=34,first=23,third=45)''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can pass all the parameters as '''keyword arguments'''. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can pass all the '''parameters''' as '''keyword arguments'''. |
Line 396: | Line 383: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn to define a | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will learn to define a '''function''' to take only '''keyword arguments.''' |
Line 402: | Line 389: | ||
− | Then to call the function, type, | + | 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''' | '''marks '''''inside brackets '''''second '''''is equal to '''''34 '''''comma '''''first '''''is equal to '''''23 '''''comma '''''third '''''is equal to '''''45''' | ||
− | Note the '''asterisk''' symbol at the starting of parameters. | + | Note the '''asterisk''' symbol at the starting of '''parameters'''. |
− | It restricts the | + | It restricts the '''function''' to accept '''keyword''' only '''arguments.''' |
|- | |- | ||
Line 422: | Line 409: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Again we will try to call the function without '''keyword arguments.''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Again we will try to call the '''function''' without '''keyword arguments.''' |
Line 439: | Line 426: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us learn to use | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next let us learn to use '''arbitrary arguments'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | * We may not always know in advance the number of '''arguments''' that will be passed into a '''function'''. | ||
+ | * Use an asterisk(*) before an '''argument''' name to denote '''arbitrary''' number of '''arguments'''. | ||
|- | |- | ||
Line 459: | Line 444: | ||
'''family("Duryodhana", "Dushasana")''' | '''family("Duryodhana", "Dushasana")''' | ||
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can define a '''function''' to accept any number of '''positional arguments'''. | |
− | + | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can define a function to accept any number of positional arguments. | + | |
Line 472: | Line 455: | ||
− | Here, we have called the function with multiple arguments. | + | Here, we have called the '''function''' with multiple '''arguments'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | These '''arguments''' get wrapped up into a '''tuple''' while passed into the '''function'''. | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Type, | ||
Line 490: | Line 469: | ||
− | As you can see, we can call the | + | As you can see, we can call the '''function family '''with more values passed to the '''names argument'''. |
− | We have passed 2 values in the first call and 4 values in the second call to the | + | We have passed 2 values in the first call and 4 values in the second '''call''' to the '''function family'''. |
− | In both cases these values are assigned to the | + | In both cases these values are assigned to the '''parameter names.''' |
|- | |- | ||
Line 515: | Line 494: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also define a function to receive arbitrary number of '''keyword''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| We can also define a '''function''' to receive '''arbitrary''' number of '''keyword arguments.''' |
Line 521: | Line 500: | ||
− | Then to call the function type as shown. | + | Then to call the '''function''' type as shown. |
− | The '''person ''' | + | The '''person function''' prints a '''dictionary''' of '''keyword arguments''' passed to it. |
− | Note the '''double | + | Note the '''double asterisk symbol '''at the beginning of the '''parameter''' names. |
Line 537: | Line 516: | ||
And the '''double asterisk (**) '''symbol is used to accept '''arbitrary number of keyword arguments.''' | And the '''double asterisk (**) '''symbol is used to accept '''arbitrary number of keyword arguments.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 548: | Line 524: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now call the '''person ''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Now call the '''person function''' as shown. |
− | Here we have passed 3 keyword arguments in the first case and 4 in the second case. | + | Here we have passed 3 '''keyword arguments''' in the first case and 4 in the second case. |
− | The function prints a dictionary of keyword arguments with values. | + | The '''function''' prints a '''dictionary''' of '''keyword arguments''' with values. |
|- | |- | ||
Line 569: | Line 545: | ||
* '''help()''' | * '''help()''' | ||
− | You can visit this link to get the full list of built-in functions and their usage. | + | You can visit this link to get the full list of '''built-in functions''' and their usage. |
|- | |- | ||
Line 581: | Line 557: | ||
− | In this tutorial, we have learned to define | + | In this tutorial, we have learned to define '''functions''' with, |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | * '''Default arguments''' | ||
+ | * '''keyword arguments''' | ||
+ | * '''Arbitrary arguments''' | ||
|- | |- | ||
Line 599: | Line 573: | ||
− | * All the arguments of a function cannot have default values. - True or False? | + | * All the '''arguments''' of a '''function''' cannot have default values. - True or False? |
− | * The following is a valid function definition. True or False? | + | * The following is a valid '''function''' definition. True or False? |
− | * While calling a function, which one is correct in the following | + | * While calling a '''function''', which one is correct in the following. |
− | + | ||
− | + | ||
|- | |- | ||
Line 615: | Line 587: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answers,''' ''' | ||
− | # False. All the arguments of a Python function can have default values. | + | # False. All the '''arguments''' of a '''Python function''' can have default values. |
− | # False. All the parameters with default arguments should be defined at the end. | + | # False. All the '''parameters''' with default '''arguments''' should be defined at the end. |
− | # While calling a function, only keyword arguments can be in any order, but should be called at the end. | + | # While '''calling''' a '''function''', only '''keyword arguments''' can be in any order, but should be called at the end. |
− | + | ||
− | + | ||
|- | |- |
Revision as of 18:49, 16 October 2018
|
|
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, one for greet and other for name.
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.
welcome open and close brackets
|
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. |