Python Program to Check if A Number is Positive, Negative, or Zero

In this Python example, we will discuss how can we check if any given number is positive, negative, or zero with explanation and implementation. Let’s get started.

1. What is Number and When is it Positive or Negative?

number is a mathematical object used to count, measure, and label. 

– Wikipedia

In simple words, we can say that number can be an arithmetical value, which can be expressed using different ways like word, symbol, or figure, representing a particular quantity and used for counting and can make the computation possible.

When any given number is greater than zero (0) then the number is said to be a positive number and when any given number is lesser than zero(0) then the number is said to be a negative number.

Example:

Input :  4
Output : The number is positive

Input : -5 
Output : The number is negative

Some of the topics which will be helpful for understanding the program implementation better are:


2. Program to Check if A Number is Positive, Negative, or Zero

As discussed above, when the number is greater than zero then it is positive, and negative when it is lesser than zero and when it is equal to zero then the number is zero.

Hence, for the program implementation, we will use the if…else… condition along with the relational operator.

Let’s implement the code and see how it works.

#Python Program to Check is Number is >0 or <0 or =0
num = float(input("Enter any Number: "))
if num > 0:
  print("The given number is Positive number")
elif num == 0:
  print("The given number is Zero")
else:
  print("The given number is Negative number")
Output
Enter any Number: 56
The given number is Positive number

In the above program, the given input is greater than zero so the output for the positive number was displayed.


3. Check if Any Number is Positive, Negative, or Zero using Exception Handling

Now, in the above program, we have covered the most basic way of implementation.

We can use the exception handling method if the user provides any other input rather than float value. Let’s use the exception handling method for the same problem and find out the working.

# Python Program to Check is Number is >0 or <0 or =0 uisng Exception Handling
try:
  num = float(input("Enter any Number: "))
  if num > 0:
    print("The given number is Positive number")
  elif num == 0:
    print("The given number is Zero")
  elif num < 0:
    print("The given number is Negative number")

except ValueError:
  print("Sorry, Your Input is Invalid")
Output
Enter any Number: 67er
Sorry, Your Input is Invalid

In the program, we have taken the input inside the try block and if the input cannot be converted to float then ValueError would have been given as output, but we have raised the exception for ValueError and handled the exception successfully.

4. Conclusion

In this article, we have learned how can we check if any provided number is negative, positive, or zero and also implemented the exception handling in case the output is not a number.


Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x