DocsHub
PythonBeginner

Even or Odd

Learn how to check if a number is even or odd in Python, and how to separate even and odd numbers from a list.

Even or Odd

Problem

Two parts to this exercise.

Part 1 — given a number, check if it is even or odd.

Input:  4
Output: Even

Input:  7
Output: Odd

Input:  0
Output: Even

Input:  -3
Output: Odd

Part 2 — given a list of mixed numbers, separate them into two lists — one for even, one for odd.

Input:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output: Even: [2, 4, 6, 8, 10]
        Odd:  [1, 3, 5, 7, 9]

Input:  [11, 22, 33, 44, 55, 66]
Output: Even: [22, 44, 66]
        Odd:  [11, 33, 55]

Logic

Part 1:

  1. A number is even if dividing by 2 leaves no remainder — n % 2 == 0
  2. If remainder is 0 → even. If remainder is 1 → odd.

Part 2:

  1. Create two empty lists — even and odd
  2. Loop through every number in the list
  3. Check if the number is even or odd
  4. Put it in the right list
  5. Return both lists

Flow

Yes No Start n % 2 == 0? Even Odd Done

Part 2 flow:

Yes No Yes No Start even = empty listodd = empty list Take each number number % 2 == 0? Add to even list Add to odd list More numbers? Return even and odd

Part 1 — Solution 1: using if/else

def even_or_odd(n):
    # % is the modulus operator — gives the remainder after division
    # any even number divided by 2 has remainder 0
    # any odd number divided by 2 has remainder 1
    if n % 2 == 0:
        return "Even"
    else:
        return "Odd"


print(even_or_odd(4))    # Even
print(even_or_odd(7))    # Odd
print(even_or_odd(0))    # Even
print(even_or_odd(-3))   # Odd

Code Execution — Part 1 Solution 1

Trace through even_or_odd(-3):

StepCodeResult
Inputn = -3-3
Modulus-3 % 21
Check1 == 0False
Return"Odd"

In Python, the modulus of a negative number follows the sign of the divisor. -3 % 2 = 1 because Python computes it as -3 = (-2) × 2 + 1. So the even/odd check still works correctly for negative numbers.


Part 1 — Solution 2: using bitwise AND

The last bit of a number tells you if it is odd or even. If the last bit is 1 — odd. If 0 — even. n & 1 checks the last bit.

def even_or_odd(n):
    # bitwise AND with 1 checks the last bit of the number
    # even numbers end in 0 in binary — n & 1 = 0
    # odd numbers end in 1 in binary  — n & 1 = 1
    if n & 1:
        return "Odd"
    else:
        return "Even"


print(even_or_odd(4))    # Even
print(even_or_odd(7))    # Odd
print(even_or_odd(0))    # Even
print(even_or_odd(-3))   # Odd

Code Execution — Part 1 Solution 2

How n & 1 works in binary:

NumberBinaryn & 1Result
401000100 & 0001 = 0Even
701110111 & 0001 = 1Odd
000000000 & 0001 = 0Even
910011001 & 0001 = 1Odd

Every even number ends in 0 in binary. Every odd number ends in 1. & 1 isolates that last bit.


Part 2 — Solution 1: using a loop

Loop through the list and put each number into the right list.

def separate_even_odd(numbers):
    even = []   # will hold all even numbers
    odd = []    # will hold all odd numbers

    for number in numbers:
        if number % 2 == 0:
            even.append(number)   # even — goes to even list
        else:
            odd.append(number)    # odd — goes to odd list

    return even, odd


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even, odd = separate_even_odd(numbers)

print(f"Even: {even}")   # Even: [2, 4, 6, 8, 10]
print(f"Odd:  {odd}")    # Odd:  [1, 3, 5, 7, 9]

Code Execution — Part 2 Solution 1

Trace through separate_even_odd([1, 2, 3, 4, 5]):

Stepnumbernumber % 2evenodd
Start[][]
1st11 → Odd[][1]
2nd20 → Even[2][1]
3rd31 → Odd[2][1, 3]
4th40 → Even[2, 4][1, 3]
5th51 → Odd[2, 4][1, 3, 5]
Done[2, 4][1, 3, 5]

Part 2 — Solution 2: using list comprehension

Build both lists in one line each using list comprehensions.

def separate_even_odd(numbers):
    # keep only numbers where number % 2 == 0
    even = [n for n in numbers if n % 2 == 0]

    # keep only numbers where number % 2 != 0
    odd  = [n for n in numbers if n % 2 != 0]

    return even, odd


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even, odd = separate_even_odd(numbers)

print(f"Even: {even}")   # Even: [2, 4, 6, 8, 10]
print(f"Odd:  {odd}")    # Odd:  [1, 3, 5, 7, 9]

Code Execution — Part 2 Solution 2

Trace through even comprehension for [1, 2, 3, 4, 5, 6]:

nn % 2 == 0Added to even?
1FalseNo
2TrueYes → [2]
3FalseNo
4TrueYes → [2, 4]
5FalseNo
6TrueYes → [2, 4, 6]

Trace through odd comprehension for [1, 2, 3, 4, 5, 6]:

nn % 2 != 0Added to odd?
1TrueYes → [1]
2FalseNo
3TrueYes → [1, 3]
4FalseNo
5TrueYes → [1, 3, 5]
6FalseNo

Part 2 — Solution 3: using filter()

Use Python's built-in filter() function with a lambda.

def separate_even_odd(numbers):
    # filter keeps only items where the function returns True
    even = list(filter(lambda n: n % 2 == 0, numbers))
    odd  = list(filter(lambda n: n % 2 != 0, numbers))

    return even, odd


numbers = [11, 22, 33, 44, 55, 66]
even, odd = separate_even_odd(numbers)

print(f"Even: {even}")   # Even: [22, 44, 66]
print(f"Odd:  {odd}")    # Odd:  [11, 33, 55]

Part 2 — Solution 4: using a single loop with tuple unpacking

Loop once and separate into two lists in one pass — more efficient than two comprehensions since the list is only scanned once.

def separate_even_odd(numbers):
    even, odd = [], []

    for n in numbers:
        # conditional expression — picks which list to append to
        (even if n % 2 == 0 else odd).append(n)

    return even, odd


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even, odd = separate_even_odd(numbers)

print(f"Even: {even}")   # Even: [2, 4, 6, 8, 10]
print(f"Odd:  {odd}")    # Odd:  [1, 3, 5, 7, 9]

Code Execution — Part 2 Solution 4

Trace through [3, 4, 5]:

Stepnn % 2 == 0Which listevenodd
Start[][]
1st3Falseodd[][3]
2nd4Trueeven[4][3]
3rd5Falseodd[4][3, 5]

The expression (even if n % 2 == 0 else odd) returns the list object itself — then .append(n) adds to it.

Solution 4 scans the list once. Solutions 2 and 3 scan the list twice — once for even, once for odd. For large lists, one pass is more efficient.


Which solution to use?

SolutionHowBest when
Part 1 — Solution 1n % 2 == 0Clear and readable
Part 1 — Solution 2Bitwise n & 1Performance-focused code
Part 2 — Solution 1Loop + appendEasy to understand
Part 2 — Solution 2List comprehensionConcise and Pythonic
Part 2 — Solution 3filter() + lambdaFunctional style
Part 2 — Solution 4Single loopEfficiency — one pass only

Output

Even
Odd
Even
Odd

Even: [2, 4, 6, 8, 10]
Odd:  [1, 3, 5, 7, 9]

Even: [22, 44, 66]
Odd:  [11, 33, 55]

On this page