4.7: Telling If a Number is Even or Odd
- Page ID
- 14440
If the product of the board width and height is divided by two and has a remainder of 0 (the % modulus operator evaluates what the remainder is) then the number is even. Even numbers divided by two will always have a remainder of zero. Odd numbers divided by two will always have a remainder of one. This is a good trick to remember if you need your code to tell if a number is even or odd:
>>> isEven = someNumber % 2 == 0 >>> isOdd = someNumber % 2 != 0
In the above case, if the integer in someNumber
was even, then isEven
will be True
. If it was odd, then isOdd
will be True
.