Use the modulo operator (%) to test a number’s remainder when divided by 2. A remainder of 0 means even; otherwise, it’s odd. Below are practical options for single numbers and collections, plus notes on edge cases.

Method 1: Use the modulo operator (%)

Why this works: Parity is defined by divisibility by 2. n % 2 evaluates to 0 for even integers and 1 for odd integers.

Step 1: Define a function that returns "Even" or "Odd" using the remainder.

def check_odd_even(n: int) -> str:
    return "Even" if n % 2 == 0 else "Odd"

Step 2: Call the function with sample values to verify behavior.

print(check_odd_even(2))    # Even
print(check_odd_even(7))    # Odd
print(check_odd_even(0))    # Even
print(check_odd_even(-3))   # Odd

Step 3: Accept user input and convert it to an integer before checking.

num = int(input("Enter an integer: "))
print(check_odd_even(num))

Tip: Zero is even. Negative integers follow the same parity rules as positive integers.


Method 2: Use bitwise AND with 1

Why this works: The least significant bit (LSB) of an integer is 1 for odd numbers and 0 for even numbers. You can read that bit using bitwise AND (&).

Step 1: Test the least significant bit to determine parity.

def is_even(n: int) -> bool:
    return (n & 1) == 0

print("Even" if is_even(24) else "Odd")  # Even
print("Even" if is_even(7) else "Odd")   # Odd

Note: Bitwise checks are for integers. If you receive floats, convert with int() only when it makes sense to truncate, or validate input type first.


Method 3: Check parity across a list or tuple

Why this helps: When scanning a collection, short-circuit evaluation avoids unnecessary work. The built-in any() returns as soon as a True value is found.

Step 1: Detect if any value is odd using a generator expression.

nums = [1, 2, 3, 4, 5]
has_odd = any(n % 2 == 1 for n in nums)
print(has_odd)  # True

Step 2: Check if all values are even using all().

all_even = all(n % 2 == 0 for n in nums)
print(all_even)  # False

Step 3: Label each element as Odd/Even. Using map returns an iterator, which can be memory-friendly for large inputs.

labels = map(lambda n: f"{n} {'Even' if n % 2 == 0 else 'Odd'}", nums)
print("\n".join(labels))
# 1 Odd
# 2 Even
# 3 Odd
# 4 Even
# 5 Odd

Common pitfalls and tips:

  • Parity applies to integers. Decide how to handle non-integers (reject or convert) before checking.
  • For simple readability, prefer n % 2. For low-level parity checks on integers, n & 1 is also valid.
  • Use generator expressions with any() or all() to short-circuit scans over large iterables.

That’s all you need to quickly determine parity in Python—stick with % 2 for clarity, or use & 1 for a bit-level check, and lean on any()/all() to scan collections efficiently.