Hackforge Academy

Category: python

Understanding any()

Published on 27 Oct 2026

Explanation

any() returns True when at least one item is truthy and False for an empty iterable. It short-circuits after a truthy item.

Code:

scores = [30, 45, 80, 25]
print(any(score >= 75 for score in scores))

Explanation

all() returns True when every item is truthy and also returns True for an empty iterable. It short-circuits after a falsy item.

Code:

scores = [80, 90, 75]
print(all(score >= 35 for score in scores))

Explanation

Use any() when at least one item must match a condition, such as finding a negative number.

Code:

numbers = [10, 20, -5, 40]
print(any(n < 0 for n in numbers))

Explanation

Use all() when every item must satisfy a condition, such as checking required fields.

Code:

user = {'name': 'Arun', 'email': 'arun@example.com'}
required = ['name', 'email']
print(all(user.get(key) for key in required))

Explanation

Combine them to express rules such as every mark being in range and at least one mark meeting a threshold.

Code:

marks = [45, 70, 82]
valid = all(0 <= m <= 100 for m in marks)
passed = any(m >= 50 for m in marks)
print(valid, passed)

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community