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.
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
- 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
True True
Membership instead of many ors
Weekend timetable
More examples
Scholarship eligibility
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:
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
Prefer
"B" alone is always truthy, so the first condition is always True.
Using and when ranges can’t overlap
Avoid
Prefer
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
- 1Write a condition for a valid exam time: a weekday between 9 and 17 hours that is not a holiday.
- 2Check 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?