Difference between revisions of "Python/C4/Getting-started-with-functions/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !Visual Cue !Narration |- | 0:01 | Hello friends and welcome to the tutorial 'Getting started with functions'. |- | 0:05 | At the end of this tutorial, you will be …')
 
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Visual Cue
+
|'''Time'''
!Narration
+
|'''Narration'''
 +
 
 
|-
 
|-
| 0:01
+
| 00:01
 
| Hello friends and welcome to the tutorial 'Getting started with functions'.
 
| Hello friends and welcome to the tutorial 'Getting started with functions'.
  
 
|-
 
|-
0:05
+
00:05
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
 
+
Define a function.
# Define a function.
+
Define functions with arguments.
# Define functions with arguments.
+
Learn about docstrings.
# Learn about docstrings.
+
Learn about function return value.
# Learn about function return value.
+
Read code.
# Read code.
+
  
 
|-
 
|-
| 0:16
+
| 00:16
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Conditionals" and "Loops".
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Conditionals" and "Loops".
  
 
|-
 
|-
| 0:22
+
| 00:22
 
| While writing code, we always want to reduce the number of lines of code, and functions is a way of reusing the code.  
 
| While writing code, we always want to reduce the number of lines of code, and functions is a way of reusing the code.  
  
 
|-
 
|-
| 0:32
+
| 00:32
 
| Thus the same lines of code can be used as many times as needed.  
 
| Thus the same lines of code can be used as many times as needed.  
  
 
|-
 
|-
| 0:35
+
| 00:35
 
| A function is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code.  
 
| A function is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code.  
  
 
|-
 
|-
| 0:43
+
| 00:43
 
| Now let us get more familiar with functions,
 
| Now let us get more familiar with functions,
  
 
|-
 
|-
| 0:48
+
| 00:48
 
| Consider a mathematical function f of x = x square.  
 
| Consider a mathematical function f of x = x square.  
  
 
|-
 
|-
| 0:53
+
| 00:53
 
| Here x is a variable and with different values of x the value of function will change.  
 
| Here x is a variable and with different values of x the value of function will change.  
  
 
|-
 
|-
| 0:58
+
| 00:58
 
| When x is one, f(1) will return the value 1 and f(2) will return us the value 4.  
 
| When x is one, f(1) will return the value 1 and f(2) will return us the value 4.  
  
 
|-
 
|-
| 1:05
+
| 01:05
 
| Let us now see how to define the function f of x in python.
 
| Let us now see how to define the function f of x in python.
  
 
|-
 
|-
| 1:10
+
| 01:10
 
| Start your ipython interpreter by typing,ipython in command line.
 
| Start your ipython interpreter by typing,ipython in command line.
  
 
|-
 
|-
| 1:17
+
| 01:17
 
| Let us define our function f of x
 
| Let us define our function f of x
  
 
|-
 
|-
|1:19
+
|01:19
|So type def f within bracket x colon
+
|So type def f within bracket x colon   return x star x
        return x star x
+
  
 
|-
 
|-
|1:29
+
|01:29
|Star denotes multipication
+
|Star denotes multiplication
  
 
|-
 
|-
| 1:34
+
| 01:34
 
| Well that defined the function, so before learning what we did let us see if it returns the expected values, try,
 
| Well that defined the function, so before learning what we did let us see if it returns the expected values, try,
  
 
|-
 
|-
|1:45
+
|01:45
|f(1)
+
|f(1) f(2)
f(2)
+
  
 
|-
 
|-
| 1:52
+
| 01:52
 
| Yes, it returned 1 and 4 respectively.  
 
| Yes, it returned 1 and 4 respectively.  
  
 
|-
 
|-
| 1:55
+
| 01:55
 
| And now let us see what we did.  
 
| And now let us see what we did.  
  
 
|-
 
|-
| 1:58
+
| 01:58
| We wrote two lines: The first line <tt>def f of x </tt> is used to define the name and the parameters to the function, and the second line is used to fix what the function is supposed to return.  
+
| We wrote two lines: The first line def f of x is used to define the name and the parameters to the function, and the second line is used to fix what the function is supposed to return.  
  
 
|-
 
|-
| 2:12
+
| 02:12
| <tt>def</tt> is a keyword and <tt>f</tt> is the name of the function and <tt>x</tt> the parameter of the function.
+
| def is a keyword and f is the name of the function and x the parameter of the function.
  
 
|-
 
|-
| 2:19
+
| 02:19
| Pause the video here, try out the following exercise and resume the video.
+
| Pause the video here, try out the following exercise and resume.
  
 
|-
 
|-
| 2:24
+
| 02:24
| '''Write a python function named <tt>cube</tt> which computes the cube of''' a given number n.
+
| '''Write a python function named cube which computes the cube of''' a given number n.
  
 
|-
 
|-
| 2:31
+
| 02:31
 
| Switch to your terminal for solution.  
 
| Switch to your terminal for solution.  
  
 
|-
 
|-
| 2:33
+
| 02:33
 
| The problem can be solved as,
 
| The problem can be solved as,
  
 
|-
 
|-
|2:36
+
|02:36
|In the terminal type def cube within bracket n colon
+
|In the terminal type def cube within bracket n colon return n star star 3
                      return n star star 3
+
  
 
|-
 
|-
| 2:48
+
| 02:48
 
| Let us check whether our function returns the cube of a number or not
 
| Let us check whether our function returns the cube of a number or not
  
 
|-
 
|-
|2:53
+
|02:53
 
|So type cube within bracket 2 and hit enter.
 
|So type cube within bracket 2 and hit enter.
  
 
|-
 
|-
| 3:00
+
| 03:00
 
| It returned 8,which means we have defined our function,the right way.  
 
| It returned 8,which means we have defined our function,the right way.  
  
 
|-
 
|-
| 3:05
+
| 03:05
 
| And now let us see how to write functions without arguments.
 
| And now let us see how to write functions without arguments.
  
 
|-
 
|-
| 3:09
+
| 03:09
| let us define a new function called <tt>greet</tt> which will print <tt>Hello World</tt>.
+
| let us define a new function called greet which will print Hello World .
  
 
|-
 
|-
|3:15
+
|03:15
 
|So type def greet() colon and hit enter.
 
|So type def greet() colon and hit enter.
  
 
|-
 
|-
|3:26
+
|03:26
|Then type print in double quotes Hello World exclamation
+
|Then type print within double quotes Hello World exclamation
  
 
|-
 
|-
| 3:39
+
| 03:39
 
| now we call the function as,greet() and hit enter.
 
| now we call the function as,greet() and hit enter.
  
 
|-
 
|-
| 3:45
+
| 03:45
 
| Well that is a function which takes no arguments.  
 
| Well that is a function which takes no arguments.  
  
 
|-
 
|-
| 3:49
+
| 03:49
 
| Also note that it is not mandatory for a function to return values.  
 
| Also note that it is not mandatory for a function to return values.  
  
 
|-
 
|-
| 3:53
+
| 03:53
| The function <tt>greet</tt> neither takes any argument nor returns any value.
+
| The function greet neither takes any argument nor returns any value.
  
 
|-
 
|-
| 3:57
+
| 03:57
 
| Now let us see how to write functions with more than one argument.
 
| Now let us see how to write functions with more than one argument.
  
 
|-
 
|-
| 4:03
+
| 04:03
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 4:08
+
| 04:08
| Write a python function named <tt>avg</tt> which computes the average of <tt>a</tt> and <tt>b</tt>.
+
| Write a python function named avg which computes the average of a and b .
  
 
|-
 
|-
| 4:16
+
| 04:16
| Switch to your terminal for solution.
+
| Switch to terminal for solution.
  
 
|-
 
|-
|4:19
+
|04:19
|Type def avg within bracket a comma b colon
+
|Type def avg within bracket a comma b colon return  within bracket a + b divided by 2
      return  within bracket a + b divided by 2
+
  
 
|-
 
|-
|4:36
+
|04:36
 
|For division we use slash.
 
|For division we use slash.
  
 
|-
 
|-
| 4:42
+
| 04:42
 
| Let us test our function,
 
| Let us test our function,
  
 
|-
 
|-
|4:44
+
|04:44
 
|Type on terminal avg within bracket 20 comma  30 and hit enter.
 
|Type on terminal avg within bracket 20 comma  30 and hit enter.
  
 
|-
 
|-
| 4:53
+
| 04:53
 
| We get the correct average, 25.
 
