Sitemap
Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Member-only story

The any() Function in Python

2 min readJul 31, 2021

--

Photo by Pakata Goh on Unsplash

or operator

Let’s say a job posting only requires one of the following to be true: at least 10 years of SQL experience, or 12 years of machine learning experience, or 8 years of statistical analysis experience.

We can write a function that checks if any of those are True by using the or operator:

def is_qualified(sql, ml, stats):
if (sql >= 10) or (ml >= 12) or (stats >= 8):
return True
else:
return False
is_qualified(11, 11, 7)
# True
is_qualified(9, 10, 7)
# False

We used the or operator in the above function. The or operator returns True if any of those expressions evaluate to True. If all the expressions evaluate to False, the or operator returns False.

any() function

Again, there is an easier way to do this, and that’s with using the any() function.

any(iterable)

As the name implies, the any() function takes in an iterable, and checks if any of the elements evaluate to True. If at least one of the elements is Truthy (evaluates to True), then it will return True.

The any() function returns

--

--

Luay Matalka
Luay Matalka

Written by Luay Matalka

Lead Machine Learning Engineer, specializing in MLOps, with a passion for teaching.