Determination condition in all () / any ()
all()
And any()
two functions suited for use in determination conditions. These two functions accept an iteration object that returns a Boolean value, where:
all(seq)
: Onlyseq
return when all objects are boolean trueTrue
, otherwise returnsFalse
any(seq)
: As long asseq
any object to a Boolean true is returnedTrue
, otherwiseFalse
If we have the following code:
DEF all_numbers_gt_10 ( Numbers):
"" "only if all the sequence numbers greater than 10, return True " "" IF Not Numbers: return False for n- in Numbers: IF n- <= 10: return False return True
If a all()
built-in function, together with a simple generator expression, the above code can be written as:
def all_numbers_gt_10_2(numbers):
return bool(numbers) and all(n > 10 for n in numbers)
def all_numbers_gt_10_2(numbers): return bool(numbers) and all(n > 10 for n in numbers) x = [-10] print(all_numbers_gt_10_2(x))
False