| We get the correct average, 25.
  
 
|-
 
|-
| 4:56
+
| 04:56
| Thus if we want a function to accept more arguments, we just list them separated with a comma between the parenthesis after the function's name in the <tt>def</tt> line.
+
| Thus if we want a function to accept more arguments, we just list them separated with a comma between the parenthesis after the function's name in the def line.
  
 
|-
 
|-
| 5:06
+
| 05:06
 
| It is always a good practice to document the code that we write, and for a function we define, we should write an abstract of what the function does, and that is called a docstring.  
 
| It is always a good practice to document the code that we write, and for a function we define, we should write an abstract of what the function does, and that is called a docstring.  
  
 
|-
 
|-
| 5:19
+
| 05:19
| Let us modify the function <tt>avg</tt> and add docstring to it.  
+
| Let us modify the function avg and add docstring to it.  
  
 
|-
 
|-
| 5:24
+
| 05:24
| Do the following,
+
| Do the following,So now in the terminal type def avg within bracket a comma b colon
  
 
|-
 
|-
|5:25
+
|05:38
|So now in the terminal type def avg within bracket a comma b colon
+
 
+
|-
+
|5:38
+
 
|Then in triple double quote you can type avg takes two numbers as input (a & b), and returns the average of a and b
 
|Then in triple double quote you can type avg takes two numbers as input (a & b), and returns the average of a and b
  
 
|-
 
|-
|5:50
+
|05:50
|Then type return return  within bracket a+b divided by 2
+
|Then type return  within bracket a+b and hit enter
  
 
|-
 
|-
|6:02
+
|06:02
 
|Note that we have a syntax error.
 
|Note that we have a syntax error.
  
 
|-
 
|-
|6:09
+
|06:09
 
|The error that we saw here is because of indentation error in return(a+b)/2
 
|The error that we saw here is because of indentation error in return(a+b)/2
  
 
|-
 
|-
|6:15
+
|06:15
 
|So just input that command again.
 
|So just input that command again.
  
 
|-
 
|-
|6:25
+
|06:25
|So type def avg within bracket a comma b colon
+
|So type def avg within bracket a comma b colon """ avg takes two numbers as input (a & b), and returns the average of a and b""" return  within bracket a+b divided by 2
 
+
  """ avg takes two numbers as input (a & b), and
+
returns the average of a and b"""
+
return  within bracket a+b divided by 2
+
  
 
|-
 
|-
| 6:45
+
| 06:45
 
| Note that docstrings are entered in the immediate line after the function definition and put as a triple quoted string.  
 
| Note that docstrings are entered in the immediate line after the function definition and put as a triple quoted string.  
  
 
|-
 
|-
| 6:55
+
| 06:55
 
| And here as far as the code functionality is concerned, we didn't do anything.
 
| And here as far as the code functionality is concerned, we didn't do anything.
  
 
|-
 
|-
| 7:00
+
| 07:00
 
|  We just added an abstract of what the function does.
 
|  We just added an abstract of what the function does.
  
 
|-
 
|-
| 7:03
+
| 07:03
 
| Now try this in the ipython interpreter.
 
| Now try this in the ipython interpreter.
  
 
|-
 
|-
|7:07
+
|07:07
 
|Type avg and question mark.
 
|Type avg and question mark.
 +
 
|-
 
|-
| 7:12
+
| 07:12
 
| It displays the docstring as we gave it.  
 
| It displays the docstring as we gave it.  
  
 
|-
 
|-
| 7:16
+
| 07:16
| Thus docstring is a good way of documenting the function we write.
+
| Thus doc string is a good way of documenting the function we write.
  
 
|-
 
|-
| 7:21
+
| 07:21
| Now type comma f question mark and hit enter.
+
| Now type f question mark and hit enter.
  
 
|-
 
|-
| 7:29
+
| 07:29
| It does have a docstring associated with it.  
+
| It does have a doc string associated with it.  
  
 
|-
 
|-
|7:37
+
|07:37
|Sorry It doesnot have a docstring associated with it.  
+
|Sorry It does not have a doc string associated with it.  
  
 
|-
 
|-
| 7:40
+
| 07:40
 
| Also we cannot infer anything from the function name, and thus we are forced to read the code to understand about the function.
 
