(upbeat music)
- [Instructor] If you've done any programming
in other languages, such as JavaScript,
or C#, or Java, you've probably seen
or worked with anonymous functions.
Python also supports these
and they are referred to as lambda functions.
Lambda functions can be passed as arguments
to other functions to perform some processing work,
much like a callback function in a language like JavaScript.
Typically, you see these used in situations
where defining a whole separate function
would needlessly increase the complexity of the code
and reduce readability.
Lambdas are defined by using the keyword lambda,
followed by any arguments that the lambda function takes
and then followed by an expression.
Let's look at how they are used in practice,
because that's usually the best way to understand something.
Here in VS Code, at the top of the file,
I have two regular functions,
each of which performs a conversion
from one temperature scale to another.
This one converts Celsius temperatures to Fahrenheit
and this one does the opposite.
In the main function, I have two lists of temperatures;
one in the Celsius scale and one in the Fahrenheit scale.
Suppose I wanted to convert each of these lists
into the other temperature scale.
To do this, I might use the map function.
The map function takes a function
as the first argument and an iterable object,
like a list, as the second.
To convert these two lists,
I would write something like map
and then I would call Fahrenheit to Celsius as the function.
Then I would pass the ftemps list.
Then to print this out, I'll put this inside a list
to generate the list.
Then I'll just call print on the entire thing
and I'll copy and paste that,
and do the same thing with Celsius to Fahrenheit
and pass in the ctemps.
Let's run what we have.
I'll go to debug view.
Remember, you can run from the Terminal command-line
if you don't want to use VS Code.
I'll run this.
You can see the results here
that each of the temperatures has been converted.
I could just reduce the complexity of my code
by writing each of these functions
as an in-line lambda, because they're pretty simple.
Let's go back and do that.
I'll clear the console.
I'm going to copy these two lines
and paste them down here.
Now I'll replace each function with a lambda equivalent.
I'll put in lambda T, because each lambda function
will take a temperature as an argument.
Then for the Fahrenheit to Celsius case,
I will copy this expression
and paste it in here.
I change that to T 'cause it's not temp anymore.
The same thing here.
I'll write lambda T,
and now I will get the Celsius to Fahrenheit version.
I'll copy that, paste it in,
and change that to T.
Now you can see that the results are the same.
In this particular case,
using the lambda expression really simplifies my code
because I can see the calculation
right where it's being used.
Someone else who has to work with my code,
even if that's me several years from now,
doesn't have to go digging through all the source code
to find out where the conversion functions are defined.
Obviously, lambdas aren't a good fit for every scenario.
In practice, you will, of course,
continue to use regular functions in your programs,
but lambdas can help make your code more readable
when defining a full function
is more effort than it's worth.
(upbeat music)