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'

更新日期: 26-Feb-2020

184 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.