Get startedGet started for free

What is a lambda expression?

1. What is a lambda expression?

Good job dealing with different argument types! In this lesson we'll recall what a lambda expression is and how to write one in Python.

2. Definition

A lambda expression or a lambda function is a short function having the following syntax:

3. Definition

It has a lambda keyword,

4. Definition

the sequence of arguments followed by a colon,

5. Definition

and the expression that uses these arguments. A good example is a squared number. After assigning this expression to a variable, we can use this variable with a value enclosed in round brackets. In this case, the lambda expression is evaluated for x equal 4.

6. Definition

The same happens if we use more arguments. Let's define the expression raising x to the power of y. Using this variable with the following values implies evaluating the lambda expression for 2 and 3.

7. Missing argument

Remember that missing an argument in the call results in TypeError.

8. Comparison to normal function definition

Let's compare lambda functions to normal function definitions, taking the example on squaring a number. Using normal definition this expression can be represented like this. As can be seen, one of the advantages of lambda expressions is writing less code for simple operations. Let's have a deeper comparison.

9. Comparison to normal function definition

The def keyword can be associated with the lambda keyword.

10. Comparison to normal function definition

A lambda expression can be assigned to some variable, whereas normal definition must have a name.

11. Comparison to normal function definition

Here are the arguments.

12. Comparison to normal function definition

And finally, the operation itself. Notice that in lambda expressions we don't need to specify the return keyword. The final operation already implies that the corresponding result will be returned. As can be seen, calling functions is identical in both cases, and both functions have the same output.

13. Passing lambda function as an argument

So, what are other advantageous sides of lambda functions in addition to being short? Assume we have the following function. The first argument is just a number. The second one is a function itself that has one argument in its definition. The functions that represent arguments to other functions are usually called callback functions or simply callbacks. Recall squared_normal() we defined before. We can pass it as a callback argument to our function_with_callback(). Do you notice the inefficiency here?

14. Passing lambda function as an argument

To do a tiny little thing, we have to define an entire function and pass it as an argument. This can be improved.

15. Passing lambda function as an argument

We can pass a lambda expression directly in a function call. This is very handy when a callback represents something short like in this example. Such a modification simplifies the code and its readability. Notice that we don't need to give any name to the lambda expression in this case: all we need is its main components.

16. Definition

Since we don't define a name for a lambda expression,

17. Definition

it is often called an anonymous function. Therefore, we can add "anonymous" descriptor in brackets in our previous definition. This property can be supported by another example. Recall the lambda expression we defined before. We assigned it to a variable.

18. Definition

Actually, it is not necessary. We can define a lambda expression and call it directly by passing a necessary value. This supports the idea of anonymity.

19. Ternary operator

Let's discuss one more thing on lambda expressions. Consider this simple function that returns 'odd' or 'even' depending on the number passed. We can substitute the if-else block with

20. Ternary operator

with its one-line version called ternary operator. It returns 'even' if the condition between 'if' and 'else' is fulfilled. Otherwise, it returns 'odd'. Since it's a one-line version,

21. Ternary operator

we can use it in a lambda expression, which can be very useful sometimes.

22. Practical use

Although lambda expressions are easy to use, they are not substitutes to the normal function definition and have to be used only when it's really necessary! Better to follow practical guidelines: to use lambda expressions within function bodies to handle some small tasks or use them as callbacks.

23. Let's practice!

And now, let's practice.