Check If Given Number is Positive, Negative or 0
Write a Python program to check if a given number is a positive, negative or 0 number ?
You can use Python’s nested if or if…elif…else statements to check if a given number positive number, negative number of a 0.
Example – Check Using Nested if
1 2 3 4 5 6 7 8 9 10 11 12 | num = -13 # Uncomment below to take user input # num = float(input("Enter any number: ")) if num >= 0: if num == 0: print("Given number is zero") else: print("Given number is a positive number") else: print("Given number is a negative number") |
Example – Check Using if…elif…else
1 2 3 4 5 6 7 8 9 10 11 | num = -13 # Uncomment below to take user input # num = float(input("Enter any number: ")) if num > 0: print("Given number is a positive number") elif num == 0: print("Given number is zero") else: print("Given number is a negative number") |
Here, we have used the if…elif…else statement. We can do the same thing using nested if statements as follows.