Python Tutorial (33) – Example: Determine whether an odd number is even

Time: Column:Python views:261

Check if a Number is Odd or Even

The following example demonstrates how to check whether a number is odd or even in Python.

Example (Python 3.0+)

# Python program to check if a number is odd or even
# An even number gives a remainder of 0 when divided by 2
# If the remainder is 1, the number is odd

num = int(input("Enter a number: "))
if (num % 2) == 0:
   print("{0} is an even number".format(num))
else:
   print("{0} is an odd number".format(num))

Output when run:

Enter a number: 3
3 is an odd number

In this example, the program takes a user input, converts it into an integer, and then uses the modulus operator (%) to check if the number is divisible by 2. If the remainder is 0, the number is even; otherwise, it’s odd.


Using Nested if Statements

You can also implement this using nested if statements for more complex logic, though it’s not necessary in this case.