Sub topic 1
What Makes an Integer Even or Odd
Prerequisites
This chapter assumes you know what an integer is, can add, subtract, and multiply negative numbers, and understand division with remainders. Familiarity with the % operator in Python is helpful for the code examples.
What Makes an Integer Even or Odd
An integer is even if it is divisible by 2 with no remainder. It is odd if dividing by 2 leaves a remainder of 1. Zero is even, and negative integers follow the same rule. For example, -4 is even because -4 = 2 × (-2), and -7 is odd because -7 = 2 × (-4) + 1.
Even and Odd in Sums and Differences
Parity describes whether a number is even or odd. For sums and differences, the rule is:
- Even + even = even
- Odd + odd = even
- Even + odd = odd
- Even − odd = odd
The same rules hold for subtraction, because subtracting an integer changes its parity exactly as adding it does.
Even and Odd in Products
A product is even whenever at least one factor is even. A product is odd only when every factor is odd. You can check all four combinations of parity with a short script:
for a in (0, 1):
for b in (0, 1):
print(a, b, (a + b) % 2, (a * b) % 2)The output shows that the sum is odd only when exactly one addend is odd, and the product is odd only when both factors are odd.
> Note: This is a callout, not a quote.
Parity as a Remainder of 2
Every integer leaves a remainder of 0 or 1 when divided by 2. So parity is simply the remainder of n % 2. This view makes the rules above easy to verify. Python's % operator returns a non-negative remainder for positive divisors, so -7 % 2 gives 1, which confirms that -7 is odd.
Exercise
If x is odd and y is even, is x × y odd or even? Is x + y odd or even?
Show answer
The product x × y is even, because one factor is even. The sum x + y is odd, because odd + even = odd.