?: in Python

Steven Bethard steven.bethard at gmail.com
Wed Dec 14 21:35:10 EST 2005


Andy Leszczynski wrote:
> How can do elegantly in Python:
> 
> if condition:
>    a=1
> else:
>    a=2
> 
> like in C:
> 
> a=condition?1:2

Step (1): Wait for Python 2.5[1]
Step (2): Write the code::

     a = 1 if condition else 2

STeVe

[1]http://www.python.org/peps/pep-0308.html



More information about the Python-list mailing list