Home >Backend Development >Python Tutorial >python function - all()
all(iterable)
version: This function first appeared in python2.5 version, applicable to versions above 2.5, including python3, and is compatible with python3 version.
Explanation: If all elements of iterable are not 0, '', False or iterable is empty, all(iterable) returns True, otherwise it returns False; the function is equivalent to:
def all(iterable): for element in iterable: if not element: return False return True
Parameter iterable: iterable Object;
Example:
>>> all(['a', 'b', 'c', 'd']) #列表list,元素都不为空或0 True >>> all(['a', 'b', '', 'd']) #列表list,存在一个为空的元素 False >>> all([0, 1,2, 3]) #列表list,存在一个为0的元素 False >>> all(('a', 'b', 'c', 'd')) #元组tuple,元素都不为空或0 True >>> all(('a', 'b', '', 'd')) #元组tuple,存在一个为空的元素 False >>> all((0, 1,2, 3)) #元组tuple,存在一个为0的元素 False >>> all([]) # 空列表 True >>> all(()) # 空元组 True
Note: The return value of empty tuple and empty list is True, please pay special attention here