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

Chris Rebert clp2 at rebertia.com
Tue Mar 30 18:01:54 CEST 2010


On Tue, Mar 30, 2010 at 8:40 AM, gentlestone <tibor.beck at hotmail.com> 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)]
>
> Is there a more elegant/common python expression for this?

Yes, Python has ternary operator-like syntax:
return ('Yes' if a==b else 'No')

Note that this requires a recent version of Python.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list