Python 有三元条件运算符吗?

pythonserver side programmingprogramming更新于 2025/8/3 16:44:17

三元运算符是在 Python 2.5 中添加的。其语法为:

语法

x if expr==True else y

示例

以下示例说明了用法

>>> percent=59
>>> 'pass' if percent>=50 else 'fail'
'pass'
>>> percent=45
>>> 'pass' if percent>=50 else 'fail'
'fail'

相关文章


有用资源