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.
def 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}") 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).
More Examples
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.