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

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

WBOY
Release: 2016-06-06 11:22:07
Original
1020 people have browsed it

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

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

代码如下:

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

可以保证[a]为True。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template