Difference between revisions of "Python-3.4.3/C4/Getting-Started-with-Functions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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...")
 
 
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 arguments '''a''' and '''b''' to the function '''avg.'''
+
| 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''' '''circle '''which returns the '''area''' and '''perimeter''' of a '''circle''' with given radius '''r'''.
+
| 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 '''function requires us to '''return''' two values.  
+
The '''circle function''' requires us to '''return''' two values.  
  
  
A '''python''' '''function''' can return any number of values in the form of a tuple.
+
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''' '''name'''
+
* Call a '''function''' by specifying the '''function''' name
* Write '''docstrings''' to a '''function''' by putting it as a '''triple quoted string'''
+
* 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.
2. 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''' '''function'''.
+
# Any number of '''arguments''' can be passed to a '''Python function'''.
  
2. We can write a function to find the area of a rectangle as:  
+
# 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

Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Getting started with functions.
Show Slide

Objectives


In this tutorial, we will learn to,
  • Define a function
  • Define functions with arguments and
  • use docstrings.
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisite

To practise this tutorial, you should know how to use tuples.


If not, see the relevant Python tutorials on this website.

Show Slide


Function

First we will learn about functions.


  • A function is a portion of code within a larger program that performs a specific task.
  • Functions are useful in reusing the code and eliminate code redundancy.
  • Functions are also used to organise our code into manageable blocks.
Slide:

Syntax of functions:


def function_name(parameters):"""docstring""" statement(s)

return [expression]


Highlight according to narration

Here is the syntax for defining the functions.


def is the keyword which defines 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.

It is an optional, but recommended.


Statement makes the function body and it must have 4 indentation level.


return statement is to return a value from the function and it is also indented by 4 spaces.

Show Slide:

Example: Function


f(x) a mathematical function


def f(x):

return x*x


Now we will understand the functions with an example.


Consider a mathematical function f of x is equal to x squared.


Here x is a variable. f of x changes when x changes.


Let us define our function f of x.


The first line def f of x is used to define the function name and its parameters.


The second line uses the function parameters to return the required value.

Open terminal Let us start ipython.


Open the terminal.

Type, ipython3

then press Enter

Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type,

def f(x):

return x*x

Type,

def f inside brackets x colon

return x asterisk x


and press the Enter key two times.

Type,

f(2)


f(2.5)


Let us call the f of x with different arguments.


Type,

f inside brackets 2


f inside brackets 2.5


It returned 4 and 6.25 respectively.

Type,

def greet():

print ("No function arguments")


Now let us see how to write functions without arguments.


Type the code as shown.


It defines a new function named greet which will print "No function 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.


The function greet neither takes any arguments nor returns any value.

Show Slide

docstrings


Next we will learn how to comment in a code.


Documenting/commenting code is a good practice.


Docstrings are triple quoted comments entered just after the function definition.


It implies what the function does.

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.


and press the Enter key two times.


Comments within triple quotes give a clear explanation about the code.

Type,

avg?


Type,

avg question mark


Here we can see the docstring of the function avg.

Type,

avg(3, 5)

Now let us pass 3 and 5 as values to the arguments a and b to the function avg.


Type, avg inside brackets 3 comma 5


We get 4.0 as output.

Pause the video.


Try this exercise and then resume 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.


Press the Enter key two times.


The circle function requires us to return two values.


A python function can return any number of values in the form of a tuple.

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


We can see the output as area and perimeter of a circle of radius 6.

Show Slide

Summary


This brings us to the end of this tutorial. Let us summarize.


In this tutorial, we have learnt to,

  • Define functions in Python
  • Call a function by specifying the function name
  • Write docstrings to a function by putting it as a triple quoted string
  • Pass parameters to a function
  • Return values from a function.
Show Slide


Self assessment questions

Here are some self assessment questions for you to solve
  1. How many arguments can be passed to a Python function?
  2. Write a function to find the area of a rectangle.
Show Slide

Solution of self assessment questions


And the answers,
  1. Any number of arguments can be passed to a Python function.
  1. We can write a function to find the area of a rectangle as:

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

http://spoken-tutorial.org

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.

Contributors and Content Editors

Nancyvarkey, Priyacst