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

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Mar 30 13:09:18 EDT 2010


On Tue, 30 Mar 2010 08:40:56 -0700, gentlestone wrote:

> 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)]

You don't need the call to bool.

('No','Yes')[a==b]

 
> Is there a more elegant/common python expression for this?

The above is pretty elegant to my eyes, but you can also do:

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



-- 
Steven



More information about the Python-list mailing list