Is there a way to perform if in pythons lambda?
Is there a way to perform if in pythons lambda?
The syntax youre looking for:
lambda x: True if x % 2 == 0 else False
But you cant use print
or raise
in a lambda.
why dont you just define a function?
def f(x):
if x == 2:
print(x)
else:
raise ValueError
there really is no justification to use lambda in this case.
Is there a way to perform if in pythons lambda?
Probably the worst python line Ive written so far:
f = lambda x: sys.stdout.write([2n,][2*(x==2)-2])
If x == 2 you print,
if x != 2 you raise.