Lambda expressions are a way to create a one time use function.
So currently we have a function like 'def func_name', and it
returns a thing.
And then we can run 'func_name', and we can run this over and
over and over and over and over and over and over again.
And that's the benefit of a named function like this, is
we can use it over and over and over again.
That's actually why we name it.
Now, a Lambda is more of a onetime use function where doesn't
really need a name.
And in other programming languages like JavaScript, this
is actually called an anonymous function.
So let's just jump right into this.
Let's say we want to get the square root of something.
We could create a function called 'square', take a number,
and 'return num ** 2'.
That is a way that we could do this.
Or we could use a Lambda expression.
And a Lambda expression is basically, you know you've got your
variable name is equal to, and then the keyword 'lambda', and then
your parameters in here,
so we just have one parameter here, colon, and then it doesn't
even take the 'return' keyword.
It just assumes that whatever is after this, is what's going to be returned.
So lambdas are really good for simple, simple functions.
So we could 'return num ** 2'
Now, if we just use 'square', we're going to see that this is a
function, and it is actually a Lambda, and it takes one
parameter called 'num'.
That's not useful when we actually want to get the square
root of something. So we could 'square()',
and let's put the number 5 in here.
25. We could 'square', let's do a big number, 909.
It's a pretty big number.
And if we wanted to, we could make another Lambda that takes
more than one parameter.
We could make a Lambda expression or an anonymous function
to just add numbers
if we wanted to. 'add = lambda num1, num2:',
and it's just going to return 'num1 + num2', that's it.
So we can say 'add(1, 4)', and this will return 5 for us,
just like that.
Now, that's overly simplistic.
This one is also overly simplistic because we can just
do that very easily.
We could literally just say any 'varname = 1 + 4'.
But if you have something that's a little more complex,
or really only ever needs to be run once, you could absolutely
do that as a Lambda function.
But if you have something a little more complex, like you
have a bunch of 'if' statements, and 'else' statements, and it's
running other functions and stuff, you probably don't want
to use a Lambda for that.
Your Lambda expressions should be nice and simple.
Now you're not going to be using lambdas probably every single
day. I don't see most people using lambdas all the time,
but they are vitally important in Python because sometimes
you don't want that function to be available all over the
place. Sometimes you just want it to be a one line, do a
basic little math thing, or some sort of simple logic, and
just assign it, assign it to a variable.
And then once it's done, you can forget about it because
it's a variable. Who cares?
But once again, if your Lambda expression starts to get complex,
that is actually a really good time to split it out.
And by complex, I mean, if you're looking at a Lambda and you're like,
"I don't understand all this other jargon that's
going on over here", that's a good time to split it out into a proper function.
But if you need a simple, one time function, Hey, lambdas
are the best way to do that.