| Also we cannot infer anything from the function name, and thus we are forced to read the code to understand about the function.
  
 
|-
 
|-
| 7:48
+
| 07:48
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 7:54
+
| 07:54
| Add docstring to the function f.
+
| Add doc string to the function f.
  
 
|-
 
|-
| 7:59
+
| 07:59
| We need to define the function again to add docstring to the function <tt>f</tt> and we do it as,
+
| We need to define the function again to add doc string to the function   f and we do it as,
  
 
|-
 
|-
| 8:06
+
| 08:06
 
| def f within bracket x colon
 
| def f within bracket x colon
  
 
|-
 
|-
| 8:13
+
| 08:13
 
|  """Accepts a number x as argument and, returns the square of the number x."""
 
|  """Accepts a number x as argument and, returns the square of the number x."""
  
 
|-
 
|-
| 8:24
+
| 08:24
|  return x star x
+
| Then type return x star x
  
 
|-
 
|-
| 8:32
+
| 08:32
 
| Let us solve one more exercise  
 
| Let us solve one more exercise  
  
 
|-
 
|-
| 8:34
+
| 08:34
| Pause the video here, try out the following exercise and resume the video.
+
| Pause the video here, try out the exercise and resume the video.
  
 
|-
 
|-
| 8:41
+
| 08:41
| '''Write a python function named <tt>circle</tt> which returns the''' area and perimeter of a circle given radius <tt>r</tt>.
+
| '''Write a python function named circle which returns the''' area and perimeter of a circle given radius r .
  
 
|-
 
|-
| 8:52
+
| 08:52
 
| Switch to the terminal for solution.
 
| Switch to the terminal for solution.
  
 
|-
 
|-
| 8:57
+
| 08:57
 
| The problem requires us to return two values instead of one which we were doing till now.  
 
| The problem requires us to return two values instead of one which we were doing till now.  
  
 
|-
 
|-
| 9:03
+
| 09:03
 
| We can solve the problem as,
 
| We can solve the problem as,
  
 
|-
 
|-
|9:05
+
|09:05
|So now we can type in terminal  
+
|So now we can type in terminal def circle within bracket r colon """returns area and perimeter of a circle given radius r"""
  def circle within bracket r colon
+
pi = 3.14
"""returns area and perimeter of a circle given radius r"""
+
area = pi star r star r
pi = 3.14
+
perimeter = 2 star pi star r
area = pi star r star r
+
return area comma  perimeter and hit enter.
perimeter = 2 star pi star r
+
return area comma  perimeter and hit enter.
+
  
 
|-
 
|-
Line 355: Line 342:
 
|-
 
|-
 
| 10:09
 
| 10:09
| Let us call the function <tt>circle</tt> as,
+
| Let us call the function circle as,
  
 
|-
 
|-
 
|10:12
 
|10:12
|Typing it on terminal a comma  p = circle within bracket 6
+
|Typing it on terminal a comma  p = circle within bracket 6. print a, print p
print a
+
print p
+
  
 
|-
 
|-
Line 369: Line 354:
 
|-
 
|-
 
| 10:46
 
| 10:46
| Pause here and try to figure out what the function <tt>what</tt> does.
+
| Pause the video here and try to figure out what the function what does.
  
 
|-
 
|-
Line 377: Line 362:
 
|-
 
|-
 
| 10:58
 
| 10:58
| <nowiki>if n less than 0 colon n = -n while n greater than 0 colon</nowiki>
+
| if n less than 0 colon n = -n while n greater than 0 colon  
  
 
|-
 
|-
Line 397: Line 382:
 
|-
 
|-
 
| 11:23
 
| 11:23
| continue from paused state It will return true if <tt>n modulo 2</tt> is not equal to 1 and will return false, otherwise.
+
| continue from paused state It will return true if n modulo 2 is not equal to 1 and will return false, otherwise.
  
 
|-
 
|-
 
| 11:36
 
| 11:36
| The function here returns <tt>True</tt> if all the digits of the number <tt>n</tt> are even, otherwise it returns <tt>False</tt>.
+
| The function here returns True if all the digits of the number n are even, otherwise it returns False .
  
 
|-
 
|-
Line 409: Line 394:
 
|-
 
|-
 
|11:51
 
|11:51
|So it is given def even underscore digits within bracket n  colon
+
|So it is given def even underscore digits within bracket n  colon """returns True if all the digits in the number n are even, returns False if all the digits in the number n are not even"""
"""returns True if all the digits in the number n are even, returns False if all the digits in the number n are not even"""
+
  
 
|-
 
|-
Line 418: Line 402:
 
|-
 
|-
 
|12:24
 
|12:24
|The next line if n modulo 2 == 1 colon
+
|The next line if n modulo 2 == 1 colon return False n slash= 10 return True  
return False
+
n slash= 10
+
return True  
+
  
 
|-
 
|-
 
| 12:40
 
| 12:40
| Pause here and try to figure out what the function <tt>what</tt> does.
+
| Pause here and   figure out what the function what does.
  
 
|-
 
|-
Line 433: Line 414:
 
|-
 
|-
 
| 12:52
 
| 12:52
| <nowiki>i = 1 while i star i les than n colon</nowiki>
+
| i = 1 while i star i is  less than n colon  
  
 
|-
 
|-
Line 445: Line 426:
 
|-
 
|-
 
| 13:07
 
| 13:07
| continue from paused state The function returns two values.  
+
| continue the video The function returns two values.  
  
 
|-
 
|-
Line 453: Line 434:
 
|-
 
|-
 
| 13:23
 
| 13:23
| Here, the function returns <tt>True</tt> and the square root of <tt>n</tt> if n is a perfect square, otherwise it returns <tt>False</tt> and the square root of the next perfect square.
+
| Here, the function True returns  and the square root of n if n is a perfect square, otherwise it returns False and the square root of the next perfect square.
  
 
|-
 
|-
Line 461: Line 442:
 
|-
 
|-
 
|13:40
 
|13:40
|def is underscore perfect underscore square within bracket  n colon
+
|def is underscore perfect underscore square within bracket  n colon """returns True and square root of n, if n is a perfect square, otherwise returns False and the square root of the next perfect square""" i = 1 while i star i less than n colon i += 1 return i star i == n comma  i  
"""returns True and square root of n, if n is a perfect square, otherwise returns False and the square root of the next perfect square""" i = 1 while i star i less than n colon
+
i += 1
+
return i star i == n comma  i  
+
  
 
|-
 
|-
Line 472: Line 450:
 
|-
 
|-
 
| 14:17
 
| 14:17
| In this tutorial, we have learnt to, 1. Define functions in Python by using the keyword <tt>def</tt>.
+
| In this tutorial, we have learnt to, Define functions in Python by using the keyword def .
  
 
|-
 
|-
 
| 14:22
 
| 14:22
| 2. Call the function by specifying the function name.
+
| Call the function by specifying the function name.
  
 
|-
 
|-
 
| 14:25
 
| 14:25
| 3. Assign a docstring to a function by putting it as a triple quoted string.
+
| Assign a doc string to a function by putting it as a triple quoted string.
  
 
|-
 
|-
 
| 14:33
 
| 14:33
| 4. Pass parameters to a function.
+
| Pass parameters to a function.
  
 
|-
 
|-
 
| 14:37
 
| 14:37
| 5. Return values from a function.
+
| Then  Return values from a function.
  
 
|-
 
|-
Line 496: Line 474:
 
|-
 
|-
 
| 14:42
 
| 14:42
| 1. What will the function do?
+
| What will the function do?
  
 
|-
 
|-
Line 516: Line 494:
 
|-
 
|-
 
| 14:54
 
| 14:54
| Function doesn't have docstring
+
| Function doesn't have doc string
 
   
 
   
 
|-
 
|-
Line 524: Line 502:
 
|-
 
|-
 
| 14:59
 
| 14:59
| 1. How many arguments can be passed to a python function?
+
| How many arguments can be passed to a python function?
** None
+
None
** One
+
One
** Two
+
Two
** Any
+
Any
  
 
|-
 
|-
 
| 15:07
 
| 15:07
| 1. Write a function which calculates the area of a rectangle.
+
| Write a function which calculates the area of a rectangle.
 
+
 
+
  
 
|-
 
|-
 
| 15:12
 
| 15:12
| And the answers,
+
|Now we look at the the answers,
  
 
|-
 
|-
 
| 15:14
 
