Python 是否有條件三元運算子?
三元運算子已新增到 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'
廣告
三元運算子已新增到 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'