1
00:00:00,000 --> 00:00:00,000
Hello guys.

2
00:00:00,000 --> 00:00:03,000
So we are going to continue the discussion with respect to Python.

3
00:00:03,000 --> 00:00:06,000
And in this video we are going to discuss about lambda functions in Python.

4
00:00:06,000 --> 00:00:08,000
Now what exactly is lambda function?

5
00:00:08,000 --> 00:00:12,000
Lambda functions are small anonymous function defined using the lambda keyword.

6
00:00:12,000 --> 00:00:18,000
Now whenever we talk about anonymous function it can be basically said a function without a name.

7
00:00:18,000 --> 00:00:19,000
Right.

8
00:00:19,000 --> 00:00:24,000
So they can have any number of arguments, uh, but only one expression.

9
00:00:25,000 --> 00:00:25,000
Okay.

10
00:00:25,000 --> 00:00:29,000
And they are commonly used for short operation or as arguments to higher order function.

11
00:00:29,000 --> 00:00:33,000
Now here let me discuss about 2 to 3 keywords.

12
00:00:33,000 --> 00:00:34,000
First of all, anonymous function.

13
00:00:34,000 --> 00:00:37,000
Anonymous function is basically a function without a name.

14
00:00:38,000 --> 00:00:44,000
And in order to create lambda function, we specifically require lambda keyword, and they can have

15
00:00:44,000 --> 00:00:47,000
any number of arguments, but only one expression.

16
00:00:47,000 --> 00:00:49,000
One expression basically means one logic, right?

17
00:00:49,000 --> 00:00:55,000
So here is one of the syntax I've given it over here like lambda arguments colon expression.

18
00:00:55,000 --> 00:01:00,000
Now let me just show you with some of the good practical examples so that you will be able to understand

19
00:01:00,000 --> 00:01:02,000
how does lambda function basically work.

20
00:01:02,000 --> 00:01:03,000
Okay.

21
00:01:03,000 --> 00:01:04,000
Now let me do one thing okay.

22
00:01:04,000 --> 00:01:09,000
Let me just go ahead and create a function which is called as definition a comma b.

23
00:01:09,000 --> 00:01:10,000
Okay.

24
00:01:10,000 --> 00:01:14,000
Now what I'm actually going to do over here is that I'll return a plus b.

25
00:01:14,000 --> 00:01:18,000
Now here obviously I've created a function which is taking two numbers.

26
00:01:18,000 --> 00:01:22,000
And uh, here clearly you can just write two comma three.

27
00:01:22,000 --> 00:01:24,000
And here you'll be able to get an answer.

28
00:01:24,000 --> 00:01:26,000
Let's say the addition of two numbers two comma three is fine.

29
00:01:27,000 --> 00:01:30,000
Now over here this is just a basic function.

30
00:01:30,000 --> 00:01:30,000
Right.

31
00:01:30,000 --> 00:01:35,000
And inside this you just have one expression, one logic that you have written or one operation that

32
00:01:35,000 --> 00:01:37,000
you have written over here.

33
00:01:37,000 --> 00:01:37,000
Right.

34
00:01:37,000 --> 00:01:40,000
And for this you have created entirely one entire function.

35
00:01:40,000 --> 00:01:44,000
You have written the definition keyword a comma b, all these things you have specifically written.

36
00:01:44,000 --> 00:01:50,000
Now, what it says is that if you have just one expression and let's say you have multiple parameters

37
00:01:50,000 --> 00:01:56,000
over here, then instead of just creating a wholesome function over here, you can create a anonymous

38
00:01:56,000 --> 00:02:00,000
function, which is also called as lambda function, a function without a name.

39
00:02:00,000 --> 00:02:05,000
Now, if I really want to convert this function into a lambda function, see the syntax okay.

40
00:02:05,000 --> 00:02:07,000
So first of all I will go ahead and write lambda.

41
00:02:07,000 --> 00:02:12,000
And then the second parameter that over here it is asking is argument.

42
00:02:12,000 --> 00:02:15,000
So what are arguments I am giving for this particular function.

43
00:02:15,000 --> 00:02:15,000
It is very simple.

44
00:02:15,000 --> 00:02:17,000
It is a comma b okay.

45
00:02:18,000 --> 00:02:22,000
Now with respect to a comma b I need to go ahead and write my expression.

46
00:02:22,000 --> 00:02:26,000
Expression is what task or what operation I'm actually doing.

47
00:02:26,000 --> 00:02:26,000
Okay.

48
00:02:26,000 --> 00:02:33,000
So once I execute this so you can see that this becomes an anonymous function or lambda function also.

