Home >Backend Development >Python Tutorial >What is the any function in python? How to use any function?

What is the any function in python? How to use any function?

乌拉乌拉~
乌拉乌拉~Original
2018-08-17 17:54:374578browse

In today's article, we will learn about the pythonany function. In this article, we will explain what the any function is, and where this function is generally used, and Its function and significance.

Description

any() function is used to determine whether the given iterable parameter iterable is all False, then return False, if one is True, then return True.

Elements are all TRUE except 0, empty, and FALSE.

The function is equivalent to:

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

Syntax

The following is the syntax of the any() method:

any(iterable)

Parameters

iterable -- Tuple or list.

Return value

If all are empty, 0, false, return false, if not all are empty, 0, false, then return true.

Example

The following shows an example of using the any() method:

>> > any(['a', 'b', 'c', 'd'])  # 列表list,元素都不为空或0
True
>> > any(['a', 'b', '', 'd'])  # 列表list,存在一个为空的元素
True
>> > any([0, '', False])  # 列表list,元素全为0,'',false
False
>> > any(('a', 'b', 'c', 'd'))  # 元组tuple,元素都不为空或0
True
>> > any(('a', 'b', '', 'd'))  # 元组tuple,存在一个为空的元素
True
>> > any((0, '', False))  # 元组tuple,元素全为0,'',false
False
>> > any([])  # 空列表
False
>> > any(())  # 空元组
False

The above is all about the python built-in functions in this article aspect any function. I hope what I said and the examples I gave can be helpful to you.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What is the any function in python? How to use any function?. For more information, please follow other related articles on the PHP Chinese website!

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

Related articles

See more