python内置函数每日一学 -- all()

时间:2022-07-10 11:24:20

all(iterable)

官方文档解释:

Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:

 def all(iterable):
for element in iterable:
if not element:
return False
return True

详解:

如果iterable中存在元素为0、''、False,all(iterable)返回False,否则返回True

注意:

空元组、空列表返回值为True

实例:

 print(all([1,2,3,4,5]))                    # True
print(all(['a','','c',3,4])) # False
print(all([1,0,2,3,4])) # False
print(all(('a','b','c','d','e'))) # True
print(all(('a','','c','d','e'))) # False
print(all(('a','b',0,'d','e'))) # False
print(all([])) # True
print(all(())) # True
print(all([False,1,2,3])) # False