Possible Duplicate:
Conditional operator in Python?
Is there a c-like operator in python that check if a condition holds and assign a value accordingly?
<condition> ? <operation> : <operation>
Possible Duplicate:
Conditional operator in Python?
Is there a c-like operator in python that check if a condition holds and assign a value accordingly?
<condition> ? <operation> : <operation>
The syntax is different in Python.
<operation> if <condition> else <operation>
For example,
x = max(y, z)
is roughly the same as:
x = z if z > y else y
[<f_operation>, <t_operation>][condition] it has the advantage of also grouping together operations but in the left side instead of the right side as in the OP codeifb=0 this x = [10, 3 / b][b > 0] failscondition and true_option or false_option. Second, it's incorrect: it will fail when true_option is false. For example, True and "" or "False" will evaluate to "False", whereas "" if True else "False" will evaluate to "".(condition and [true_option] or [false_option])[0]. Since the t if c else f syntax was introduced I've forgotten the way it went.One of Python's design philosophies seems to be to use words instead of symbols when possible. In this case, the best words to use are if and else. But those words are already taken. So Python cheats a bit and uses syntax to disambiguate the version of if that controls flow from the version of if that returns a value.