Python built-in Method - any()
The any()
method is a built-in function in Python that returns True
if at least one element in an iterable is True
, and False
otherwise. An iterable is any object in Python that can be looped over, such as a list, tuple, or set.
Here is the syntax for any()
method:
any(iterable)Soecru:www.theitroad.com
where iterable
can be any iterable object such as a list, tuple, or set.
Here are some examples of how to use any()
:
print(any([True, True, True])) # Output: True print(any([True, False, True])) # Output: True print(any([False, False, False])) # Output: False print(any([])) # Output: False
In the first example, all elements in the list [True, True, True]
are True
, so the any()
method returns True
. In the second example, there is one element in the list [True, False, True]
that is True
, so the any()
method returns True
. In the third example, all elements in the list [False, False, False]
are False
, so the any()
method returns False
. In the fourth example, the list is empty, so the any()
method returns False
.
The any()
method can be useful in situations where you need to check if at least one element in an iterable meets a certain condition, for example when validating user input or checking the status of a group of sensors.