S1Q1 · Check Even Number and Second Last Digit is Two¶
⚡ Quick Reference
Function: is_even_and_second_last_digit_is_two(num: int) -> bool
Two conditions, both must be true:
numis even →num % 2 == 0- Second last digit is 2 →
(num // 10) % 10 == 2
def is_even_and_second_last_digit_is_two(num: int) -> bool:
return num % 2 == 0 and (num // 10) % 10 == 2
Key rules:
- Both conditions must hold - use and
- // 10 removes the last digit, % 10 then isolates the new last digit (which was the second last)
- Works correctly for any positive integer including single-digit numbers (second last digit = 0)
Problem Statement¶
Problem
Write a function is_even_and_second_last_digit_is_two(num: int) -> bool that returns True if both of the following are satisfied, and False otherwise:
numis even- The second last digit of
numis2
Examples:
232
False
120
True
25
False
13
False
Understanding the problem¶
There are exactly two things to check. Both must be True for the function to return True.
| Condition | How to check |
|---|---|
| Number is even | num % 2 == 0 |
| Second last digit is 2 | (num // 10) % 10 == 2 |
The second condition is the interesting one. Let's break down how to extract the second last digit.
How to extract the second last digit
Think of it in two steps:
- Step 1 - drop the last digit: integer divide by 10.
120 // 10→12 - Step 2 - isolate the last digit of the result: modulo 10.
12 % 10→2
Combined: (num // 10) % 10
Tracing all four examples¶
num |
Even? | num // 10 |
(num // 10) % 10 |
Second last = 2? | Result |
|---|---|---|---|---|---|
232 |
✅ Yes (232 % 2 = 0) | 23 |
3 |
❌ No | False |
120 |
✅ Yes (120 % 2 = 0) | 12 |
2 |
✅ Yes | True |
25 |
❌ No (25 % 2 = 1) | 2 |
2 |
✅ Yes | False |
13 |
❌ No (13 % 2 = 1) | 1 |
1 |
❌ No | False |
Notice 232 - it is even, and the digit 2 appears in it, but the second last digit is 3, not 2. Both conditions must hold simultaneously.
Notice 25 - the second last digit is 2, but the number is odd. Again, one condition failing is enough to return False.
Solution approaches¶
def is_even_and_second_last_digit_is_two(num: int) -> bool:
is_even = (num % 2 == 0)
second_last = (num // 10) % 10
if is_even and second_last == 2:
return True
else:
return False
Most readable for beginners - each check is named and explicit.
def is_even_and_second_last_digit_is_two(num: int) -> bool:
return num % 2 == 0 and (num // 10) % 10 == 2
Since both conditions are boolean expressions, we can return their and directly. No if needed - the result of and is already True or False.
def is_even_and_second_last_digit_is_two(num: int) -> bool:
s = str(num)
if len(s) < 2:
return False # single-digit: no second last digit
return num % 2 == 0 and s[-2] == '2'
Convert to string and index s[-2] for the second last character. Clean and readable, but needs an explicit length guard for single-digit numbers. The arithmetic approach handles this automatically ((5 // 10) % 10 = 0, which is never 2).
Why and short-circuits¶
Python's and operator is short-circuit evaluated:
If the first condition (num % 2 == 0) is already False, Python skips evaluating the second condition entirely and returns False immediately. This means for odd numbers, the second last digit is never computed - a small efficiency bonus.
Key takeaways¶
Extracting the nth digit
General pattern: (num // 10**n) % 10 gives the digit at position n from the right (0-indexed). Last digit: n=0. Second last: n=1.
Return boolean expressions directly
If a function returns True or False, you rarely need an if. Just return condition_a and condition_b.
and vs or
Use and when all conditions must hold. Use or when any one is enough. Here both must be true → and.