Custom Search

Monday, October 11, 2010

python ternary operator example

python ternary operator examples

Syntax:
(cond and [val_1] or [val_2])[0]


Example:
>>> a=10
>>> b=5
>>> (a > b and [a] or [b])[0] <---- Note the bracket, 'a' and 'b' in a list and [0]
10
>>>
>>> a=5
>>> b=10
>>>
>>>
>>> (a > b and [a] or [b])[0]<---- Note the bracket, 'a' and 'b' in a list and [0]
10
>>>

No comments:

Post a Comment