0:00
hello everyone my name is and today I'm
0:02
going to teach you about Lambda
0:04
functions in Python what is a Lambda
0:07
function in Python it is an anonymous
0:11
expression it is essentially a function
0:14
without a function name so it can take
0:17
any number of argument including no
0:19
arguments but it only has a single
0:21
expression so let's look at how this
0:25
works so essentially Lambda is just a
0:27
shorter way to define a simple function
0:33
expression so how it works the Lambda
0:35
syntax is that you taken some argument
0:39
one zero or multiple and you have just
0:42
one expression and you can uh execute it
0:46
so let's make a simple example of how uh
0:50
is structured a Lambda function so the
0:53
first thing is you can say okay I have
0:56
Lambda x and x + one so that's our
1:00
structure for a Lambda function if we uh
1:04
run this you'll see that it just
1:06
returned function name so it hasn't been
1:08
called yet uh in order to call this uh
1:12
we'll see later how to call it uh next
1:15
you can have a different number of
1:17
arguments so you can say x y z and say
1:23
uh print XY Z so you can see the Lambda
1:28
function can have have multiple
1:31
arguments here but has a single
1:33
expression you can also have Lambda
1:36
without a single argument by saying just
1:43
Lambda uh so you don't need actually uh
1:46
any argument if we run this we still
1:49
have just a function and we have an
1:53
example of how we can use this you can
2:00
function and you can assign it to a
2:02
variable and in this case you get print
2:06
a Lambda and if we call that variable
2:09
then we can see it still returns that
2:12
function main if you want to execute the
2:14
Lambda function you have some uh some
2:18
idea what's going on here is you use a
2:21
parentheses right there and then you
2:23
call that Lambda function and that's how
2:26
you use Lambda that's the simplest way
2:30
if you have multiple Arguments for
2:33
example uh you can uh let's see later uh
2:38
there's a second way you can actually
2:41
execute a Lambda function without
2:43
assigning it to a variable because
2:45
Lambda functions want to be anonymous so
2:48
uh you don't want to assign it for
2:50
example so you could just say uh okay
3:02
hello and then you return
3:07
this uh it's missing the argument okay
3:10
yeah sorry you just print and you return
3:13
this if you want to add an argument then
3:15
you would say uh print you could say
3:19
Lambda X and you could say hello X and
3:23
then here you could say hello JC and
3:26
then you can provide that argument
3:29
within the next parenthesis so that's a
3:33
second way you can execute a Lambda
3:37
function if you have a single argument
3:40
in your Lambda we've just looked at it
3:43
but single argument and you do
3:46
Lambda x x + one as we have just seen
3:50
then in order to call the single
3:52
argument function uh Lambda function you
3:55
just use this parenthesis if you run it
3:58
will have the same type error that we
4:00
just discussed uh then you can put a
4:04
number instead and get the actual
4:08
result you can also have multiple
4:11
arguments as we hinted
4:13
earlier and to do this you do
4:17
Lambda x y and then you can say x + y so
4:22
that's can be one function with multiple
4:25
arguments and you have to think that the
4:28
order that you define Define them is the
4:30
order that you're going to have to
4:32
Define them so 3 + one and that's how
4:35
you can call a Lambda function with any
4:40
arguments so what exactly does Lambda do
4:44
in Python Lambda it's a function and it
4:48
returns Panda expression for that
4:49
argument it is very similar uh to um how
4:55
function uh identity function works so
4:59
if you are used to uh defining function
5:03
using the def keyword let's say a def
5:06
funk you have X put the argument and
5:10
then return x + one so that's
5:15
essentially if you run this uh function
5:20
do X2 you will do replace X by2 and it's
5:25
going to return three the Lambda
5:27
function is very similar and as we have
5:30
seen Lambda Funk equal
5:38
one so if we do check whether or not
5:42
Lambda function does the same thing we
5:44
can see that X2 and Lambda function 2 is
5:50
equal to three uh true in that case
5:53
showing that the results of this
5:55
function and the result of this function
5:57
will return the same thing
6:01
you can use Panda python Lambda inside
6:06
functions so python supports functional
6:09
programming so you can return a function
6:12
as the result of another function so we
6:16
function that we say it will increment
6:21
n uh and return and instead of just
6:24
doing some number n + one for example as
6:30
what we want to know is we want to use a
6:33
function that will have x x + n so in
6:38
this case if we take the incremental
6:41
function and we assign it to F and we
6:44
say that the increment is 10 uh we're
6:48
still missing something here we don't
6:50
know what x is we've defined n here
6:54
using 10 so n equal 10 so in order to
6:57
use this what we want to do do is we
7:00
want to take F the Lambda function and
7:02
we want to give it the value of x and if
7:05
we re return this we'll see that it
7:08
returns 12 so we can print some results
7:11
and we can also use it to assign it to a
7:16
new function so we can say instead of
7:18
the increment of n equal 10 we can say
7:21
the increment is n equal uh three
7:25
instead and then if we print all of this
7:28
we'll see the difference
7:30
uh between changing the incrementer we
7:33
can see that we're calling a function
7:35
another function using the Lambda
7:40
expression so when should you use uh
7:45
Lambda you can use Python Lambda to sort
7:48
a list of two Poes so here we have a
7:53
poles and let's say we want to sort that
7:56
list using the one two three so we want
7:58
to sort them in order of
8:01
appearance uh we can use Lambda for that
8:04
so we can say Okay sorted pair so that's
8:07
the variable when we want to sort this
8:10
we can use the sorted function in Python
8:13
we pass in the pairs and you know we can
8:16
Define which key we want to
8:19
use uh and in this case for the key
8:22
we're going to pass in the Lambda X and
8:26
we're going to select the first value of
8:28
x that means X will be the two pole that
8:32
will passed be passed and we will return
8:35
the first value here so that's what we
8:38
do we sort based on the first key and if
8:41
we do sorted pair here you'll see that
8:44
it's sorted from one to four and if we
8:46
were to use Lambda 1 it would be sorted
8:49
alphabetically 41 32 instead of by
8:54
numbers we can also Define a function by
9:00
so we can say result equal and we want
9:04
to so the the structure it does is it
9:07
takes a a on line IFL so we say Lambda
9:12
X and we say return greater than five if
9:19
five else return smaller than five so
9:24
that's a oneliner IFL statement you can
9:27
learn about this in a separate video
9:29
tutorial uh but let's say if we want to
9:32
test we can say that this one should uh
9:35
return greater than five and this one
9:36
should return smaller than five and that
9:41
case there are other very cool things
9:44
that we can do with Lambda functions is
9:47
that we can take a list range six let's
9:50
start with this and show the nums uh and
9:53
we say that we have a list of 0 1 2 3 4
9:57
five let's say we want to just keep the
10:01
numbers so what we can do is we can use
10:05
the filter function so when I say we
10:09
want to keep just the the the the even
10:12
number that means I want to filter use
10:15
the filter to to to keep holding these
10:19
Lambda X and in order to get filters
10:24
there is the very useful module function
10:28
that we can use so if module 2 equals
10:31
equals z and then the filter function
10:34
requires me to pass in the list and then
10:37
I can see I have indeed managed to
10:39
filter to keep only even
10:43
numbers if I want to do other
10:45
mathematical operations I can do so by
10:48
using the map function instead so if I
10:51
want to square every single item I would
10:55
do list map I will map Lambda
11:00
x x 2 and nums and that will return the
11:06
Square value so essentially it will map
11:09
all the values from nums times
11:13
two here you can see that it
11:17
works so it's super useful because you
11:20
can start by a project by putting all
11:23
your mathematical operations so you can
11:25
say I want to add subtract I want to
11:28
divide so you can see that I can create
11:31
a a single um function for every single
11:36
possible mathematical operation that I
11:38
want to do in my project so I can Define
11:40
it at first and then I can just call
11:42
them like that and not focus on doing it
11:46
ever again later on so that's is a very
11:49
useful way of using penda
11:53
functions you can also do this with
11:55
strings let's say I have a list of
11:57
emails I can also use list maps to do
12:02
other more complex operation list map
12:05
and I can say Lambda and let's not call
12:10
instead just because we can uh and we do
12:14
split and then we split on the a
12:19
mcent uh and essentially we want to keep
12:23
just the domain so essentially what
12:25
we're doing here is we're mapping taking
12:27
a list here and we m
12:30
uh the M the Lambda function then we
12:32
return the emails and we get those
12:34
strings as we can see here we're
12:37
starting to get in the realm of the a
12:39
little bit more complex notation and
12:41
maybe a loop uh would be simpler but
12:49
you so the next piece is the most the
12:53
the place where I use Lambda function
12:55
the most is generally I have a panda
12:58
data frame and I want to apply some
13:00
operation to the score uh column so I
13:03
want to apply something to every item in
13:05
a score column and that is a p one of
13:09
the powerful way that Lambda can do uh
13:12
of what Lambda can do so you can do
13:14
score Pur so let's say we want to
13:17
convert the point notation the float
13:21
notation to a percentage string we can
13:24
say DF score and we use the apply
13:27
function that will say essentially go
13:29
through each item and apply a function
13:32
and in this case the function is a
13:36
function X will take in the value so it
13:39
will go first X second X and for each
13:43
item it will Loop X let's say I want to
13:45
create a a F string that ends up as a
13:49
percentage and I need to put in x * 100
13:54
so in that case if I run this and I
14:00
you can see I've added a new column with
14:03
the string notation for the
14:08
percentage uh now we can also use and
14:12
now we're getting into a little bit more
14:14
complex uh things uh but uh I want to
14:18
show you how you can use Lambda function
14:20
as a decorator so uh let's say you have
14:23
a function and you want to convert it so
14:26
let's I have let's start with the Lambda
14:28
function so I have my Lambda
14:31
function and the Lambda function
14:41
world so I have that Lambda function
14:44
that just essentially just does hello
14:48
world and the output that I want is
14:54
calling something like hello
14:57
Lambda that that's the output that I
15:00
want to do so I can create a decorator
15:03
function so I can say my
15:07
decorator and it takes a function as an
15:10
input so it takes the Lambda function as
15:13
an input and I put the wrapper instead
15:18
inside so I'm not going too deep into
15:21
what decorators are but let's take
15:24
result and I have Lambda function
15:32
runs it runs the Lambda function and it
15:43
result so that's what the function does
15:51
rapper it takes in this so how you use
15:55
that decorator then you can use uh
16:04
decorator my Lambda so you pass in the
16:07
Lambda function to The Decorator and if
16:11
we return this it should return what we
16:15
we trying to do which is decorated hello
16:19
world so this is it uh thanks for
16:22
following me uh please hit like And
16:25
subscribe and or follow me on one of my
16:29
uh website or social media thank you
16:31
very much see you next time