Skip to content

Module 5 · Conditional Statements — topic 6 of 10

Multiple conditions

Combine several requirements in one decision with and, or and not, ranges, membership tests, any() and all(). Learn to write compound conditions that stay readable and correct.

Beginner 12 min read·Lesson 65 of 306 in Python Programming

Learning objectives

  • Combine conditions with and, or and not
  • Use in, any() and all() for many alternatives
  • Keep compound conditions readable with names and parentheses

Definition

Combine several requirements in one decision with and, or and not, ranges, membership tests, any() and all(). Learn to write compound conditions that stay readable and correct.

Introduction

Real rules rarely depend on one fact. A scholarship might require marks above 85 and family income below a limit, or a sports quota. This lesson shows how to express such rules accurately in a single condition.

Why it matters

Business rules, eligibility checks and validation all combine several facts. Expressing them clearly avoids the classic bugs: forgetting a requirement, mixing up and with or, or writing conditions that are always true.

Detailed explanation

and: every part must be true. Use it for requirements that must all hold.

or: at least one part must be true. Use it for alternative ways to qualify.

not: reverses a condition; not (a or b) means neither a nor b.

Ranges: use chained comparisons such as 18 <= age <= 25 instead of two comparisons joined by and.

Several allowed values: use membership, day in ("Sat", "Sun"), instead of day == "Sat" or day == "Sun".

Many conditions from a collection: all(conditions) is true when every item is true; any(conditions) is true when at least one is.

Readability: name sub-conditions (is_merit = marks > 85) and use parentheses whenever and and or appear together.

Before coding a rule with several parts, write one example that should pass and one that should fail for each part. Running those examples afterwards shows immediately whether a requirement was forgotten or combined with the wrong operator.

Syntax

Text
if (a and b) or c:
if value in (option1, option2):
if all(checks):
if any(checks):
  • Parentheses state the grouping explicitly.
  • in replaces chains of == joined by or.
  • all() and any() work on any collection of conditions.

Code examples

and versus or

Python
marks, income = 88, 400_000
print(marks > 85 and income < 500_000)
print(marks > 95 or income < 500_000)
Output
True
True

Membership instead of many ors

Python
day = "Sun"
if day in ("Sat", "Sun"):
print("Weekend timetable")
Output
Weekend timetable

More examples

Scholarship eligibility

Python
def eligible(marks, income, sports_quota, documents):
is_merit = marks > 85
is_low_income = income < 500_000
has_documents = all(documents.values())
return has_documents and ((is_merit and is_low_income) or sports_quota)
applicants = [
("Asha", 90, 300_000, False, {"id": True, "marksheet": True}),
("Ben", 70, 200_000, True, {"id": True, "marksheet": True}),
("Chen", 95, 800_000, False, {"id": True, "marksheet": True}),
("Dev", 92, 100_000, False, {"id": True, "marksheet": False}),
]
for name, *details in applicants:
print(name, eligible(*details))
Output
Asha True
Ben True
Chen False
Dev False

Step-by-step explanation

  • Each requirement is stored in a named Boolean, so the final rule reads like a sentence.
  • all(documents.values()) is True only if every document was submitted.
  • The return line requires documents and either (merit and low income) or the sports quota.
  • Chen has merit but too high an income; Dev qualifies on merit but is missing a document.

Expected output

Running “Scholarship eligibility” prints exactly:

TextOutput
Asha True
Ben True
Chen False
Dev False

Practical applications

  • Loan approvals combine credit score, income and existing debt.
  • E-commerce filters combine price range, brand and rating.
  • Access control checks role and account status together.

Best practices

  • Name each requirement as a Boolean before combining.
  • Use parentheses when mixing and and or.
  • Prefer in, all() and any() over long chains.

Common mistakes

Shortened comparisons

Avoid

if grade == "A" or "B":

Prefer

if grade in ("A", "B"):

"B" alone is always truthy, so the first condition is always True.

Using and when ranges can’t overlap

Avoid

if age < 13 and age > 60:

Prefer

if age < 13 or age > 60:

No age is both under 13 and over 60, so the and version is never true.

Key takeaways

  • and needs every part; or needs one; not reverses.
  • in, all() and any() simplify many conditions.
  • Named sub-conditions keep compound rules readable.

Try it yourself

  1. 1
    Write a condition for a valid exam time: a weekday between 9 and 17 hours that is not a holiday.
  2. 2
    Check whether every value in a list of marks is at least 35 using all().

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic