WEBVTT

00:00.140 --> 00:00.710
Hello guys.

00:00.710 --> 00:03.140
So we are going to continue the discussion with respect to Python.

00:03.140 --> 00:06.680
And in this video we are going to discuss about lambda functions in Python.

00:06.710 --> 00:08.390
Now what exactly is lambda function?

00:08.390 --> 00:12.980
Lambda functions are small anonymous function defined using the lambda keyword.

00:12.980 --> 00:18.980
Now whenever we talk about anonymous function it can be basically said a function without a name.

00:18.980 --> 00:19.520
Right.

00:19.520 --> 00:25.730
So they can have any number of arguments but only one expression okay.

00:25.760 --> 00:29.960
And they are commonly used for short operation or as arguments to higher order function.

00:29.960 --> 00:33.170
Now here let me discuss about 2 to 3 keywords.

00:33.170 --> 00:34.580
First of all anonymous function.

00:34.610 --> 00:37.970
Anonymous function is basically a function without a name.

00:38.060 --> 00:44.090
And in order to create lambda function, we specifically require lambda keyword, and they can have

00:44.090 --> 00:47.300
any number of arguments, but only one expression.

00:47.300 --> 00:49.940
One expression basically means one logic, right?

00:49.940 --> 00:55.640
So here is one of the syntax I have given it over here like lambda arguments colon expression.

00:55.670 --> 01:00.520
Now let me just show you with some of the good practical examples so that you will be able to understand

01:00.520 --> 01:02.530
how does lambda function basically work?

01:02.560 --> 01:03.100
Okay.

01:03.160 --> 01:04.420
Now let me do one thing.

01:04.450 --> 01:04.630
Okay.

01:04.660 --> 01:10.090
Let me just go ahead and create a function which is called as definition a comma b okay.

01:10.150 --> 01:14.350
Now what I'm actually going to do over here is that I'll return a plus b.

01:14.440 --> 01:18.730
Now here obviously I've created a function which is taking two numbers.

01:18.730 --> 01:22.210
And here clearly you can just write two comma three.

01:22.210 --> 01:24.040
And here you'll be able to get an answer.

01:24.040 --> 01:26.860
Let's say the addition of two numbers two comma three is five.

01:27.070 --> 01:30.520
Now over here this is just a basic function right.

01:30.550 --> 01:36.070
And inside this you just have one expression one logic that you have written or one operation that you

01:36.100 --> 01:37.060
have written over here.

01:37.090 --> 01:37.450
Right.

01:37.480 --> 01:40.660
And for this you have created entirely one entire function.

01:40.660 --> 01:44.770
You have written the definition keyword a comma b, all these things you have specifically written.

01:44.800 --> 01:50.530
Now, what it says is that if you have just one expression and let's say you have multiple parameters

01:50.530 --> 01:56.590
over here, then instead of just creating a wholesome function over here, you can create a anonymous

01:56.590 --> 01:58.700
function, which is also called as lambda function.

01:58.730 --> 02:00.200
A function without a name.

02:00.290 --> 02:04.340
Now, if I really want to convert this function into a lambda function.

02:04.370 --> 02:05.150
See the syntax.

02:05.180 --> 02:07.790
Okay, so first of all I will go ahead and write lambda.

02:07.790 --> 02:12.260
And then the second parameter that over here it is asking is argument.

02:12.260 --> 02:15.170
So what are the arguments I am giving for this particular function.

02:15.200 --> 02:15.980
It is very simple.

02:15.980 --> 02:17.780
It is a comma b okay.

02:17.990 --> 02:22.940
Now with respect to a comma b I need to go ahead and write my expression.

02:22.940 --> 02:26.330
Expression is what task or what operation I am actually doing.

02:26.360 --> 02:32.930
Okay, so once I execute this so you can see that this becomes an anonymous function or lambda function

02:32.930 --> 02:33.410
also.

02:33.410 --> 02:36.020
So here I will just go ahead and create a variable.

