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: OddPart 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:
- A number is even if dividing by 2 leaves no remainder —
n % 2 == 0 - If remainder is 0 → even. If remainder is 1 → odd.
Part 2:
- Create two empty lists —
evenandodd - Loop through every number in the list
- Check if the number is even or odd
- Put it in the right list
- Return both lists
Flow
Part 2 flow:
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)) # OddCode Execution — Part 1 Solution 1
Trace through even_or_odd(-3):
| Step | Code | Result |
|---|---|---|
| Input | n = -3 | -3 |
| Modulus | -3 % 2 | 1 |
| Check | 1 == 0 | False |
| 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)) # OddCode Execution — Part 1 Solution 2
How n & 1 works in binary:
| Number | Binary | n & 1 | Result |
|---|---|---|---|
4 | 0100 | 0100 & 0001 = 0 | Even |
7 | 0111 | 0111 & 0001 = 1 | Odd |
0 | 0000 | 0000 & 0001 = 0 | Even |
9 | 1001 | 1001 & 0001 = 1 | Odd |
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]):
| Step | number | number % 2 | even | odd |
|---|---|---|---|---|
| Start | — | — | [] | [] |
| 1st | 1 | 1 → Odd | [] | [1] |
| 2nd | 2 | 0 → Even | [2] | [1] |
| 3rd | 3 | 1 → Odd | [2] | [1, 3] |
| 4th | 4 | 0 → Even | [2, 4] | [1, 3] |
| 5th | 5 | 1 → 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]:
n | n % 2 == 0 | Added to even? |
|---|---|---|
1 | False | No |
2 | True | Yes → [2] |
3 | False | No |
4 | True | Yes → [2, 4] |
5 | False | No |
6 | True | Yes → [2, 4, 6] |
Trace through odd comprehension for [1, 2, 3, 4, 5, 6]:
n | n % 2 != 0 | Added to odd? |
|---|---|---|
1 | True | Yes → [1] |
2 | False | No |
3 | True | Yes → [1, 3] |
4 | False | No |
5 | True | Yes → [1, 3, 5] |
6 | False | No |
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]:
| Step | n | n % 2 == 0 | Which list | even | odd |
|---|---|---|---|---|---|
| Start | — | — | — | [] | [] |
| 1st | 3 | False | odd | [] | [3] |
| 2nd | 4 | True | even | [4] | [3] |
| 3rd | 5 | False | odd | [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?
| Solution | How | Best when |
|---|---|---|
| Part 1 — Solution 1 | n % 2 == 0 | Clear and readable |
| Part 1 — Solution 2 | Bitwise n & 1 | Performance-focused code |
| Part 2 — Solution 1 | Loop + append | Easy to understand |
| Part 2 — Solution 2 | List comprehension | Concise and Pythonic |
| Part 2 — Solution 3 | filter() + lambda | Functional style |
| Part 2 — Solution 4 | Single loop | Efficiency — 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]