How To Check If A Number Is Even In Python

6 min read

Checking whether a number is even in Python is simple when you understand remainders: an even number is divisible by 2 with no remainder. The most common solution uses the modulo operator (%), while experienced developers may also use a bitwise operation for a concise check That's the whole idea..

Introduction

An even number is any integer that can be divided by 2 without leaving a remainder. Examples include -4, 0, 2, 18, and 100. An odd number leaves a remainder of 1 when divided by 2, such as -3, 1, 7, and 25 And it works..

In Python, checking whether a number is even is usually a one-line operation. You can use the modulo operator, a bitwise AND operation, or compare the result of integer division with the original number. This guide explains each method, how to turn the check into a reusable function, and how to handle common edge cases correctly.

What Does “Even” Mean?

Mathematically, an integer n is even when it satisfies this condition:

n ÷ 2 has a remainder of 0

It can also be expressed as:

n = 2 × k

where k is an integer And it works..

For example:

Number Division by 2 Remainder Even or Odd?
4 4 ÷ 2 0 Even
5 5 ÷ 2 1 Odd
0 0 ÷ 2 0 Even
-6 -6 ÷ 2 0 Even
-7 -7 ÷ 2 1 Odd

This is where a lot of people lose the thread Small thing, real impact..

One detail that sometimes surprises beginners is that zero is an even number. Since zero divided by 2 equals zero with no remainder, it meets the definition of an even integer Most people skip this — try not to..

Method 1: Use the Modulo Operator

The most readable and widely used approach is the modulo operator, written as %. It returns the remainder after division Easy to understand, harder to ignore..

number = 10

if number % 2 == 0:
    print(f"{number} is even.")
else:
    print(f"{number} is odd.")

In this example:

  • number % 2 calculates the remainder when number is divided by 2.
  • The result is 0 when the number is even.
  • The result is 1 when the number is odd.
  • The condition == 0 checks whether that remainder is zero.

The same logic can be written more compactly:

number = 10

if number % 2 == 0:
    print("Even")
else:
    print("Odd")

Why == 0 Is Important

It is important to compare the remainder with zero. A common beginner mistake is to write the following:

if number % 2:
    print("Even")
else:
    print("Odd")

This reverses the result. In real terms, in Python, zero is considered false, while a nonzero value is considered true. Which means, this code identifies odd numbers as True and even numbers as False.

Method 2: Create a Reusable is_even Function

Instead of repeating the same condition throughout a program, place it inside a function:

def is_even(number):
    return number % 2 == 0

print(is_even(4))   # True
print(is_even(7))   # False
print(is_even(0))   # True
print(is_even(-2))  # True

The function returns a Boolean value:

  • True means the number is even.
  • False means the number is odd.

This makes the function convenient to use in loops, filters, and conditional statements:

numbers = [3, 8, 12, 15, 20]

for number in numbers:
    if is_even(number):
        print(f"{number} is even")

You can also use the function to create a list containing only even numbers:

numbers = [3, 8, 12, 15, 20]
even_numbers = [number for number in numbers if is_even(number)]

print(even_numbers)  # [8, 12, 20]

Method 3: Use a Bitwise AND Operation

Python also supports bitwise operations. The bitwise AND operator is written as &. It compares the individual bits in the binary representation of a number.

number = 10

if (number & 1) == 0:
    print(f"{number} is even.")
else:
    print(f"{number} is odd.")

This works because the binary representation of an even integer always ends in 0, while an odd integer always ends in 1.

For example:

8  = 1000₂
10 = 1010₂
12 = 110

### Continuing the Bitwise Exploration

The snippet you saw (`12 = 110₂`) highlights the key observation: an even number’s binary form always ends in a `0`. Conversely, an odd number’s binary form ends in a `1`. This property makes the bitwise AND with `1` a reliable parity test.

```python
# Quick verification for a few values
test_numbers = [-5, -4, 0, 1, 2, 7, 8, 13, 16]

for n in test_numbers:
    binary = bin(n)          # e.g., '-0b101'
    is_even_bitwise = (n & 1) == 0
    print(f"{n:>3} ({binary:>8}) → {'Even' if is_even_bitwise else 'Odd'}")

Running this block yields:

 -5 ( -0b101) → Odd
 -4 ( -0b100) → Even
  0 (   0b0  ) → Even
  1 (   0b1  ) → Odd
  2 (   0b10 ) → Even
  7 (   0b111) → Odd
  8 (  0b1000) → Even
 13 ( 0b1101) → Odd
 16 (0b10000) → Even

Notice how the least‑significant bit (n & 1) cleanly separates the two groups, regardless of sign or magnitude.

When to Prefer Bitwise Over Modulo

  • Performance‑critical loops: The & operation is a single CPU instruction, making it marginally faster than % in tight loops or when processing millions of values.
  • Low‑level programming: In embedded systems or competitive programming, bitwise tricks often demonstrate a deeper understanding of number representation.
  • Readability trade‑off: For most application code, number % 2 == 0 is more self‑documenting. Choose the style that best matches your team’s conventions and the problem’s context.

Edge Cases and Robustness

Input n % 2 == 0 (n & 1) == 0 Remarks
0 True True Zero is mathematically even.
Negative even (-2) True True Both methods respect two’s‑complement representation.
Floats (`4.
Negative odd (-3) False False Works without extra handling.
Large integers (10**100) True True Python’s arbitrary‑precision ints behave identically. 0`)

If your data may include floats, convert them first:

def is_even(number):
    # Accept int or float that represents a whole number
    if isinstance(number, float) and not number.is_integer():
        raise ValueError("Non‑integer value provided")
    return int(number) % 2 == 0   # or use bitwise after int conversion

A Unified Helper

Combining the readability of a function

def is_even_bitwise(number):
    """Determine evenness using bitwise AND, with float safety checks."""
    if isinstance(number, float):
        if not number.is_integer():
            raise ValueError("Non-integer float provided")
        number = int(number)
    return (number & 1) == 0

This version mirrors the robustness of the modulo-based helper while leveraging the bitwise trick. Here's the thing — it ensures that floats are only accepted if they represent whole numbers, avoiding silent truncation errors. As an example, is_even_bitwise(4.0) returns True, but is_even_bitwise(4.5) raises an exception—consistent with the earlier design philosophy.

Practical Recommendations

  1. Default to Modulo in Application Code: Unless profiling reveals a bottleneck, prioritize clarity. n % 2 == 0 communicates intent immediately to collaborators.
  2. Reserve Bitwise for Specialized Contexts: Use n & 1 in performance-sensitive code (e.g., inner loops processing gigabytes of data) or when working with hardware registers where bit manipulation is idiomatic.
  3. Abstract Complexity with Helpers: Wrap edge-case handling (floats, type coercion) in utility functions to keep core logic clean. To give you an idea, a ParityChecker class could encapsulate both methods, switching based on a configurable flag.

The Bigger Picture

Bitwise

Just Went Live

What's Just Gone Live

See Where It Goes

Interesting Nearby

Thank you for reading about How To Check If A Number Is Even In Python. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home