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

python function - any()

高洛峰
Release: 2016-10-17 15:38:42
Original
1247 people have browsed it

any(iterable)


version: This function is suitable for version 2.5 or above and is compatible with python3 version.


Explanation: If any element of iterable is not 0, '', False, all(iterable) returns True. If iterable is empty, returns False. The function is equivalent to:


Note the difference between this function and the all() function, any means any, and all means all. It is recommended to compare and study the differences and connections between the two. You can refer to "Daily Lecture on Python Functions - all()"


def any(iterable):

for element in iterable:

if element:

return False

return True

Parameter iterable: Iterable object;


Example:



>>> any(['a', 'b', 'c', 'd']) #List list, none of the elements are Empty or 0

True

>>> any(['a', 'b', '', 'd']) #List list, there is an empty element

True

>>> any( [0, '', False]) #List list, all elements are 0,'',false

False

>>> any(('a', 'b', 'c', 'd' )) #Tuple tuple, none of the elements is empty or 0

True

>>> any(('a', 'b', '', 'd')) #Tuple tuple, one of the elements is empty The elements of

True

>>> any((0, '', False)) #Tuple tuple, the elements are all 0,'', false

False

>>> any([ ]) # Empty list

False

>>> any(()) # Empty tuple

False


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