Python code to verify if the number is positive, negative or zero

To determine whether a number is positive, negative or zero is crucial for various mathematical and statistical analyses. Such as calculating a factorial of a number or analysing experimental results or financial data.

As we know any result of an experiment or factorial of a negative number is not a feasible outcome. In addition to that the factorial of the number 0 is predefined and equals to 1. Therefore it is imperative to determine and check whether a number is positive, negative, or 0.

In today’s lesson, we are going to learn about a simple Python program to check whether a number is Positive, Negative or 0.

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

# Check Whether a Number is Positive, Negative, or Zero in Python # accept input from user num = float(input("Enter any number: ")) # using if condition to verify number type if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")

- Accepting the input from the user
- As we are checking for a sign of a number, we accept the float value as input
- Syntax : num = float(input(“Enter any number: “))

- Using
**‘if’**to determine if the number is a positive number- As we are comparing the conditional statements, we are using these conditions
- We are using the
**if**condition to check whether the number is greater than 0 - The syntax is : if num > 0: print (“Positive number”)

- Using
**‘elif’**to determine if the number is zero- ‘elif’ condition is executed when the ‘if’ statement is false
- Hence using the
**elif**condition to show that the input number is zero, which is not a positive number - Therefore, syntax for the elif is : elif num == 0: print (“Zero”)

- Using
**‘else’**to determine if the number is a negative number- ‘else’ condition is executed when all the above ‘if’ or ‘elif’ condition is false
- Which is the reason why we are using the
**else**condition to verify the input number is a negative number - As we have checked the number for being zero or a natural number, the only numbers left are a negative number
- Therefore, syntax for the else is : else: print (“Negative number”)

#### Output:

```
Enter any number: -9
Negative number
```

```
Enter any number: 5
Positive number
```

```
Enter any number: 0
Zero
```

In this Python program, we will take input from the user and check whether the number entered is positive, negative or zero using an if-elif-else statement. As the user enters a number, the input is stored in the variable num. If the user enters a decimal number, it is converted to a float number using the float function.

The program then uses an if statement to check whether ‘num’ is greater than 0. If the number is greater than 0, the code prints “Positive number”. If num is not greater than 0, the elif statement checks if it is equal to 0, if yes then it displays “Zero” as output. But, if num is neither greater than 0 nor equal to 0, the else statement is executed and the output displays the text “**Negative number**“.

For example, if the user enters -9 as input, the output comes as “**Negative number**“. When the user enters 0 as input, the output is “**Zero**“. And, when the user enters 5 as input, the output is “**Positive number**“.

**Why We used the ‘if’, ‘elif’ and ‘else’ conditional statements**

We used this program to determine whether a number is positive, negative, or zero. This method checks the value of the input number and prints the appropriate response using an if-else expression. Alternatively, we can use the ternary operator in Python, which is a shorthand way of writing an if-else statement in a single line of code.

The following is an alternate code using the ternary operator:

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

This code produces the same results, but it may be more difficult to understand for individuals who are unfamiliar with the ternary operator.

In this tutorial, we learned about the logic and the execution of the logic to determine if the number is negative, positive or zero in Python. We used the ‘if’, ‘elif’ and ‘else’ conditional statements to create the logic for the code. Checking the sign of a number may be a common operation in programming, and Python gives a basic and instinctive way to execute it.

#### FAQs on Check Whether a Number is Positive, Negative, or 0 in Python

**If I insert a non-numeric input, will the program run?**

No, this program can only accept numeric input. If non-numeric input is entered, it will result in a ValueError.

**Can this program handle very large positive or negative numbers?**

Yes, this program can handle very large numbers, as long as they are within the range of float data type in Python.

**What will happen if I enter a string containing a number?**

If you enter a string containing a number, the program will convert it to a float data type. But, if the string does not contain a number, it will result in a ValueError.