?: in Python

gene tani gene.tani at gmail.com
Wed Dec 14 16:06:14 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

google for ternary operator

http://www.python.org/doc/faq/programming.html#is-there-an-equivalent-of-c-s-ternary-operator
http://www.norvig.com/python-iaq.html

http://mail.python.org/pipermail/python-list/2005-November/310854.html
http://www.python.org/peps/pep-0308.html

http://diveintopython.org/power_of_introspection/and_or.html#d0e9975
http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52310




More information about the Python-list mailing list