您的位置:首页 > 编程语言 > Python开发

python的三元运算符

2013-07-11 00:10 190 查看
Yes, it was added in
version 2.5. It's frowned upon by some pythonistas, so keep that in mind.

The syntax is:
a if test else b


First
test
is
evaluated, then either
a
or
b
is
returned based on the Boolean value
of
test
;

if
test
evaluates
to True
a
is
returned, else
b
is
returned.

For example:
>>> 'true' if True else 'false'
'true'
>>> 'true' if False else 'false'
'false'


Official documentation:

Conditional
expressions

Is
there an equivalent of C’s ”?:” ternary operator?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: