Lambdas are defined by using the keyword lambda followed by any arguments that the lambda function takes, and then followed by an expression. So, let's look at how they are used in practice because that's usually the best way to understand something. So, here in VS Code, I'll open up lambdas_start.py in my Functions chapter. 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. Now, suppose I wanted to convert each of these lists into the other temperature scale. To do this, I might use the map function, which we learned about earlier in the course. And, recall that the map function takes a function as the first argument, and an iterable object, like a list, as the second. So, to convert these two lists, I would write something like map, and then I would call FahrenheitToCelsius as the function, and then I would pass the ftemps list.
Then, to print this out, I'll put this inside a list to generate the list, and then I'll just call print on the entire thing, and I'll copy and paste that and do the same thing with CelsiusToFahrenheit, and pass in the ctemps. Okay, so let's run what we have. I'll go to debug view. And, remember, you can run from the terminal command line if you don't wanna use VS Code. So, I'll run this. And, you can see the results here that each of the temperatures has been converted.
Now, I could just reduce the complexity of my code by writing each of these functions as an inline lambda because they're pretty simple. So, let's go back and do that. I'll clear the console. And, I'm going to copy these two lines and paste them down here. Now, I'll replace each function with a lambda equivalent. So, I'll put in lambda t because each lambda function will take a temperature as an argument. And, then for the FahrenheitToCelsius case, I will copy this expression and paste it in here.
And, I have to change that to t 'cause it's not temp anymore. And, the same thing here. I'll write lambda t, and now I will get the CelsiusToFahrenheit version. So, I'll copy that, paste it in, and change that to t. All right, so 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, and 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.
Now, 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.
- Truth value testing
- Template strings
- Advanced Python functions
- Advanced collections
- Advanced classes and objects
- Python comprehensions: list, dictionary, and set