Python/C4/Getting-started-with-functions/English-timed
From Script | Spoken-Tutorial
| 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! |