0:00
Hello everyone, welcome to the chapter function
0:02
So I'll be explaining the function definitions in Python. So basically whenever you wanted to write a group of statement which is
0:10
definitely required in your code, then it is not recommended to write this statement
0:15
every time separately. So basically what we are doing we are just following dry principle
0:19
Dry is nothing but do not repeat yourself, right? So whenever you are using programming language, so if you sit off
0:29
lines are repeated multiple times in your code it is a standard practice to create a
0:35
function and make it use of that function by calling it separately right the main
0:41
advantage is code reuseability another language is functions are also known as
0:46
method procedures subroutines etc there are two types of functions basically Python were in wind functions so these are pretty defined that we have already
0:55
seen in different chapters wherein we are using ID type input file even we are using print and all of those other functions these are inbuilt functions in Python
1:07
we are also having user defined functions that are nothing but developed by programmer according to the
1:13
business requirements the syntax is df keyword followed by the name of function and then there are
1:20
parameters right and then column right so here you can write a dox string your code and then either you can
1:29
the value or if not you can print the value so basically this is a function
1:35
definition so doveting that we have already seen multiple lines of things which
1:39
will tell what this function is all about and these are the code of line and then
1:42
return value so basically whenever I was talking about this parameter there are
1:47
different types of function argument that we can pass what is positional
1:51
named default and variable in what is conditional argument for example I have
1:56
defined a sum function with two values as E's parameter that is A comma B and there is one more variable with total who is just adding
2:06
all E plus B and I am returning the value total. So now whenever I am I wanted to call this function I need to create identifier or an object
2:16
of function I am just giving the value as n and I am calling this function right but now
2:22
it has two arguments A and B so I need to give certain inputs for example I have given
2:28
to values 10 comma 20 what will happen this will 10 plus 20 will be given to total right and that
2:37
total will be 30 and that 30 value we are returning that means that 30 value will be returned back to
2:45
n and whenever you are printing n you will get 30 as your output but instead after that
2:53
this is your final output whenever you are printing that value but you are also pretty
2:58
printing two outputs that is value which you are given as inputs right so since
3:03
whatever you are passing it is taking as a and whatever you are passing second
3:08
it is taking as b this is called as positional arguments similar way if at you are telling B is 5 and A is 6 that means whenever you are calling the functions here
3:22
so it will be given A, 6 and B 5, and then instead of A plus B, which was earlier 5 plus 6, it will be 6 plus 5, that is 11 which will be printed
3:34
Third one will be default arguments. So now whenever you are passing these two values
3:39
but while defining the function you are seeing b is 0 so in respect to you of you provide b value as 8
3:45
or even if at all you do not want it to provide you can just simply see sum of 6 so this way you are
3:52
just providing one value that is a but second value if you are not providing it will take it as b
3:58
and that is 0 so that is an default argument and the last one is variable let the argument
4:05
whenever you want to apply a different inputs you can always use star which we
4:11
like there are variable inputs that are past as parameters to this function so
4:16
now whenever I am seeing some of a 10 20 30 and so on so all these inputs I can take
4:23
it to the functions I can apply for loop and add it to the total and printout of
4:28
course so this is the way where you can call different functions
4:35
arguments so there are different powerful functions in Python that we can use these are really
4:41
really important functions that you can discuss in your interviews so one of them is
4:47
lambda function map function filter functions Zift function and functions are reduced function so we'll see one by one what are lambda function so basically
4:56
when you are defining function you are defying like this params certain things and
5:03
then you are returning right What are you wanted to return? So this is again taking more than two to three lives of court
5:11
But now you said all you wanted to do a single line, the function definition, you can use
5:16
a lambda function. Now what is the definition of lambda function? It will be lambda and then argument and expression
5:24
So basically it will be argument color extension. Whenever you're calling a lambda function, you should do certain parameters to it
5:32
Whatever the parameters you are passing, it will be. coming into the arguments that we pass to the expression right so for example I have created
5:40
this lambda x pollen triple x x star x that means this is a cube right so whenever I am passing
5:49
14 here so it will see this is a lambda cube variable definition of this lambda function that is an
5:56
object I am passing certain value then is 14 this protein will be given to x and then 14
6:03
into 14 into 14 that is 274 will be rented as output similarly we have a we can find some with the
6:12
nabra that is a comma b and a plus b so for example we are providing two
6:18
inputs and write from a sixty four both of these will be given to this
6:22
lambda function and output will be taken back to s and 164 will be printed Similarly we have Mac function which is nothing but to
6:33
So for example for every element present in the sequence we have to apply certain functionality and generate the new element
6:40
So for example we have this list and now we wanted to double each element of the list
6:45
what we can do in general way we can apply for loop or i in l and then I can see for a
6:53
of i have l is equal to 2 times l of i right so and then i should append it so this is another
7:00
formal week but if i now i want to use map function i can do is this is the lambda expression
7:06
which is standing where what are the input you are passing i am doubling it so to do that what
7:11
is the map function is doing this expression it is applying to list l list l as having
7:17
this many values and once these operations is applied to this list l now again with
7:23
Type casting, we are converting output to list and saving it to L1
7:27
So whatever 1, 2, 3, 4, 5 is passed. So whenever 1 is passed, it will be 1 is 2 into 1
7:34
Whenever 2 is passed 2. 2.3 is past, 3 into 3 and so on
7:42
So all the inputs given will be double. That is how MAC function is used
7:46
Third one will be filter function. So whatever the list is provided, now you wanted to only output
7:53
the even values out of it. So we know the x mod 2 equal to 0. This is an expression
8:00
which will picture out our even values. So for that we will use lambda and colon. So
8:09
this is basically lambda x colon and expression comma whatever the list that we wanted this
8:16
filter to be applied on. And then we are using a instead of map function, we are using
8:23
filter function here right so this filter function will just filter out the
8:28
u1 elements from the list and then print out so from all 10 20 and 30 whenever they are divided
8:35
by 2 reminder is 0 so all these output are filtered out with filter function similarly now we have
8:42
a reduced function now so to use this reduce function we have to import
8:48
fun tool we are importing everything that means all method classes and functions and
8:53
copied or called inside your code. So now this is a normal list and now we are reducing it with a reduced function
9:03
So what is the expression? Expression says x comma y, follow x plus y
9:08
These two values are added. So when you're I'm passing list means first two values
9:13
The first 10 comma 20 will be added, it will be 30. Second one will be 30 come of 30 will be added, it will be 60
9:20
then 60 comma 40 will be added here it will be 100 and then 100 comma 50 will be added it
9:29
will be 150 so all of these values are reduced to single output so that is how a reduced
9:34
function is used then next one is enough function what this enough function will do for example it will help you to track your iterable elements So we have apple grass and oranges
9:47
Now for this is a normal method if I wanted to go and check each element I can do for I in this fruit
9:54
See for I in this fruit print I come a fruit. So it will just tell I and fruit eye. I means it's index zero and fruiti means apple
10:03
So zero apple one gloves two orange. This is your output of this. is your output of your first code. Now whenever I am using enumerate function, I can add
10:12
the moment called index and values together without doing this operation separately. How I can do
10:19
For i.e comma value in enumerate of fruit. So I can generate whenever I am seeing enumerate
10:28
of fruit, it will create this kind of operation. So whenever I am printing ID it will print 0, whenever I am printing name, it will print the value
10:43
So 0 Apple, Craps Orange. This way our enumerate formations is used to track the iterable elements in your indexes
10:52
Now what is Zip function? Zip function will just zip two different lists into comma separated tuples
11:00
For example, this is apple grass oranges and 187. Now I am doing zip fruit come up price for eye in the results and print I am doing it will be
11:10
apply and write together, grep's 80 together and orange 70 together. Least will be zipped together by using zip functions
11:23
So we have the functions, these are building functions that we are using, these are user definition functions
11:28
Then also we have, this is just an example by creating a function we can use two keyword to define
11:34
is a mandatory return in optional so this is how it is called when you're
11:38
MD in df message and I am just calling it with that function name directly since
11:45
it has print message that message will be intact now with parameters if I am
11:51
passing name instead of whenever I am passing or calling the functions I just
11:55
have to pass this name it will be going over here and that will be coming over here
11:59
inside a print and then I can print doubt food similar way there are functions with return statement all the outputs I have returned so
12:10
for that I am passing these two values doing certain operations returning these values
12:15
back to output and printing the output so output will be printed and these all
12:20
arguments and its definition that we have seen after that we have also seen variable
12:26
length arguments then these are the different lambda functions flinter functions map function reviews function
12:34
functions that we have seen in last one is inumerate functions so these are the
12:38
all most important functions which are there in Python which are used with to reduce your
12:44
port repeatability so drive principles will be followed and that's why function is one of the
12:49
most important topics in Python basic we will see in the next chapter with the modules and packages