AppDividend
Latest Code Tutorials

How to Use If, Else and Elif in Lambda Functions in Python

Python lambda function is the function that is defined without a name. Instead, the anonymous functions are defined using a lambda keyword. In this article, we will discuss how to use if, else if, and else in lambda functions in  Python.

Syntax of Lambda function

lambda arguments: expression

The lambda functions can have any number of parameters but only one expression. That expression is evaluated and returned. Thus, lambda functions can be used wherever a function objects are required.

See the following example of a Lambda function in Python.

# app.py

cube = lambda x: x * x * x

print(cube(11))

Output

python3 app.py
1331

Using if-else in Lambda function

The if-else in lambda function syntax is the following.

lambda <arguments> : <Return Value if condition is True> if <condition> else <Return Value if condition is False>

For example, let’s create the lambda function to check if the given value is between 11 to 22.

1 Comment
  1. anonymous says

    Thanks for this post bro

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.