if-else statement

Philip Semanchuk philip at semanchuk.com
Sat Jan 10 19:22:00 CET 2009

On Jan 10, 2009, at 9:26 AM, Duncan Booth wrote:

> Gandalf <goldnery at gmail.com> wrote:
>> other languages like PHP or javascript  as this if-else operator like
>> this
>> myVar = checking  == 1? 'string': 'other string'
>> is this stuff exist in python?
> See http://docs.python.org/reference/expressions.html#boolean-operations
> conditional_expression ::=  or_test ["if" or_test "else" expression]
> ...
> The expression x if C else y first evaluates C (not x); if C is  
> true, x is
> evaluated and its value is returned; otherwise, y is evaluated and its
> value is returned.

I'd add to the above that this expression was only added to Python in  
v2.5, so if you want your code to be compatible with versions of  
Python <= 2.4, you should not use the ternary if.


More information about the Python-list mailing list