- Pairs
- 4 pairs
Check Even Number in Python
What you’ll learn
- What an even number means, including why zero is even.
- How to check evenness with
n % 2 == 0. - How to print all even numbers in a range.
Overview
Even numbers are exactly those divisible by 2 without remainder. In Python, the easiest test is number % 2 == 0.
Two programs
Single number and range check.
Live preview
Quick parity check in browser.
Important note
Negative numbers also follow the same even rule.
Prerequisites
Modulo operator, if statements, and loops.
- Know that
n % 2gives 0 or 1 for nonnegative integers. - Basic function creation with
def.
What is an even number?
An integer is even if it can be split into pairs with nothing left over. Mathematically: n = 2 * k for some integer k.
Examples: 10, 0, and -4 are even. 11 is odd.
Modulo 2 view
Even numbers are congruent to 0 modulo 2. Odd numbers are congruent to 1 modulo 2.
1010 % 2 = 0, so 10 is even.
Intuition
- Remainder
7 % 2 = 1
Takeaway: parity tells whether a number can be split into complete pairs.
Live preview
Works with positive and negative integers in safe range.
Algorithm
Goal: return True only when number is divisible by 2.
Compute remainder
Find n % 2.
Check zero
If remainder is 0, number is even.
Use in loop
Apply same check for each value in a range.
📜 Pseudocode
function is_even(n):
return (n mod 2) == 0
function print_evens(start, end):
for i from start to end:
if is_even(i):
output i Check one number with modulo
Simple helper function for single value parity checking.
def is_even(number: int) -> bool:
return number % 2 == 0
number = 10
if is_even(number):
print(f"{number} is an even number.")
else:
print(f"{number} is not an even number.") Explanation
The function returns True when remainder by 2 is exactly zero.
Print evens in range [1, 10]
Reuses the same helper and prints only even values.
def is_even(num: int) -> bool:
return num % 2 == 0
def print_evens_in_range(start: int, end: int) -> None:
print(f"Even numbers in the range {start} to {end}:")
for i in range(start, end + 1):
if is_even(i):
print(i, end=" ")
print_evens_in_range(1, 10) Explanation
Inclusive range uses end + 1 in Python.
Optimization
Step by two. Start from first even value and increment by 2 when printing ranges.
Bitwise option. (n & 1) == 0 is a common parity shortcut.
Interview: explain modulo first, then mention bitwise.
❓ FAQ
🔄 Input / output examples
Test these values in the first example.
| n | Even? |
|---|---|
0 | Yes |
10 | Yes |
11 | No |
-4 | Yes |
Edge cases and pitfalls
The formula works for all integers, including negatives and zero.
n = 0
Even by definition.
Still valid
-4 % 2 == 0, so -4 is even.
Inclusive end
Remember range(start, end + 1).
Validate types
Convert user input to integer safely before checking.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
| Single check | O(1) | O(1) |
Range scan [a, b] | O(b - a + 1) | O(1) |
Summary
- Use
n % 2 == 0to test evenness. - Zero is even, and negatives can be even too.
- For ranges, reuse the same helper function.
Zero is an even number because 0 = 2 * 0. In Python, 0 % 2 == 0 is True.
8 people found this page helpful
