Python Set – any() Function

It is a function in python that returns True if any of the items in the set are True else returns False. 1 is considered as True and 0 is considered False.

Syntax:
any(itearable)

Also Read: Python Set – all() Function

Example 1:

# Python code to understand any() function in set
# www.codewindow.in

s = {True, True, True}
x = any(s)
print(x)

Output:

True

Explanation:
Here all the items in the set are True (at least one needs to be True). Hence returned True.

Example 2:

# Python code to understand any() function in set
# www.codewindow.in

s = {True, False, False}
x = any(s)
print(x)

Output:

True

Explanation:
Here there is a False in the second position of the set else rest are all True (at least one is True). Hence returned True.

Example 3:

# Python code to understand any() function in set
# www.codewindow.in

s = {0, 1, 1}
x = any(s)
print(x)

Output:

True

Explanation:
Here 0 is considered as false but rest all are 0 hence rest all are True (at least one is true). Hence returned True.

Example 4:

# Python code to understand any() function in set
# www.codewindow.in

s = {0, 0, 0}
x = any(s)
print(x)

Output:

False

Explanation:
Here all the elements are 0 (False) none of them are 1 (True) therefore all are True. Hence returned False.


Follow Us

You Missed

Also Checkout