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

python function - all()

高洛峰
Release: 2016-10-17 15:40:46
Original
1126 people have browsed it

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
Copy after login

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
Copy after login


Note: The return value of empty tuple and empty list is True, please pay special attention here


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