Understand Python any() with Examples – Python Tutorial

By | June 16, 2022

any() function is a python built-in function. In this tutorial, we will use some examples to show you how to use it.

Python any()

This function is defined as:

any(iterable)

It will return True if any element of the iterable is true. If the iterable is empty, return False.

This function is equivalent to:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

How to use python any()?

Here are some examples we can find how to use.

For example:

x = [1, 2, 3]
print(any(x))
#True

All elements in x are True, we will get True.

x = ["", 2, 3]
print(any(x))
#True

“” is False, however, 2 and 3 are True, we also can get True.

x = ["", None, 0, [],{},()]
print(any(x))

All elements in x are False, we will get False

False

Leave a Reply