49
00:02:33,000 --> 00:02:36,000
So here I will just go ahead and create a variable.

50
00:02:36,000 --> 00:02:40,000
And this addition variable will be my lambda function.

51
00:02:40,000 --> 00:02:40,000
Okay.

52
00:02:40,000 --> 00:02:44,000
If you don't believe me let's go ahead and see this type of addition okay.

53
00:02:44,000 --> 00:02:45,000
And we'll try to print it.

54
00:02:45,000 --> 00:02:48,000
So here you can see it is a function okay.

55
00:02:48,000 --> 00:02:48,000
Lambda function.

56
00:02:48,000 --> 00:02:49,000
It is not saying.

57
00:02:49,000 --> 00:02:55,000
But here you can see that it is a kind of lambda function itself a function without any specific name.

58
00:02:55,000 --> 00:02:58,000
Like we did not use any definition, we have just created a variable.

59
00:02:58,000 --> 00:03:01,000
Now in order to call this okay.

60
00:03:02,000 --> 00:03:06,000
All I have to do is that just call this variable, give the parameters that I want.

61
00:03:06,000 --> 00:03:08,000
Let's say I want to give five comma six.

62
00:03:08,000 --> 00:03:10,000
And let me just go ahead and execute it.

63
00:03:10,000 --> 00:03:15,000
So here you can see automatically with the help of this lambda expression entirely.

64
00:03:15,000 --> 00:03:17,000
The operation is basically happening here.

65
00:03:17,000 --> 00:03:20,000
These are my arguments and this is the expression that we have specifically given.

66
00:03:20,000 --> 00:03:21,000
Right.

67
00:03:21,000 --> 00:03:27,000
So I hope you are able to understand and how beautifully, just by understanding the syntax we have

68
00:03:27,000 --> 00:03:29,000
just given the values over here and you are able to see the answer.

69
00:03:29,000 --> 00:03:31,000
Okay, let me do one more thing.

70
00:03:31,000 --> 00:03:31,000
Okay.

71
00:03:31,000 --> 00:03:33,000
Let me just go ahead and create one function.

72
00:03:33,000 --> 00:03:36,000
Let's say uh, I'll create a function which is called as even.

73
00:03:36,000 --> 00:03:37,000
Okay.

74
00:03:37,000 --> 00:03:39,000
And here I give a number okay.

75
00:03:40,000 --> 00:03:43,000
Now with respect to this number I'll just go ahead and put a condition.

76
00:03:43,000 --> 00:03:46,000
If num modulus two is double equal to zero.

77
00:03:46,000 --> 00:03:49,000
So in short I'm just checking whether the number is even or odd.

78
00:03:49,000 --> 00:03:52,000
Then I'm just going to return the value as true okay.

79
00:03:52,000 --> 00:03:53,000
Perfect.

80
00:03:53,000 --> 00:03:56,000
Now let me just go ahead and give the value.

81
00:03:56,000 --> 00:03:58,000
So here I'm going to call a function right.

82
00:03:58,000 --> 00:03:59,000
Even 24.

83
00:03:59,000 --> 00:04:01,000
So here I'm actually going to get true okay.

84
00:04:01,000 --> 00:04:06,000
Now similar if I want to convert this into a lambda function let me just go ahead and create my variable

85
00:04:06,000 --> 00:04:09,000
even one I'll be using lambda.

86
00:04:09,000 --> 00:04:13,000
I'll give my parameters what is my parameters arguments that I need to give only num.

87
00:04:13,000 --> 00:04:19,000
Then I'll write my colon and all I have to basically do is write my expression and the expression is

88
00:04:19,000 --> 00:04:19,000
over here.

89
00:04:19,000 --> 00:04:23,000
Simple num modulus two is double equal to zero.

90
00:04:23,000 --> 00:04:27,000
Right now this even one becomes my variable.

91
00:04:27,000 --> 00:04:33,000
So now if I go ahead and just go ahead and write 12, now it is going to give me true because 12 is

92
00:04:33,000 --> 00:04:35,000
basically an even number, right.

93
00:04:35,000 --> 00:04:37,000
And here you can write any variable.

94
00:04:37,000 --> 00:04:40,000
It is not necessary that you only need to write number okay.

95
00:04:41,000 --> 00:04:45,000
You can write A, you can write B, you can write C, whatever things you specifically want to write

96
00:04:45,000 --> 00:04:46,000
okay.

97
00:04:46,000 --> 00:04:48,000
Based on that you can actually do that.

98
00:04:48,000 --> 00:04:51,000
Now uh let's try with some more examples.

99
00:04:51,000 --> 00:04:54,000
And here this time I'm going to take multiple parameters okay.

