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.
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.
The syntax to check if a number is even or odd in Python is:
if number % 2 == 0:
print("Even")
else:
print("Odd")
We can use the modulo operator to check if a number is even or odd.
For example,
number
and assign it a value.if
statement to check if the number modulo 2 equals zero. If true, print "Even".else
statement to handle the case where the number is not divisible by 2 and print "Odd".number = 7
# Check if the number is even or odd
if number % 2 == 0:
print("Even")
else:
print("Odd")
Odd