Python 三元运算符 [英] Python ternary operator

查看:89
本文介绍了Python 三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这在 Python 中会是什么样子?

What would this look like in Python?

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

Using Python 2.7 if that makes a difference.

推荐答案

PEP 308 添加三元运算符:

foo = "True" if test else "False"

它从 Python 2.5 开始实现

It's been implemented since Python 2.5

这篇关于Python 三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