Even or Odd in Python
Modulo reveals whether division by two leaves a remainder.
What is Even or Odd?
Modulo reveals whether division by two leaves a remainder.
Determine whether an integer is even or odd.
When should you use it?
- Learn Python syntax through a practical example.
- Build a foundation for larger programming problems.
- Test an idea quickly without a local setup.
Example code
main.py
number = 42
# Even numbers leave no remainder when divided by 2.
kind = "even" if number % 2 == 0 else "odd"
print(f"{number} is {kind}")
Expected output
42 is even
How it works
A remainder of zero means the number is divisible by two.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.