Python lambda function

Sir if want to create a simple function like div(a function for division ) then also i require if- else condition to check whether the denominator is 0 or not . How can i use if-else condition in lambda functions ? Is there anything like logical if-else(:?) like there is in cpp?

Hey Shreeniwas, there is no ternary operator in python like c++, but you can perform the similar functionality in python as c++.
for eg.:

x, y = 10, 2
div = x if y==0 else x/y 
print(div)

Yes you can use if-else in lambda function.
for eg.

g=lambda x: True if x % 2 == 0 else False
print(g(10))
1 Like