Python/C4/Getting-started-with-functions/English-timed
From Script | Spoken-Tutorial
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 able to,
|
0:16 | Before beginning this tutorial,we would suggest you to complete the tutorial on "Conditionals" and "Loops". |
0:22 | 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 | Thus the same lines of code can be used as many times as needed. |
0: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. |
0:43 | Now let us get more familiar with functions, |
0:48 | Consider a mathematical function f of x = x square. |
0:53 | Here x is a variable and with different values of x the value of function will change. |
0:58 | When x is one, f(1) will return the value 1 and f(2) will return us the value 4. |
1:05 | Let us now see how to define the function f of x in python. |
1:10 | Start your ipython interpreter by typing,ipython in command line. |
1:17 | Let us define our function f of x |
1:19 | So type def f within bracket x colon
return x star x |
1:29 | Star denotes multipication |
1:34 | Well that defined the function, so before learning what we did let us see if it returns the expected values, try, |
1:45 | f(1)
f(2) |
1:52 | Yes, it returned 1 and 4 respectively. |
1:55 | And now let us see what we did. |
1: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. |
2:12 | def is a keyword and f is the name of the function and x the parameter of the function. |
2:19 | Pause the video here, try out the following exercise and resume the video. |
2:24 | Write a python function named cube which computes the cube of a given number n. |
2:31 | Switch to your terminal for solution. |
2:33 | The problem can be solved as, |
2:36 | In the terminal type def cube within bracket n colon
return n star star 3 |
2:48 | Let us check whether our function returns the cube of a number or not |
2:53 | So type cube within bracket 2 and hit enter. |
3:00 | It returned 8,which means we have defined our function,the right way. |
3:05 | And now let us see how to write functions without arguments. |
3:09 | let us define a new function called greet which will print Hello World. |
3:15 | So type def greet() colon and hit enter. |
3:26 | Then type print in double quotes Hello World exclamation |
3:39 | now we call the function as,greet() and hit enter. |
3:45 | Well that is a function which takes no arguments. |
3:49 | Also note that it is not mandatory for a function to return values. |
3:53 | The function greet neither takes any argument nor returns any value. |
3:57 | Now let us see how to write functions with more than one argument. |
4:03 | Pause the video here, try out the following exercise and resume the video. |
4:08 | Write a python function named avg which computes the average of a and b. |
4:16 | Switch to your terminal for solution. |
4:19 | Type def avg within bracket a comma b colon
return within bracket a + b divided by 2 |
4:36 | For division we use slash. |
4:42 | Let us test our function, |
4:44 | Type on terminal avg within bracket 20 comma 30 and hit enter. |
4:53 | We get the correct average, 25. |
4: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. |
5: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. |
5:19 | Let us modify the function avg and add docstring to it. |
5:24 | Do the following, |
5:25 | 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 |
5:50 | Then type return return within bracket a+b divided by 2 |
6:02 | Note that we have a syntax error. |
6:09 | The error that we saw here is because of indentation error in return(a+b)/2 |
6:15 | So just input that command again. |
6: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 |
6:45 | Note that docstrings are entered in the immediate line after the function definition and put as a triple quoted string. |
6:55 | And here as far as the code functionality is concerned, we didn't do anything. |
7:00 | We just added an abstract of what the function does. |
7:03 | Now try this in the ipython interpreter. |
7:07 | Type avg and question mark. |
7:12 | It displays the docstring as we gave it. |
7:16 | Thus docstring is a good way of documenting the function we write. |
7:21 | Now type comma f question mark and hit enter. |
7:29 | It does have a docstring associated with it. |
7:37 | Sorry It doesnot have a docstring associated with it. |
7:40 | 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 | Pause the video here, try out the following exercise and resume the video. |
7:54 | Add docstring to the function f. |
7:59 | We need to define the function again to add docstring to the function f and we do it as, |
8:06 | def f within bracket x colon |
8:13 | """Accepts a number x as argument and, returns the square of the number x.""" |
8:24 | return x star x |
8:32 | Let us solve one more exercise |
8:34 | Pause the video here, try out the following exercise and resume the video. |
8:41 | Write a python function named circle which returns the area and perimeter of a circle given radius r. |
8:52 | Switch to the terminal for solution. |
8:57 | The problem requires us to return two values instead of one which we were doing till now. |
9:03 | We can solve the problem as, |
9: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 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 try to figure out what the function what does. |
12:48 | def what within bracket n colon |
12:52 | i = 1 while i star i les than n colon |
12:59 | i += 1 |
13:02 | return i star i == n comma i |
13:07 | continue from paused state 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 returns True 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, 1. Define functions in Python by using the keyword def. |
14:22 | 2. Call the function by specifying the function name. |
14:25 | 3. Assign a docstring to a function by putting it as a triple quoted string. |
14:33 | 4. Pass parameters to a function. |
14:37 | 5. Return values from a function. |
14:39 | Here are some self assessment questions for you to solve |
14:42 | 1. 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 docstring |
14:57 | Error |
14:59 | 1. How many arguments can be passed to a python function?
|
15:07 | 1. Write a function which calculates the area of a rectangle.
|
15:12 | And the answers, |
15:14 | 1.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, |
15:42 | 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! |