S1Q1 · Check Even or Divisible by 5¶
⚡ Quick Reference
Function: is_even_or_divisible_by_5(num: int) -> bool
Core idea: check two conditions - either one being true is enough.
Key rules:
- Even → num % 2 == 0
- Divisible by 5 → num % 5 == 0
- Either condition is sufficient → use or
Problem Statement¶
Problem
Write a function is_even_or_divisible_by_5(num: int) -> bool that returns True if num is even or divisible by 5, and False otherwise.
Examples:
8
True
10
True
15
True
7
False
Understanding the problem¶
Only one condition needs to be satisfied for the function to return True:
| Condition | Check |
|---|---|
| Number is even | num % 2 == 0 |
| Number is divisible by 5 | num % 5 == 0 |
Use or - if either is true, return True. Only when both are false, return False.
and vs or
- Use
andwhen all conditions must hold simultaneously - Use
orwhen at least one condition is enough
This problem uses or - being even alone is enough, being divisible by 5 alone is enough, and being both (like 10) is also fine.
Tracing all four examples¶
num |
Even? (% 2 == 0) |
Div by 5? (% 5 == 0) |
Result |
|---|---|---|---|
| 8 | ✅ Yes (8 % 2 = 0) | ❌ No (8 % 5 = 3) | True |
| 10 | ✅ Yes (10 % 2 = 0) | ✅ Yes (10 % 5 = 0) | True |
| 15 | ❌ No (15 % 2 = 1) | ✅ Yes (15 % 5 = 0) | True |
| 7 | ❌ No (7 % 2 = 1) | ❌ No (7 % 5 = 2) | False |
Solution approaches¶
def is_even_or_divisible_by_5(num: int) -> bool:
is_even = (num % 2 == 0)
is_div_by_5 = (num % 5 == 0)
if is_even or is_div_by_5:
return True
else:
return False
Each condition named separately - most readable for beginners.
Since both conditions are boolean expressions, return their or directly. No if needed.
Short-circuit evaluation¶
Python's or is short-circuit evaluated:
If the first condition (num % 2 == 0) is already True, Python skips the second condition entirely and returns True immediately. This means for even numbers, the divisibility by 5 check is never computed.
This is the mirror of and short-circuiting - and stops early on False, or stops early on True.
Key takeaways¶
or for "at least one"
Use or when any single condition being true is sufficient. The result is False only when every condition is false.
Return boolean expressions directly
return a or b is cleaner than if a or b: return True else: return False. The expression already evaluates to a boolean.
Contrast with S1Q1 Set 3
Set 3 S1Q1 used and (both conditions required). This problem uses or (either condition sufficient). Recognising which logical operator fits the problem is the key skill.