Open In App

How to use if, else & elif in Python Lambda Functions

Last Updated : 08 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Lambda function can have multiple parameters but have only one expression. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to use if, else & elif in Lambda Functions.

Using if-else in lambda function

The lambda function will return a value for every validated input. Here, if block will be returned when the condition is true, and else block will be returned when the condition is false. 

Syntax:

lambda <arguments> : <statement1> if <condition> else <statement2>

Here, the lambda function will return statement1 when if the condition is true and return statement2 when if the condition is false.

Example:

Here, We are going to find whether a number is even or odd. when we pass number 12 to lambda function it will execute statement 1 and statement2 for 11. 

Python3




# Use if-else in Lambda Functions
 
# check if number is even or odd
result = lambda x : f"{x} is even" if x %2==0 else f"{x} is odd"
 
# print for numbers
print(result(12))
print(result(11))


Output

12 is even
11 is odd

Using if else & elif in lambda function

We can also use nested if, if-else in lambda function. Here we will create a lambda function to check if two number is equal or greater or lesser. We will implement this using the lambda function.

Syntax:

lambda <args> : <statement1> if <condition > ( <statement2> if <condition> else <statement3>)

Here, statement1 will be returned when if the condition is true, statement2 will be returned when elif true, and statement3 will be returned when else is executed. 

Example:

Here, we passed 2 numbers to the lambda function. and check the relation between them. That is if one number is greater or equal or lesser than another number

Python3




# Use if-else in Lambda Functions
 
# check if two numbers is equal or greater or lesser
result = lambda x,y : f"{x} is smaller than {y}" \
if x < y else (f"{x} is greater than {y}" if x > y \
               else f"{x} is equal to {y}")
 
 
# print for numbers
print(result(12, 11))
print(result(12, 12))
print(result(12, 13))


 
 

Output

12 is greater than 11
12 is equal to 12
12 is smaller than 13

 



Previous Article
Next Article

Similar Reads

Avoiding elif and ELSE IF Ladder and Stairs Problem
This article focuses on discussing the elif and else if ladder and stairs problem and the solution for the same in the C and Python Programming languages. The ELIF and ELSE IF Ladder and Stairs ProblemThere are programmers who shy away from long sequences of IF, ELSE IF, ELSE IF, ELSE IF , etc. typically ending with a final ELSE clause. In language
6 min read
Python3 - if , if..else, Nested if, if-elif statements
There are situations in real life when we need to do some specific task and based on some specific conditions, we decide what we should do next. Similarly, there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases, conditional statements can be used. The following are the conditio
5 min read
Lambda with if but without else in Python
In Python, Lambda function is an anonymous function, which means that it is a function without a name. It can have any number of arguments but only one expression, which is evaluated and returned. It must have a return value. Since a lambda function must have a return value for every valid input, we cannot define it with if but without else as we a
3 min read
Python Lambda Functions
Python Lambda Functions are anonymous functions means that the function is without a name. As we already know the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python. Python Lambda Function SyntaxSyntax: lambda arguments : expression This function can have any nu
9 min read
Using Apply in Pandas Lambda functions with multiple if statements
In this article, we are going to see how to apply multiple if statements with lambda function in a pandas dataframe. Sometimes in the real world, we will need to apply more than one conditional statement to a dataframe to prepare the data for better analysis. We normally use lambda functions to apply any condition on a dataframe, Syntax: lambda arg
3 min read
Applying Lambda functions to Pandas Dataframe
In Python Pandas, we have the freedom to add different functions whenever needed like lambda function, sort function, etc. We can apply a lambda function to both the columns and rows of the Pandas data frame. Syntax: lambda arguments: expression An anonymous function which we can pass in instantly without defining a name or any thing like a full tr
4 min read
Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Lambda and filter in Python Examples
Prerequisite : Lambda in Python Given a list of numbers, find all numbers divisible by 13. Input : my_list = [12, 65, 54, 39, 102, 339, 221, 50, 70] Output : [65, 39, 221] We can use Lambda function inside the filter() built-in function to find all the numbers divisible by 13 in the list. In Python, anonymous function means that a function is witho
2 min read
Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Article Tags :
Practice Tags :