| 15:14
| 1.The function will result into an error due to the use of wrong syntax in defining the function.  
+
| The function will result into an error due to the use of wrong syntax in defining the function.  
  
 
|-
 
|-
Line 558: Line 534:
 
|-
 
|-
 
| 15:41
 
| 15:41
| Hence, we define our function as,
+
| Hence, we define our function as, def area within bracket l comma b colon
 
+
|-
+
| 15:42
+
| def area within bracket l comma b colon
+
  
 
|-
 
|-

Latest revision as of 12:44, 27 March 2017

Time Narration
00:01 Hello friends and welcome to the tutorial 'Getting started with functions'.
00:05 At the end of this tutorial, you will be able to,

Define a function. Define functions with arguments. Learn about docstrings. Learn about function return value. Read code.

00:16 Before beginning this tutorial,we would suggest you to complete the tutorial on "Conditionals" and "Loops".
00:22 While writing code, we always want to reduce the number of lines of code, and functions is a way of reusing the code.
00:32 Thus the same lines of code can be used as many times as needed.
00:35 A function is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code.
00:43 Now let us get more familiar with functions,
00:48 Consider a mathematical function f of x = x square.
00:53 Here x is a variable and with different values of x the value of function will change.
00:58 When x is one, f(1) will return the value 1 and f(2) will return us the value 4.
01:05 Let us now see how to define the function f of x in python.
01:10 Start your ipython interpreter by typing,ipython in command line.
01:17 Let us define our function f of x
01:19 So type def f within bracket x colon return x star x
01:29 Star denotes multiplication
01:34 Well that defined the function, so before learning what we did let us see if it returns the expected values, try,
01:45 f(1) f(2)
01:52 Yes, it returned 1 and 4 respectively.
01:55 And now let us see what we did.
01:58 We wrote two lines: The first line def f of x is used to define the name and the parameters to the function, and the second line is used to fix what the function is supposed to return.
02:12 def is a keyword and f is the name of the function and x the parameter of the function.
02:19 Pause the video here, try out the following exercise and resume.
02:24 Write a python function named cube which computes the cube of a given number n.
02:31 Switch to your terminal for solution.
02:33 The problem can be solved as,
02:36 In the terminal type def cube within bracket n colon return n star star 3
02:48 Let us check whether our function returns the cube of a number or not
02:53 So type cube within bracket 2 and hit enter.
03:00 It returned 8,which means we have defined our function,the right way.
03:05 And now let us see how to write functions without arguments.
03:09 let us define a new function called greet which will print Hello World .
03:15 So type def greet() colon and hit enter.
03:26 Then type print within double quotes Hello World exclamation
03:39 now we call the function as,greet() and hit enter.
03:45 Well that is a function which takes no arguments.
03:49 Also note that it is not mandatory for a function to return values.
03:53 The function greet neither takes any argument nor returns any value.
03:57 Now let us see how to write functions with more than one argument.
04:03 Pause the video here, try out the following exercise and resume the video.
04:08 Write a python function named avg which computes the average of a and b .
04:16 Switch to terminal for solution.
04:19 Type def avg within bracket a comma b colon return within bracket a + b divided by 2
04:36 For division we use slash.
04:42 Let us test our function,
04:44 Type on terminal avg within bracket 20 comma 30 and hit enter.
04:53 We get the correct average, 25.
04:56 Thus if we want a function to accept more arguments, we just list them separated with a comma between the parenthesis after the function's name in the def line.
05:06 It is always a good practice to document the code that we write, and for a function we define, we should write an abstract of what the function does, and that is called a docstring.
05:19 Let us modify the function avg and add docstring to it.
05:24 Do the following,So now in the terminal type def avg within bracket a comma b colon
05:38 Then in triple double quote you can type avg takes two numbers as input (a & b), and returns the average of a and b
05:50 Then type return within bracket a+b and hit enter
06:02 Note that we have a syntax error.
06:09 The error that we saw here is because of indentation error in return(a+b)/2
06:15 So just input that command again.
06:25 So type def avg within bracket a comma b colon """ avg takes two numbers as input (a & b), and returns the average of a and b""" return within bracket a+b divided by 2
06:45 Note that docstrings are entered in the immediate line after the function definition and put as a triple quoted string.
06:55 And here as far as the code functionality is concerned, we didn't do anything.
07:00 We just added an abstract of what the function does.
07:03 Now try this in the ipython interpreter.
07:07 Type avg and question mark.
07:12 It displays the docstring as we gave it.
07:16 Thus doc string is a good way of documenting the function we write.
07:21 Now type f question mark and hit enter.
07:29 It does have a doc string associated with it.
07:37 Sorry It does not have a doc string associated with it.
07:40 Also we cannot infer anything from the function name, and thus we are forced to read the code to understand about the function.
07:48 Pause the video here, try out the following exercise and resume the video.
07:54 Add doc string to the function f.
07:59 We need to define the function again to add doc string to the function f and we do it as,
08:06 def f within bracket x colon
08:13 """Accepts a number x as argument and, returns the square of the number x."""
08:24 Then type return x star x
08:32 Let us solve one more exercise
08:34 Pause the video here, try out the exercise and resume the video.
08:41 Write a python function named circle which returns the area and perimeter of a circle given radius r .
08:52 Switch to the terminal for solution.
08:57 The problem requires us to return two values instead of one which we were doing till now.
09:03 We can solve the problem as,
09:05 So now we can type in terminal def circle within bracket r colon """returns area and perimeter of a circle given radius r"""

