Python内置函数 — all,any
1、all
源码注释:
def all(*args, **kwargs): # real signature unknown"""Return True if bool(x) is True for all values x in the iterable.If the iterable is empty, return True."""pass
语法格式:
all(iterable)
如果 iterable 的所有元素均为真值(或可迭代对象为空)则返回 True 。
用法实例:
(1)所有元素为真,返回 True
print(all([1, 2, 3]))
---------------------------------------------------------------------------
True
(2)有的元素为假,返回 False
print(all([0, 1, 2]))
--------------------------------------------------------------------------
False
(3)对象为空,返回 True
print(all([]))
-----------------------------------------------------------------------
True
2、any
源码注释:
def any(*args, **kwargs): # real signature unknown"""Return True if bool(x) is True for any x in the iterable.If the iterable is empty, return False."""pass
语法格式:
any(iterable)
如果 iterable 的任一元素为真值则返回 True。 如果可迭代对象为空,返回 False。
用法实例:
(1)任一元素为真,返回 True
print(any([0, None, 3]))
-----------------------------------------------------------------------
True
(2)所有元素为假,返回 False
print(any([0, None, 0.00]))
--------------------------------------------------------------------------------
False
(3)对象为空,返回 False
print(any([]))
---------------------------------------------------------------------------
False
注:
关于逻辑值真假的判断可以阅读:Python基础 — 逻辑值真假判断_笃行之.kiss的博客-CSDN博客
reference:
内置函数 — Python 3.8.16 文档