02:36.020 --> 02:40.310
And this addition variable will be my lambda function.

02:40.340 --> 02:40.670
Okay.

02:40.700 --> 02:44.690
If you don't believe me let's go ahead and see the type of addition okay.

02:44.720 --> 02:46.010
And we'll try to print it.

02:46.010 --> 02:48.320
So here you can see it is a function okay.

02:48.350 --> 02:48.980
Lambda function.

02:48.980 --> 02:49.550
It is not saying.

02:49.550 --> 02:54.830
But here you can see that it is a kind of lambda function itself, the function without any specific

02:54.830 --> 02:55.190
name.

02:55.190 --> 02:58.270
Like we did not use any definition, we have just created a variable.

02:58.300 --> 03:06.250
Now, in order to call this okay, all I have to do is that just call this variable, give the parameters

03:06.250 --> 03:06.850
that I want.

03:06.880 --> 03:08.830
Let's say I want to give five comma six.

03:08.830 --> 03:10.720
And let me just go ahead and execute it.

03:10.720 --> 03:15.160
So here you can see automatically with the help of this lambda expression entirely.

03:15.160 --> 03:17.470
The operation is basically happening here.

03:17.470 --> 03:18.520
These are my arguments.

03:18.520 --> 03:20.800
And this is the expression that we have specifically given.

03:20.830 --> 03:21.190
Right.

03:21.220 --> 03:27.340
So I hope you are able to understand and how beautifully just by understanding the syntax we have just

03:27.340 --> 03:29.770
given the values over here and you are able to see the answer.

03:29.800 --> 03:31.450
Okay, let me do one more thing.

03:31.480 --> 03:31.630
Okay.

03:31.660 --> 03:33.310
Let me just go ahead and create one function.

03:33.310 --> 03:36.640
Let's say uh, I'll create a function which is called as even.

03:36.670 --> 03:37.180
Okay.

03:37.180 --> 03:40.030
And here I give a number okay.

03:40.240 --> 03:43.930
Now with respect to this number I'll just go ahead and put a condition.

03:43.930 --> 03:46.570
If num modulus two is double equal to zero.

03:46.570 --> 03:49.450
So in short I'm just checking whether the number is even or odd.

03:49.480 --> 03:52.600
Then I'm just going to return the value as true okay.

03:52.810 --> 03:53.620
Perfect.

03:53.710 --> 03:56.770
Now let me just go ahead and give the value.

03:56.770 --> 03:58.630
So here I'm going to call a function right.

03:58.660 --> 03:59.530
Even 24.

03:59.530 --> 04:01.870
So here I'm actually going to get true okay.

04:01.900 --> 04:06.640
Now similarly if I want to convert this into a lambda function let me just go ahead and create my variable.

04:06.670 --> 04:09.310
Even one I'll be using lambda.

04:09.340 --> 04:10.600
I'll give my parameters.

04:10.600 --> 04:13.630
What is my parameters arguments that I need to give only num.

04:13.630 --> 04:19.210
Then I'll write my colon and all I have to basically do is write my expression and the expression is

04:19.210 --> 04:19.690
over here.

04:19.690 --> 04:23.500
Simple num modulus two is double equal to zero.

04:23.530 --> 04:27.850
Right now this even one becomes my variable.

04:27.850 --> 04:30.520
So now if I go ahead and just go ahead and write 12.

04:30.550 --> 04:34.930
Now it is going to give me true because 12 is basically an even number.

04:34.960 --> 04:35.320
Right.

04:35.320 --> 04:37.390
And here you can write any variable.

04:37.390 --> 04:40.870
It is not necessary that you only need to write number okay.

04:41.440 --> 04:45.550
You can write A, you can write B, you can write C, whatever things you specifically want to write

04:45.580 --> 04:46.060
okay.

04:46.090 --> 04:48.040
Based on that you can actually do that.

04:48.070 --> 04:51.190
Now let's try with some more examples.

