Home >Backend Development >Python Tutorial >Python中AND、OR的一个使用小技巧

Python中AND、OR的一个使用小技巧

WBOY
WBOYOriginal
2016-06-06 11:22:071022browse

python中的and-or可以用来当作c用的?:用法。比如 1 and a or b,但是需要确保a为True,否则a为False,还要继续判断b的值,最后打印b的值。

今天看到一个好方法避免这种情况,记录一下:

代码如下:

(1 and [a] or [b])[0]

可以保证[a]为True。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn