Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - Lambda function to find the smaller value between two elements
Next article icon

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. 

Python
# Use if-else in Lambda Functions# check if number is even or oddresult=lambdax:f"{x} is even"ifx%2==0elsef"{x} is odd"# print for numbersprint(result(12))print(result(11))

Output
12 is even11 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 <condition1> else ( <statement2> if <condition2> 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

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

 
 


Output
12 is greater than 1112 is equal to 1212 is smaller than 13


 


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp