Skip to content

S1Q1 · Check If Even Two-Digit Number

⚡ Quick Reference

Function: is_even_two_digit_number(num: int) -> bool

Core idea: check two conditions - exactly two digits (ignoring sign) AND even.

def is_even_two_digit_number(num):
    return 10 <= abs(num) <= 99 and num % 2 == 0

Key rules: - Two-digit: 10 ≤ abs(num) ≤ 99 - Even: num % 2 == 0 - Negative numbers like -36 count as two-digit (ignore the sign) - Both conditions must hold


Problem Statement

Problem

Write a function is_even_two_digit_number(num) that returns True if num is both a two-digit number (ignoring sign) and even.

Examples:

Input
24
Output
True
Input
-36
Output
True
Input
5
Output
False
Input
101
Output
False
Input
-19
Output
False

Tracing all examples

num abs(num) Two-digit? Even? Result
24 24 10≤24≤99 ✅ 24%2=0 ✅ True
-36 36 10≤36≤99 ✅ -36%2=0 ✅ True
5 5 5<10 ❌ - False
101 101 101>99 ❌ - False
-19 19 10≤19≤99 ✅ -19%2≠0 ❌ False
42 42 True
99 99 99%2≠0 ❌ False
-48 48 True

Solution approaches

def is_even_two_digit_number(num):
    return 10 <= abs(num) <= 99 and num % 2 == 0

Chained comparison 10 <= abs(num) <= 99 checks both bounds at once. Short-circuit and skips the even check if the number isn't two digits.

def is_even_two_digit_number(num):
    is_two_digit = 10 <= abs(num) <= 99
    is_even      = num % 2 == 0
    return is_two_digit and is_even
def is_even_two_digit_number(num):
    is_two_digit = len(str(abs(num))) == 2
    is_even      = num % 2 == 0
    return is_two_digit and is_even

Converts to string and checks length - useful when the digit-count approach feels more intuitive.

is_even_two_digit_number = lambda num: 10 <= abs(num) <= 99 and num % 2 == 0

Key takeaways

01

abs() handles the sign

abs(num) strips the negative sign before checking the digit range. -36 has two digits (ignoring the sign) just like 36 - abs(-36) = 36 which is in range.

02

10 ≤ x ≤ 99 for two-digit numbers

Two-digit numbers range from 10 to 99 inclusive. Python's chained comparison 10 <= x <= 99 is cleaner than x >= 10 and x <= 99.

03

Check two-digit before even

The two-digit check uses abs(num); the even check uses num. Keep them separate - don't accidentally check abs(num) % 2 for evenness, which would also make -19 appear even.