Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Lambda with underscore as an argument
Next article icon

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 withif but withoutelse as we are not specifying what will we return if theif-condition will be false i.e. itselse part.

Let's understand this with a simple example of lambda function to square a number only if it is greater than 0 usingif but withoutelse.

Example #1:

Python3
# Lambda function with if but without else.square=lambdax:x*xif(x>0)print(square(6))

Output:

File "/home/2c8c59351e1635b6b026fb3c7fc17c8f.py", line 2    square = lambda x : x*x if(x > 0)                                    ^SyntaxError: invalid syntax

The above code on execution shows SyntaxError, as we know that a lambda function must return a value and this function returns x*x if x > 0 and it does not specify what will be returned if the value of x is less than or equal to 0.

To correct it, we need to specify what will be returned if theif-condition will be false i.e. we must have to specify itselse part.

Let's see the above code with itselse part.

Code:

Python3
# Lambda function with if-elsesquare=lambdax:x*xif(x>0)elseNoneprint(square(4))

Output:

16

Example #2:The first code is withif but withoutelse then second is withif-else.

Python3
# Example of lambda function using if without elsemod=lambdax:xif(x>=0)print(mod(-1))

Output:

File "/home/20b09bdd29e24dfe24c185cd99dfcdfa.py", line 2    mod = lambda x : x if(x >= 0)                                ^SyntaxError: invalid syntax

Now, let's see it using if-else.

Python3
# Example of lambda function using if-elsemod=lambdax:xif(x>=0)else-xprint(mod(-1))

Output:

1

Example #3:The first code is with if but without else then second is with if-else.

Python3
# Example of lambda function using if without elsemax=lambdaa,b:xif(a>b)print(max(1,2))

Output:

File "/home/8cf3856fc13d0ce75edfdd76793bdde4.py", line 2    max = lambda a, b : x if(a > b)                                  ^SyntaxError: invalid syntax

Now, let's see it using if-else.

Python3
# Example of lambda function using if-elsemax=lambdaa,b:aif(a>b)elsebprint(max(1,2))print(max(10,2))

Output:

210

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