(a==b) ? 'Yes' : 'No'

Daniel Fetchinson fetchinson at googlemail.com
Tue Mar 30 18:17:04 CEST 2010


>> Hi, how can I write the popular C/JAVA syntax in Python?
>>
>> Java example:
>>     return (a==b) ? 'Yes' : 'No'
>>
>> My first idea is:
>>     return ('No','Yes')[bool(a==b)]
>>
>> Is there a more elegant/common python expression for this?
>
> return ('Yes' if a == b else 'No')

And for less clutter you can even leave the parenthesis:

return 'Yes' if a == b else 'No'


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list