Prev - #2 Temperature Conversion | Table of Contents | Next - #4 Area & Volume
isOdd(13) →
True
isEven(13) → False
Determining if a number is even or odd is a common calculation that uses the modulo operator. Like Exercise #2, “Temperature Conversion,” the functions for this exercise’s solution functions can be as little as one line long.
This exercise covers the % modulo operator, and the technique of using modulo-2 arithmetic to determine if a number is even or odd.
Exercise Description
Write two functions, isOdd()
and isEven(), with a single numeric parameter named number. The isOdd()
function
returns True
if number
is
odd and False
if number
is even. The isEven()
function returns the True if number
is even and False if number
is odd. Both
functions return False
for numbers with fractional
parts, such as 3.14
or -4.5
.
Zero is considered an even number.
These Python assert
statements stop
the program if their condition is False
. Copy them
to the bottom of your solution program. Your solution is correct if the following
assert
statements’ conditions are all True:
assert isOdd(42) == False
assert isOdd(9999) == True
assert isOdd(-10) == False
assert isOdd(-11) == True
assert isOdd(3.1415) == False
assert isEven(42) == True
assert isEven(9999) == False
assert isEven(-10) == True
assert isEven(-11) == False
assert isEven(3.1415) == False
Try to write a solution based on the information in this description. If you still have trouble solving this exercise, read the Solution Design and Special Cases and Gotchas sections for additional hints.
Prerequisite concepts: modulo operator
Solution Design
You might not know how to write code that finds out if a number is odd or even. This kind of exercise is easy to solve if you already know how to solve it, but difficult if you have to reinvent the solution yourself. Feel free to look up answers to questions you have on the internet. Include the programming language’s name for more specific results, such as “python find out if a number is odd or even”. The question-and-answer website https://stackoverflow.com is usually at the top of the search results and reliably has direct, high-quality answers. Looking up programming answers online isn’t “cheating.” Professional software developers look up answers dozens of times a day!
The %
modulo operator can mod a number
by 2
to determine its evenness or oddness. The modulo
operator acts as a sort of “division remainder” operator. If you divide a
number by 2
and the remainder is 1, the number must be odd. If the remainder is 0, the number must be even. For example, 42 % 2 is 0
, meaning that 42 is even. But 7 % 2
is 1, meaning that 7
is odd.
Floating-point numbers such as 3.1415
are neither odd nor even, so both isOdd()
and isEven() should return False
for them.
Keep in mind that the isOdd()
and isEven() function you write must return a Boolean True or False
value, not an
integer 0
or 1
. You need
to use a ==
or !=
comparison
operator to produce a Boolean value: 7 % 2 == 1
evaluates to 1 == 1
, which in turn evaluates to True.
Special Cases and Gotchas
Non-integer numbers such as 4.5
or 3.1415 are neither odd nor even, so both isOdd() and isEven()
should
return False
for them.
Now try to write a solution based on the information in the previous sections. If you still have trouble solving this exercise, read the Solution Template section for additional hints.
Solution Template
Try to first write a solution from scratch. But if you have difficulty, you can use the following partial program as a starting place. Copy the following code from https://invpy.com/oddeven-template.py and paste it into your code editor. Replace the underscores with code to make a working program:
def isOdd(number):
# Return whether number mod 2 is 1:
return ____ % 2 == ____
def isEven(number):
# Return whether number mod 2 is 0:
return ____ % 2 == ____
The complete solution for this exercise is given in Appendix A and https://invpy.com/oddeven.py. You can view each step of this program as it runs under a debugger at https://invpy.com/oddeven-debug/.
Further Reading
There are several uses of the %
modulo
operator. You can learn more about them in the tutorial “Python Modulo in
Practice: How to Use the % Operator” at https://realpython.com/python-modulo-operator/.
Prev - #2 Temperature Conversion | Table of Contents | Next - #4 Area & Volume