首先,python中没有C语言中的三目运算,即Python没有 a ? b : c 的语法。但是可以python有两种方法模拟这种运算。
C语言版本: result = condition ? true_part : false_part
python版本1: result = condition and true_part or
false_part
python版本2: result
=
true_part if condition else false_part
本文介绍了Python中模拟C语言三目运算的方法,提供了两种实现方式,并对比了不同版本间的语法差异。
首先,python中没有C语言中的三目运算,即Python没有 a ? b : c 的语法。但是可以python有两种方法模拟这种运算。
C语言版本: result = condition ? true_part : false_part
python版本1: result = condition and true_part or
false_part
python版本2: result
=
true_part if condition else false_part

被折叠的 条评论
为什么被折叠?
