Skip to content
Related Articles

Related Articles

Lambda with if but without else in Python
  • Last Updated : 21 Jan, 2021

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 are not specifying what will we return if the if-condition will be false i.e. its else part.

Let’s understand this with a simple example of lambda function to square a number only if it is greater than 0 using if but without else.

Example #1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 the if-condition will be false i.e. we must have to specify its else part.

Let’s see the above code with its else part.

Code:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Lambda function with if-else
square = lambda x : x*x if(x > 0) else None
 
print(square(4))

chevron_right


Output:

16

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Example of lambda fuction using if without else
mod = lambda x : x if(x >= 0)
 
print(mod(-1))

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Example of lambda fuction using if-else
mod = lambda x : x if(x >= 0) else -x
 
print(mod(-1))

chevron_right


Output:

1

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Example of lambda fuction using if without else
max = lambda a, b : x if(a > b)
 
print(max(1, 2))

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Example of lambda fuction using if-else
max = lambda a, b : x if(a > b) else b
 
print(max(1, 2))

chevron_right


Output:

2

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :