Python Even or Odd Number Checker
Determine if an integer is even or odd using the modulo arithmetic operator in Python.
How it Works
An even number is an integer that is exactly divisible by 2. An odd number leaves a remainder of 1 when divided by 2.
We use the Python modulo operator `%`, which returns the remainder of a division. For any even number, `n % 2` evaluates to `0`.
This classic computer science concept is widely used in looping, partitioning data, and basic algorithm design.
Source Code
Function to evaluate odd/even status for negative, positive, and zero integers.
even_odd.py
Try in Editordef is_even(num):
return num % 2 == 0
numbers = [4, 7, 0, -3]
for n in numbers:
result = "Even" if is_even(n) else "Odd"
print(f"{n:2d} is {result}")Terminal Output
4 is Even
7 is Odd
0 is Even
-3 is OddReal-world Applications
- Alternating row colors in tables/UIs
- Distributing tasks across multiple worker processes
- Mathematical number theory puzzles
Frequently Asked Questions
Is 0 even or odd?
Zero is even because it leaves a remainder of 0 when divided by 2 (0 = 2 * 0).