Python any () function

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_29720657/article/details/102761167

description:

If any element iterable is true, it returns true. If iterable is empty, false returns. It is equivalent to:

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

Means: determining whether a tuple list or all null, 0, False. If the whole blank, 0, False, False is returned; if (as long as the non [null or 0 or False]) is empty insufficiency, 0, False, True is returned. 

Note: Empty tuple (parentheses) and the empty list (in parentheses), empty dictionary dictionary-empty set (braces) return value is False.

grammar:

any(iterable)

Parameter Description:

iterable---包括string、list、dict、tuple、set()

return value:

The following example shows any () function to use
 

print(any([1,2,3,4,5]))  # 列表list,元素都不为空或0。True
print(any(['a','b','c','d',''])) # 列表list,存在一个为空的元素。True
print(any([1,2,3,0,5]))   # 列表list,存在一个为0的元素。True
print(any([0,False,'']))   # # 列表list,元素全为0,'',false。False
print(any((1,2,3,4,5)))  # 元组tuple,元素都不为空或0。True
print(any(('a','b','c','d',''))) # 元组tuple,存在一个为空的元素。True
print(any((1,2,3,0,5)))   # 元组tuple,存在一个为0的元素。True
print(any((0,False,'')))   # 元组tuple,元素全为0,'',false。False
print(any([])) #空列表。False
print(any(())) #空元组。False
print(any({})) #空集合空字典。False

Export

True
True
True
False
True
True
True
False
False
False
False

This issue any () function will learn here.

Guess you like

Origin blog.csdn.net/qq_29720657/article/details/102761167