Just tooling around for my own amusement, and I want to use a lambda, because I feel like it. Can I replace this function with a lambda?
def isodd(number):
if (number%2 == 0):
return False
else:
return True
Elementary, yes. But I'm interested to know...
And if you don't really need a function you can replace it even without a lambda. :)
(number % 2 != 0)
by itself is an expression that evaluates to True or False. Or even plainer,
bool(number % 2)
which you can simplify like so:
if number % 2:
print "Odd!"
else:
print "Even!"
But if that's readable or not is probably in the eye of the beholder.
Yes you can:
isodd = lambda x: x % 2 != 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With