Python Program to Check if Number is Zero

Python – Check if Number is Zero

To check if a number n is zero in Python, you can use Equal-to == comparison operator and check if the number is zero. Pass the variable n and number zero as operands to the Equal-to comparison operator. The expression n == 0 returns True is the value in n is zero, of False if the value in n is not zero.

The following is an example Python program, where we will take a number in variable n and check if it is zero or not. We will use an if-else statement, and the expression n == 0 as the if-condition.

Python Program

n = 0

if (n == 0):
    print('n is zero.')
else:
    print('n is not zero.')
Run Code Copy

For n = 0, the condition n == 0 evaluates to True, and the if-block executes.

Output

n is zero.

Let us take non-zero value in n and rerun the program.

Python Program

n = 7

if (n == 0):
    print('n is zero.')
else:
    print('n is not zero.')
Run Code Copy

For n = 7, the condition n == 0 evaluates to False, and the else-block executes.

Output

n is not zero.

Summary

In this tutorial, we have written a Python program to check if a given number is zero or not using Equal-to == comparison operator and if-else conditional statement.

Code copied to clipboard successfully 👍