Python 三元运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4103667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:16:15  来源:igfitidea点击:

Python ternary operator

python

提问by cynicaljoy

Possible Duplicate:
Ternary conditional operator in Python

可能的重复:
Python 中的三元条件运算符

var foo = (test) ? "True" : "False";

What would this look like in Python?

这在 Python 中会是什么样子?

Using Python 2.7 if that makes a difference.

如果有区别,请使用 Python 2.7。

采纳答案by Brian McKenna

PEP 308adds a ternary operator:

PEP 308添加了一个三元运算符:

foo = "True" if test else "False"

It's been implemented since Python 2.5

它从 Python 2.5 开始实现

回答by Frost.baka

This one looks a bit more like original ternary:

这个看起来更像原始的三元:

foo=a and b or c