100
00:04:54,000 --> 00:04:59,000
So let me just go ahead and create one function which is called as addition let's say.

101
00:04:59,000 --> 00:05:03,000
And here I'm actually going to give my x comma y comma z.

102
00:05:03,000 --> 00:05:04,000
So these are my parameters.

103
00:05:04,000 --> 00:05:08,000
I'm just going to return x plus y plus z okay.

104
00:05:08,000 --> 00:05:13,000
Now when I'm actually doing this if I am calling my addition function.

105
00:05:13,000 --> 00:05:16,000
So here you'll be able to see that I'll give my values.

106
00:05:16,000 --> 00:05:19,000
So let's say 12 comma 13 comma 14.

107
00:05:19,000 --> 00:05:21,000
If I execute it the sum is basically 39.

108
00:05:21,000 --> 00:05:22,000
Now let me see.

109
00:05:22,000 --> 00:05:26,000
Let me show you how you can actually convert this, uh, into an lambda function.

110
00:05:26,000 --> 00:05:28,000
I'll create an addition one variable.

111
00:05:28,000 --> 00:05:31,000
And now I will be using a lambda function.

112
00:05:31,000 --> 00:05:35,000
And with respect to this specific lambda function, what I am actually going to do, I'm going to give

113
00:05:35,000 --> 00:05:39,000
my arguments which all is required, let's say x y, z.

114
00:05:39,000 --> 00:05:40,000
Then I'll be giving the colon.

115
00:05:40,000 --> 00:05:43,000
And I only have to write the expression.

116
00:05:43,000 --> 00:05:46,000
And I have already shown you how to write it down x plus y plus z.

117
00:05:46,000 --> 00:05:46,000
Right.

118
00:05:46,000 --> 00:05:48,000
So this is it.

119
00:05:48,000 --> 00:05:50,000
And if I go ahead and call my addition one.

120
00:05:50,000 --> 00:05:57,000
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 right.

121
00:05:57,000 --> 00:06:02,000
So through this way, I am not wasting much time to probably create a function.

122
00:06:02,000 --> 00:06:07,000
Instead, just by using a lambda function or anonymous function, I am able to do all this kind of expression,

123
00:06:07,000 --> 00:06:08,000
uh, operations.

124
00:06:08,000 --> 00:06:13,000
Right again, to repeat, guys, uh, lambda function where only a single expression is there, we can

125
00:06:13,000 --> 00:06:16,000
actually convert that entire function into a lambda function itself.

126
00:06:16,000 --> 00:06:22,000
Uh, lambda function has more amazing features, but for that we really need to understand about map.

127
00:06:22,000 --> 00:06:26,000
And uh, there are two function which is called as map and filter.

128
00:06:26,000 --> 00:06:31,000
So in the upcoming videos we will be discussing about this map function and filter function.

129
00:06:31,000 --> 00:06:37,000
And I will just give you one example right now just to make you understand what exactly map and filter

130
00:06:37,000 --> 00:06:38,000
basically does.

131
00:06:38,000 --> 00:06:39,000
Okay.

132
00:06:39,000 --> 00:06:44,000
So yes, in the next video we'll be discussing more, but I just want to give you the expression of

133
00:06:44,000 --> 00:06:46,000
power of lambda function with map.

134
00:06:46,000 --> 00:06:46,000
Okay.

135
00:06:46,000 --> 00:06:50,000
So let's say I want to probably go ahead and create a list.

136
00:06:50,000 --> 00:06:50,000
Right.

137
00:06:50,000 --> 00:06:55,000
And with respect to the list I have values like 123456 Okay.

138
00:06:55,000 --> 00:07:00,000
Now let's say that I want to find the square of each and every number.

139
00:07:00,000 --> 00:07:00,000
Right.

140
00:07:00,000 --> 00:07:04,000
If I want to find the square of each and every number, or let's say I want to just go and see that,

141
00:07:04,000 --> 00:07:10,000
hey, uh, uh, you know, uh, let's say I'll just go ahead and create a variable, let's say squared

142
00:07:10,000 --> 00:07:11,000
underscore numbers.

143
00:07:11,000 --> 00:07:11,000
Okay.

144
00:07:12,000 --> 00:07:15,000
Now in order to find out square for every number.

145
00:07:15,000 --> 00:07:15,000
Right.

146
00:07:15,000 --> 00:07:17,000
What is the kind of function we will define.

147
00:07:17,000 --> 00:07:21,000
Let's say I'm going to define definition square okay.

148
00:07:22,000 --> 00:07:23,000
Square.

