How to Check a Number is Negative, Positive or 0 in Python
In this tutorial, we will look at some of the different ways of determining whether a number is positive, negative or zero in Python.
The if elif else Solution
First, we check if the number is greater than 0, if True
it must be positive, then we check if it is equal to zero, else the number must be negative.
num = -1
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Negative number
Nested if Statement Solution
Another way to approach this is with a nested if statement.
num = -1
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Negative number