pythonif-statementlambda

How to write lambda function with three conditions


I want to write a function that gives returns 3 if x>y returns 1 if x==0 and returns 0 if x<y.

def make_points():
    return lambda x,y: 3 if x>y else 0 

I tried this but I want to add another condition.


Solution

  • returns 3 if x>y returns 1 if x==0 and returns 0 if x<y

    You can basically write that as you described:

    lambda x,y: 3 if x>y else 1 if x==0 else 0