【Python】any() all() usage

These are two python built-in APIs;

1 any()

any () is an input iterator objects;
any () for determining the input iterables whether full true / false;
as long as not all the air / false, true outputs;
completely empty / False False output, otherwise the output True;
only false/empty will output false ;


a = [[],"",0,()]	# 全假/空
print(any(a))
>>> False

a = [1," ",]		# 全真
print(any(a))
>>> True

a = [0,1,0,0,0]		# 有真,非全真
print(any(a))
>>> True

print(any([]))				# 空
print(any([[],[],[]]))		# 全空
>>> False

2 all()

The input of all() is an iterable object; it is
used to determine whether the input iterator element is empty/false;
only the element is all true/input empty iterator can output true ;


a = [[],"",0,()]			# 全假/空
print(all(a))
print(all([0,0,0,0,0]))
print(all([[],[],[]]))		# 全是空元素=全假/空
>>> False

a = [1," ",]				# 全真
a = [1,1,1,1]
print(all(a))
>>> True

a = [0,1,0,0,0]				# 有真,非全真
print(all(a))
>>> False

print(all([]))				# 全空
>>> True

3 all() any() difference

3.1 Similarities:

The input is an iterable object ;

3.2 Differences:

all() determines whether the input iterator element is empty or false;
any() determines whether the input iterator is empty or false;

Guess you like

Origin blog.csdn.net/ao1886/article/details/110078860