Python Program to Check if a Number is Positive, Negative or 0

To understand this example, you should have the knowledge of the following Python programming topics:


Source Code: Using if...elif...else

num = float(input("Enter a number: "))
if num > 0:
   print("Positive number")
elif num == 0:
   print("Zero")
else:
   print("Negative number")

Here, we have used the if...elif...else statement. We can do the same thing using nested if statements as follows.

Source Code: Using Nested if

num = float(input("Enter a number: "))
if num >= 0:
   if num == 0:
       print("Zero")
   else:
       print("Positive number")
else:
   print("Negative number")

The output of both programs will be the same.

Output 1

Enter a number: 2
Positive number

Output 2

Enter a number: 0
Zero

A number is positive if it is greater than zero. We check this in the expression of if. If it is False, the number will either be zero or negative. This is also tested in subsequent expression.


Also Read:

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to return 'Up' if the input number is positive, 'Down' if it's negative, and 'Zero' if it's zero.

  • For example, for input 5, the output should be 'Up'.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges