Difference between revisions of "Python-3.4.3/C4/Getting-Started-with-Functions/English"
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:n...") |
Nancyvarkey (Talk | contribs) |
||
Line 20: | Line 20: | ||
* Define '''functions''' with '''arguments''' and | * Define '''functions''' with '''arguments''' and | ||
* use '''docstrings'''. | * use '''docstrings'''. | ||
− | |||
− | |||
|- | |- | ||
Line 32: | Line 30: | ||
* '''Python 3.4.3 '''and | * '''Python 3.4.3 '''and | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 39: | Line 35: | ||
Pre-requisite | Pre-requisite | ||
− | |||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to use '''tuples.''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| To practise this tutorial, you should know how to use '''tuples.''' | ||
Line 58: | Line 51: | ||
* '''Functions '''are useful in reusing the '''code''' and eliminate code redundancy. | * '''Functions '''are useful in reusing the '''code''' and eliminate code redundancy. | ||
* '''Functions''' are also used to organise our code into manageable blocks. | * '''Functions''' are also used to organise our code into manageable blocks. | ||
− | |||
− | |||
|- | |- | ||
Line 73: | Line 64: | ||
Highlight according to narration | Highlight according to narration | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here is the syntax for defining the functions. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here is the syntax for defining the '''functions'''. |
− | '''def''' is the '''keyword '''which defines the function name. | + | '''def''' is the '''keyword '''which defines the '''function''' name. |
− | colon is used to mark the end of the function name. | + | colon is used to mark the end of the '''function''' name. |
− | '''docstring''' is the documentation string to describe what the function does. | + | '''docstring''' is the documentation string to describe what the '''function''' does. |
It is an optional, but recommended. | It is an optional, but recommended. | ||
Line 107: | Line 98: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now we will understand the functions with an example. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now we will understand the '''functions''' with an example. |
Line 122: | Line 113: | ||
− | The second line uses the function parameters to return the required value. | + | The second line uses the '''function parameters''' to return the required value. |
|- | |- | ||
Line 129: | Line 120: | ||
− | Open the terminal. | + | Open the '''terminal'''. |
|- | |- | ||
Line 138: | Line 129: | ||
− | 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 187: | Line 178: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now let us see how to write functions without arguments. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now let us see how to write '''functions''' without '''arguments'''. |
Line 199: | Line 190: | ||
'''greet()''' | '''greet()''' | ||
− | |||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now we will call the '''function''' as,'''greet '''''open and close parentheses'' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now we will call the '''function''' as,'''greet '''''open and close parentheses'' | ||
Line 218: | Line 206: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will learn how to comment in a code. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next we will learn how to '''comment''' in a '''code'''. |
− | Documenting/commenting code is a good practice. | + | '''Documenting/commenting code''' is a good practice. |
Line 243: | Line 231: | ||
− | Comments within triple quotes give a clear explanation about the code. | + | '''Comments''' within triple quotes give a clear explanation about the '''code'''. |
|- | |- | ||
Line 254: | Line 242: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, | ||
− | '''avg '''''question mark | + | '''avg '''''question mark'' |
Line 263: | Line 251: | ||
'''avg(3, 5)''' | '''avg(3, 5)''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now let us pass 3 and 5 as values to the | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Now let us pass 3 and 5 as values to the '''arguments a''' and '''b''' to the '''function avg.''' |
Line 282: | Line 270: | ||
Assignment 1 | Assignment 1 | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Write a '''function | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Write a '''function circle '''which returns the '''area''' and '''perimeter''' of a '''circle''' with given radius '''r'''. |
|- | |- | ||
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch terminal | | style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch terminal | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the terminal for the solution. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the '''terminal''' for the solution. |
|- | |- | ||
Line 302: | Line 290: | ||
'''perimeter = 2 * pi * r return area, perimeter''' | '''perimeter = 2 * pi * r return area, perimeter''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type the code as shown. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type the '''code''' as shown. |
Line 308: | Line 296: | ||
− | The '''circle ''' | + | The '''circle function''' requires us to '''return''' two values. |
− | A '''python | + | A '''python function''' can return any number of values in the form of a '''tuple'''. |
|- | |- | ||
Line 325: | Line 313: | ||
'''print (a, p)''' | '''print (a, p)''' | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Type, | ||
Line 348: | Line 334: | ||
* Define '''functions''' in '''Python''' | * Define '''functions''' in '''Python''' | ||
− | * Call a '''function''' by specifying the '''function | + | * Call a '''function''' by specifying the '''function''' name |
− | * Write '''docstrings''' to a '''function''' by putting it as a ''' | + | * Write '''docstrings''' to a '''function''' by putting it as a triple quoted '''string''' |
* Pass '''parameters''' to a '''function''' | * Pass '''parameters''' to a '''function''' | ||
* Return '''values''' from a '''function'''. | * Return '''values''' from a '''function'''. | ||
− | |||
− | |||
|- | |- | ||
Line 362: | Line 346: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve | ||
− | # How many arguments can be passed to a Python function? | + | # How many '''arguments''' can be passed to a '''Python function'''? |
− | + | # Write a '''function''' to find the area of a rectangle. | |
− | + | ||
|- | |- | ||
Line 375: | Line 358: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;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.095cm;padding-right:0.191cm;"| And the answers, | ||
− | # Any number of '''arguments''' can be passed to a '''Python | + | # Any number of '''arguments''' can be passed to a '''Python function'''. |
− | + | # We can write a '''function''' to find the area of a rectangle as: | |
'''def rectangle''''' underscore '''''area '''''inside brackets '''''l '''''comma''''' b '''''colon'' | '''def rectangle''''' underscore '''''area '''''inside brackets '''''l '''''comma''''' b '''''colon'' |
Latest revision as of 18:20, 16 October 2018
|
|
Show Slide title | Welcome to the spoken tutorial on Getting started with 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 |
To practise this tutorial, you should know how to use tuples.
|
Show Slide
|
First we will learn about functions.
|
Slide:
Syntax of functions:
return [expression]
|
Here is the syntax for defining the functions.
It is an optional, but recommended.
|
Show Slide:
Example: Function
return x*x
|
Now we will understand the functions with an example.
|
Open terminal | Let us start ipython.
|
Type, ipython3
then press Enter |
Type ipython3 and press Enter.
|
Type,
def f(x): return x*x |
Type,
def f inside brackets x colon return x asterisk x
|
Type,
f(2)
|
Let us call the f of x with different arguments.
f inside brackets 2
|
Type,
def greet(): print ("No function arguments")
|
Now let us see how to write functions without arguments.
|
Type,
greet() |
Now we will call the function as,greet open and close parentheses |
Note that it is not mandatory for a function to return values.
| |
Show Slide
docstrings
|
Next we will learn how to comment in a code.
|
Type, (type manually)def avg(a, b):
""" avg takes two numbers as input a & b. Returns the average of a and b""" return (a + b) / 2.0 |
Let us write a function which returns average of two numbers.
Type the code as shown.
|
Type,
avg?
|
Type,
avg question mark
|
Type,
avg(3, 5) |
Now let us pass 3 and 5 as values to the arguments a and b to the function avg.
|
Pause the video.
| |
Show Slide
Assignment 1 |
Write a function circle which returns the area and perimeter of a circle with given radius r. |
Switch terminal | Switch back to the terminal for the solution. |
Type,
def circle(r): """returns area and perimeter of a circle given radius r""" pi = 3.14 area = pi * r * r perimeter = 2 * pi * r return area, perimeter |
Type the code as shown.
|
Type,
a, p = circle(6) |
Let us call the function circle as,
a comma p is equal to circle inside brackets 6 |
Type,
print (a, p) |
Type,
print inside brackets a comma p
|
Show Slide
Summary
|
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
|
Here are some self assessment questions for you to solve
|
Show Slide
Solution of self assessment questions
|
And the answers,
def rectangle underscore area inside brackets l comma b colon return l asterisk b |
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
Acknowledgment |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is Priya from IIT Bombay signing off.
Thanks for watching. |