Skip to content

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.

def is_even_or_divisible_by_5(num: int) -> bool:
    return num % 2 == 0 or num % 5 == 0

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:

Input
8
Output
True
Input
10
Output
True
Input
15
Output
True
Input
7
Output
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 and when all conditions must hold simultaneously
  • Use or when 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.

def is_even_or_divisible_by_5(num: int) -> bool:
    return num % 2 == 0 or num % 5 == 0

Since both conditions are boolean expressions, return their or directly. No if needed.

def is_even_or_divisible_by_5(num: int) -> bool:
    return num % 2 == 0 or num % 10 == 5 or num % 10 == 0

A number is divisible by 5 if its last digit is 0 or 5. num % 10 gives the last digit. This works but is more complex than num % 5 == 0 - stick with the direct approach.


Short-circuit evaluation

Python's or is short-circuit evaluated:

num % 2 == 0 or num % 5 == 0

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

01

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.

02

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.

03

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.