I came across the below expression in python to evaluate if a number is odd or even. While it looks elegant I was surprised to see it work. I guess for the input 3
:
x%2
equals 1
which evaluates to True
and non-empty strings (odd
) evaulate to True
. So we have True and True or <don't have to check because it doesn't matter if True or False as the overall statement will be True>
. But why is the output in that case not True
but odd
- what concept do I miss?
>>> (lambda x:
... (x % 2 and 'odd' or 'even'))(3)
'odd'