Python built-in Method - all()
The all()
method is a built-in function in Python that returns True
if all elements in an iterable are 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 all()
method:
all(iterable)
where iterable
can be any iterable object such as a list, tuple, or set.
Here are some examples of how to use all()
:
print(all([True, True, True])) # Output: True print(all([True, False, True])) # Output: False print(all([])) # Output: True
In the first example, all elements in the list [True, True, True]
are True
, so the all()
method returns True
. In the second example, there is one element in the list [True, False, True]
that is False
, so the all()
method returns False
. In the third example, the list is empty, so the all()
method returns True
.
The all()
method can be useful in situations where you need to check if all elements in an iterable meet a certain condition, for example when validating user input or checking the status of a group of sensors.