Lambda Expressions in Python is one of the most useful tools which helps in creating an anonymous function.

Anonymous function in python is a function which has no name and can be created without a def keyword.

Function created using lambda expression works exactly same as that of a normal function using def but the only difference is that lambda’s body is a single expression, not a block of statements.

Basically, lambda’s body only contains what we return in def function body’s.

Let’s see what I am talking about by slowly converting a def function into a lambda function.

Converting def Function to Lambda Function in Python

Output

Making function simple.

Output

Trying to write the whole function into a new line (Although it’s a bad practice).

Output

Now we remove def and convert it into its lambda form.

But it is an anonymous function and cannot be referenced so let’s assign a label to it.

Output

Python Lambda Examples

Example 1: Lambda Function to check an Even number.

Output

Example 2: Lambda Function to Grab first character of a String.

Output

Example 3: Lambda Function to Reverse a string.

Output

Example 4: Lambda Function to Add two numbers.

Output

I hope this has helped you to learn something about lambda function. If you have any doubt then comment below.

Leave a Reply

Close Menu