04:51.190 --> 04:54.340
And here this time I'm going to take multiple parameters.

04:54.370 --> 04:54.820
Okay.

04:54.850 --> 04:58.930
So let me just go ahead and create one function which is called as addition.

04:58.930 --> 04:59.680
Let's say.

04:59.680 --> 05:03.400
And here I'm actually going to give my x comma y comma z.

05:03.400 --> 05:08.650
So these are my parameters I'm just going to return x plus y plus z okay.

05:08.680 --> 05:13.330
Now when I'm actually doing this if I am calling my addition function.

05:13.450 --> 05:16.180
So here you will be able to see that I'll give my values.

05:16.180 --> 05:19.030
So let's say 1213 comma 14.

05:19.150 --> 05:21.370
If I execute it the sum is basically 39.

05:21.370 --> 05:22.180
Now let me see.

05:22.210 --> 05:26.290
Let me show you how you can actually convert this into an lambda function.

05:26.290 --> 05:28.210
I'll create a addition one variable.

05:28.210 --> 05:31.060
And now I will be using a lambda function.

05:31.060 --> 05:36.190
And with respect to this specific lambda function what I am actually going to do I'm going to give my

05:36.190 --> 05:38.080
arguments which all is required.

05:38.080 --> 05:39.070
Let's say x, y, z.

05:39.100 --> 05:43.150
Then I will be giving the colon and I only have to write the expression.

05:43.150 --> 05:46.180
And I have already shown you how to write it down x plus y plus z.

05:46.210 --> 05:46.660
Right.

05:46.690 --> 05:48.070
So this is it.

05:48.070 --> 05:50.850
And if I go ahead and call my addition additions one.

05:50.850 --> 05:57.060
So here you'll be able to see that if I give my values 12, 13 and 14, I should be able to get 39,

05:57.090 --> 05:57.600
right?

05:57.600 --> 06:02.280
So through this way, I am not wasting much time to probably create a function.

06:02.280 --> 06:07.080
Instead, just by using a lambda function or anonymous function, I am able to do all this kind of expression,

06:07.200 --> 06:08.280
uh, operations.

06:08.280 --> 06:08.670
Right?

06:08.700 --> 06:13.530
Again, to repeat guys, a lambda function where only a single expression is there, we can actually

06:13.530 --> 06:16.350
convert that entire function into a lambda function itself.

06:16.680 --> 06:23.010
Uh, lambda function has more amazing features, but for that we really need to understand about map.

06:23.010 --> 06:26.400
And there is there are two function which is called as map and filter.

06:26.400 --> 06:30.930
So in the upcoming videos we will be discussing about this map function and filter function.

06:30.930 --> 06:37.440
And I will just give you one example right now just to make you understand, uh, what exactly map and

06:37.440 --> 06:38.550
filter basically does.

06:38.580 --> 06:39.150
Okay.

06:39.180 --> 06:44.730
So yes, in the next video we'll be discussing more, but I just want to give you the expression of

06:44.730 --> 06:46.260
power of lambda function with map.

06:46.290 --> 06:46.650
Okay.

06:46.650 --> 06:50.930
So let's say I want to probably go ahead and create a list, right?

06:50.930 --> 06:54.590
And with respect to the list, I have values like one, two, three, four, five, six.

06:54.620 --> 06:55.160
Okay.

06:55.190 --> 07:00.020
Now let's say that I want to find the square of each and every number.

07:00.050 --> 07:00.590
Right?

07:00.590 --> 07:04.910
If I want to find the square of each and every number, or let's say I want to just go and see that,

07:04.940 --> 07:10.520
hey, uh, uh, you know, uh, let's say I'll just go ahead and create a variable, let's say squared

07:10.520 --> 07:11.420
underscore numbers.

07:11.450 --> 07:12.020
Okay.

07:12.200 --> 07:15.020
Now in order to find out square for every number.

07:15.020 --> 07:15.560
Right.

07:15.590 --> 07:17.690
What is the kind of function we will define.

07:17.690 --> 07:21.230
Let's say I'm going to define definition square okay.

07:22.490 --> 07:23.360
Square.

07:24.890 --> 07:28.430
And here I am going to give my numbers.

07:28.460 --> 07:30.380
Or let me just give single number.

07:30.410 --> 07:31.670
Then what this is going to do.

07:31.700 --> 07:36.350
Return number double star square.

07:36.380 --> 07:36.920
Right.

07:37.130 --> 07:39.740
So here we are just giving a square.

07:40.340 --> 07:43.400
And if I give two number I'm going to get the square of that particular number.

07:43.400 --> 07:44.150
This is fine.

07:44.150 --> 07:45.710
And this is only for one number.

07:45.740 --> 07:50.230
Right now if I am giving for a list of number, then what I have to do, I'll give this number as a

07:50.230 --> 07:50.620
parameter.

07:50.650 --> 07:54.460
Over here I'll write a for loop, iterate through each and every number and do this particular operation

07:54.460 --> 07:55.780
and return it right.

07:55.930 --> 07:59.770
But is there a better way instead of iterating it?

07:59.770 --> 08:03.790
Truly, you know, probably writing more couple of lines of code for this, right?

08:03.820 --> 08:04.810
Is there a better way?

08:04.810 --> 08:08.020
So we can specifically use this map function map function.

08:08.020 --> 08:10.210
What it does is that it applies.

08:10.330 --> 08:17.950
It applies a function to all items in a list.

08:17.980 --> 08:19.450
Okay, right.

08:19.480 --> 08:23.020
Or any collection also okay, so what does this basically mean.

08:23.020 --> 08:25.780
And I will show you along with the map function okay.

08:25.780 --> 08:28.360
So now sorry along with the lambda function.

08:28.390 --> 08:30.520
Now let me just go ahead and create this lambda function.

08:30.520 --> 08:34.150
So first of all I will go ahead and write lambda I know what is my operation.

08:34.150 --> 08:35.770
My operation is basically squaring right.

08:35.800 --> 08:37.660
So I will create a argument.

08:37.660 --> 08:42.130
So argument I will be requiring a number x and the expression will be x square okay.

08:42.160 --> 08:48.050
So this basically becomes lambda right Now map basically requires.

08:48.050 --> 08:50.870
The first parameter in map is basically a function.

08:50.870 --> 08:53.180
The second parameter is iterables.

08:53.180 --> 08:55.400
So this actually is my lambda function.

08:55.400 --> 08:57.290
Or you can also call this particular function.

08:57.290 --> 08:59.900
But right now I'm giving my lambda function over here.

08:59.900 --> 09:04.100
And the second parameter that I'm actually going to give is nothing but my numbers.

09:04.100 --> 09:04.640
Right.

09:04.640 --> 09:07.880
So here is basically my map right now.

09:07.880 --> 09:13.310
At the end of the day, if I execute this, this basically creates a map object at this particular memory

09:13.310 --> 09:14.000
location.

09:14.000 --> 09:20.270
And map usually uses a lazy loading technique that basically means still it has not completely initialized.

09:20.270 --> 09:23.690
So to make it initialized I will convert this into a list.

09:23.720 --> 09:29.060
Now see the magic okay, for every number in this particular list, automatically you are able to get

09:29.060 --> 09:30.110
the square right.

09:30.140 --> 09:31.250
Isn't it beautiful?

09:31.250 --> 09:34.880
So I will just try to show you like how we can use Map Lambda.

09:34.880 --> 09:37.850
And there is also one more function which is called as filter.

09:37.880 --> 09:38.150
Right.

09:38.150 --> 09:41.000
And I'll explain all these things in my upcoming videos.

09:41.000 --> 09:42.440
So yes this was it from my side.

09:42.470 --> 09:43.940
I will see you all in the next video.

09:43.940 --> 09:44.570
Thank you.
