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

PythonServer Side ProgrammingProgramming

When it is required to check if a number is positive, negative or 0, a simple ‘if’ condition can be used.

Below is a demonstration for the same −

Example

 Live Demo

my_num = 58
if my_num >= 0:
   if my_num == 0:
      print("The number is equal to zero")
   else:
      print("It is a positive number")
else:
   print("It is a negative number")

Output

It is a positive number

Explanation

  • A number is defined.
  • It is checked to see if it is 0, otherwise if it is greater than 0, it is shown as positive number.
  • If both these conditions don’t hold good, it is determined to be a negative number.
  • It is then displayed as output on the console.
raja
Published on 11-Mar-2021 17:12:22
Advertisements