pi = 3.14 area = pi star r star r perimeter = 2 star pi star r return area comma perimeter and hit enter.

10:04 A python function can return any number of values.
10:07 There is no restriction for it.
10:09 Let us call the function circle as,
10:12 Typing it on terminal a comma p = circle within bracket 6. print a, print p
10:39 Now we have done enough coding, let us do some code reading exercise,
10:46 Pause the video here and try to figure out what the function what does.
10:54 def what within bracket n colon
10:58 if n less than 0 colon n = -n while n greater than 0 colon
11:08 if n modulo 2 == 1 colon
11:12 return False
11:14 n slash = 10
11:19 And the next line is return True
11:23 continue from paused state It will return true if n modulo 2 is not equal to 1 and will return false, otherwise.
11:36 The function here returns True if all the digits of the number n are even, otherwise it returns False .
11:45 Now one more code reading exercise,
11:51 So it is given def even underscore digits within bracket n colon """returns True if all the digits in the number n are even, returns False if all the digits in the number n are not even"""
12:13 Then next line if n less than 0 colon n = -n while n greater than 0 colon
12:24 The next line if n modulo 2 == 1 colon return False n slash= 10 return True
12:40 Pause here and figure out what the function what does.
12:48 def what within bracket n colon
12:52 i = 1 while i star i is less than n colon
12:59 i += 1
13:02 return i star i == n comma i
13:07 continue the video The function returns two values.
13:11 One it returns the result of the while statement whether true of false, and second it prints the value that ii` currently holds.
13:23 Here, the function True returns and the square root of n if n is a perfect square, otherwise it returns False and the square root of the next perfect square.
13:37 So we look at that
13:40 def is underscore perfect underscore square within bracket n colon """returns True and square root of n, if n is a perfect square, otherwise returns False and the square root of the next perfect square""" i = 1 while i star i less than n colon i += 1 return i star i == n comma i
14:14 This brings us to the end of this tutorial.
14:17 In this tutorial, we have learnt to, Define functions in Python by using the keyword def .
14:22 Call the function by specifying the function name.
14:25 Assign a doc string to a function by putting it as a triple quoted string.
14:33 Pass parameters to a function.
14:37 Then Return values from a function.
14:39 Here are some self assessment questions for you to solve
14:42 What will the function do?
14:46 def what(x)
14:48 return x star x
14:50 Returns the square of x
14:52 Returns x
14:54 Function doesn't have doc string
14:57 Error
14:59 How many arguments can be passed to a python function?

None One Two Any

15:07 Write a function which calculates the area of a rectangle.
15:12 Now we look at the the answers,
15:14 The function will result into an error due to the use of wrong syntax in defining the function.
15:27 The function line should always end with a colon
15:32 Any number of arguments can be passed to a python function.
15:37 As we know, area of a rectangle is product of it's length and breadth.
15:41 Hence, we define our function as, def area within bracket l comma b colon
15:47 return l star b
15:51 Hope you have enjoyed this tutorial and found it useful.
15:55 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha