Conditional statement in a one line lambda function in python?

后端 未结 6 1612
借酒劲吻你
借酒劲吻你 2020-12-12 22:45

Apologies if this has been asked before, but I couldn\'t see it anywhere.

Essentially I\'ve come across a scenario where i need to make use of an if statement inside

6条回答
  •  借酒劲吻你
    2020-12-12 23:14

    Yes, you can use the shorthand syntax for if statements.

    rate = lambda(t): (200 * exp(-t)) if t > 200 else (400 * exp(-t))
    

    Note that you don't use explicit return statements inlambdas either.

提交回复
热议问题