149
00:07:24,000 --> 00:07:28,000
And here I am going to give my numbers.

150
00:07:28,000 --> 00:07:30,000
Or let me just give single number.

151
00:07:30,000 --> 00:07:31,000
Then what this is going to do.

152
00:07:31,000 --> 00:07:36,000
Return number double star square.

153
00:07:36,000 --> 00:07:36,000
Right.

154
00:07:37,000 --> 00:07:39,000
So here we are just giving a square.

155
00:07:40,000 --> 00:07:43,000
And if I give two number I'm going to get the square of that particular number.

156
00:07:43,000 --> 00:07:44,000
This is fine.

157
00:07:44,000 --> 00:07:45,000
And this is only for one number.

158
00:07:45,000 --> 00:07:50,000
Right now, if I'm giving for a list of number then what I have to do, I'll give this number as a parameter.

159
00:07:50,000 --> 00:07:51,000
Over here I'll write a for loop.

160
00:07:51,000 --> 00:07:55,000
I iterate through each and every number and do this particular operation and return it right.

161
00:07:55,000 --> 00:07:59,000
But is there a better way instead of iterating it?

162
00:07:59,000 --> 00:08:03,000
Truly, you know, probably writing more couple of lines of code for this, right?

163
00:08:03,000 --> 00:08:04,000
Is there a better way?

164
00:08:04,000 --> 00:08:07,000
So we can specifically use this map function.

165
00:08:07,000 --> 00:08:08,000
map function.

166
00:08:08,000 --> 00:08:10,000
What it does is that it applies.

167
00:08:10,000 --> 00:08:17,000
It applies a function to all items in a list.

168
00:08:18,000 --> 00:08:19,000
Okay right.

169
00:08:19,000 --> 00:08:23,000
Or any collection also okay, so what does this basically mean.

170
00:08:23,000 --> 00:08:25,000
And I will show you along with the map function okay.

171
00:08:25,000 --> 00:08:28,000
So now sorry along with the lambda function.

172
00:08:28,000 --> 00:08:30,000
Now let me just go ahead and create this lambda function.

173
00:08:30,000 --> 00:08:32,000
So first of all I will go ahead and write lambda.

174
00:08:32,000 --> 00:08:34,000
I know what is my operation.

175
00:08:34,000 --> 00:08:35,000
My operation is basically squaring right.

176
00:08:35,000 --> 00:08:37,000
So I will create a argument.

177
00:08:37,000 --> 00:08:42,000
So argument I will be requiring a number x and the expression will be x square okay.

178
00:08:42,000 --> 00:08:43,000
So this basically becomes lambda.

179
00:08:43,000 --> 00:08:50,000
Right now map basically requires the first parameter in map is basically a function.

180
00:08:50,000 --> 00:08:53,000
The second parameter is iterables.

181
00:08:53,000 --> 00:08:55,000
So this actually is my lambda function.

182
00:08:55,000 --> 00:08:57,000
Or you can also call this particular function.

183
00:08:57,000 --> 00:08:59,000
But right now I'm giving my lambda function over here.

184
00:08:59,000 --> 00:09:04,000
And the second parameter that I'm actually going to give is nothing but my numbers.

185
00:09:04,000 --> 00:09:04,000
Right.

186
00:09:04,000 --> 00:09:07,000
So here is basically my map right now.

187
00:09:07,000 --> 00:09:13,000
At the end of the day, if I execute this, this basically creates a map object at this particular memory

188
00:09:13,000 --> 00:09:13,000
location.

189
00:09:13,000 --> 00:09:19,000
And map usually uses a lazy loading technique that basically means uh, still, uh, it has not completely

190
00:09:19,000 --> 00:09:20,000
initialized.

191
00:09:20,000 --> 00:09:23,000
So to make it initialized, I will convert this into a list.

192
00:09:23,000 --> 00:09:24,000
Now see the magic?

193
00:09:24,000 --> 00:09:30,000
Okay, for every number in this particular list, automatically you are able to get the square right.

194
00:09:30,000 --> 00:09:31,000
Isn't it beautiful?

195
00:09:31,000 --> 00:09:34,000
So I will just try to show you like how you can use Map Lambda.

196
00:09:34,000 --> 00:09:37,000
And there is also one more function which is called as filter.

197
00:09:37,000 --> 00:09:38,000
Right.

198
00:09:38,000 --> 00:09:40,000
And uh, I'll explain all these things in my upcoming video.

199
00:09:40,000 --> 00:09:42,000
So yes, this was it for my side.

200
00:09:42,000 --> 00:09:43,000
I will see you all in the next video.

201
00:09:43,000 --> 00:09:44,000
Thank you.

