In this tutorial, we will learn how to check if a number is even or odd in Python. We will cover the basic conditional statements and the modulo operator to determine the parity of a number.

What is Even and Odd Number Checking

Even and odd number checking is the process of determining whether a number is divisible by 2. An even number is completely divisible by 2, whereas an odd number leaves a remainder of 1 when divided by 2.

Syntax

The syntax to check if a number is even or odd in Python is:

if number % 2 == 0:
print("Even")
else:
print("Odd")

Checking if a number is even or odd

We can use the modulo operator to check if a number is even or odd.

For example,

Declare a variable number and assign it a value.

Use an if statement to check if the number modulo 2 equals zero. If true, print "Even".

Use an else statement to handle the case where the number is not divisible by 2 and print "Odd".

Print the result based on the condition met.

Python Program

number = 7
# Check if the number is even or odd
if number % 2 == 0:
print("Even")
else:
print("Odd")