三元表达式
2018-12-07 本文已影响6人
庵下桃花仙
value = true-expr if condition else false-expr
In [1]: x = 5
In [2]: 'Non-negative' if x >= 0 else 'Negative'
Out[2]: 'Non-negative'
In [1]: x = 5
In [2]: 'Non-negative' if x >= 0 else 'Negative'
Out[2]: 'Non-negative'