Home >Backend Development >Python Tutorial >python function - all()

python function - all()

高洛峰
高洛峰Original
2016-10-17 15:40:461122browse

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


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
Previous article:python function - any()Next article:python function - any()