如果运营商'?'在Python?

时间:2016-04-09 07:24:36

标签: python

Python有一个if运算符'?'像Java?

Java中的示例:

String res = (2 > 1) ? "yes" : "no";

1 个答案:

答案 0 :(得分:3)

不,Python没有?:运算符,但可以使用if / else在一行上获得相同的结果:

res = 'yes' if 2 > 1 else 'no'