Check for Positive, Negative or Zero Numbers

In this Python lesson, we will practice how to check whether the number entered by the user is positive, negative or zero using decision-making statements.

Example 1: Check the State of a Number Using if…elif…else

# Request input from user
num = float(input('Enter any number: '))
if num > 0:
   print('This is a Positive number')
elif num == 0:
   print('This is Zero')
else:
   print('This is a Negative number')

Output

Enter any number: 9

This is a Positive number

In the above program, we have used the if…elif…else statement to check if a number is either positive, negative or a zero value. We can also do the same using nested if statements. See the practice below.

Example 2: Check the State of a Number Using Nested if

# Request input from user
num = float(input('Enter any number: '))
if num >= 0:
   if num == 0:
       print('This is Zero')
   else:
       print('This is Positive number')
else:
   print('This is Negative number')

Output

Enter any number: -7

This is Negative number

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials