Even / Odd Checker
Learn how to check whether a number is even or odd using Python operators, conditions, input and output.
1. Project Introduction
An Even / Odd Checker is one of the simplest Python projects for beginners.
The program takes a number from the user and checks whether the number is divisible by 2.
2. Taking User Input
First, we need to ask the user for a number.
number = int(input("Enter a number: "))
The int() function converts the input into an integer.
3. Understanding the Modulus Operator
The modulus operator is written as %.
It returns the remainder after division.
10 % 2 # Result: 0 7 % 2 # Result: 1
If a number divided by 2 gives a remainder of 0, the number is even.
4. Checking an Even Number
We can use an if statement to check whether the remainder is zero.
if number % 2 == 0:
print("Even number")
Here == checks whether two values are equal.
5. Checking an Odd Number
If the remainder is not zero, the number is odd.
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
6. Complete Basic Logic
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
This is the complete basic logic of the project.
7. Handling Negative Numbers
The same logic also works with negative numbers.
-8 % 2 # Result: 0 -7 % 2 # Result: 1
Therefore, the program can correctly identify negative even and odd numbers as well.
8. Using a Function
We can make the program reusable by placing the checking logic inside a function.
def check_even_odd(number):
if number % 2 == 0:
return "Even"
return "Odd"
9. Calling the Function
number = int(input("Enter a number: "))
result = check_even_odd(number)
print("The number is:", result)
10. Adding Input Validation
We should also handle invalid input. For example, the user may enter text instead of a number.
try:
number = int(input("Enter a number: "))
except ValueError:
print("Please enter a valid number.")
The try / except structure prevents the program from crashing when invalid input is entered.
11. Project Flow
12. Concepts You Practiced
- Python Variables
- input()
- int()
- Modulus Operator %
- Comparison Operators
- if / else
- Functions
- return
- try / except
- Basic Error Handling
13. Run Complete Project 🚀
You have learned how an Even / Odd Checker works. Now open the complete working project.
Run Complete Project