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


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co

Positive and negative numbers are types of numbers that can be used to compare values against zero1. Zero is a special number that is neither positive nor negative2.

Positive numbers are numbers that are bigger, greater, or higher than zero2. They are on the right side of the number line. For example, 1, 2, 3, 4… are positive numbers. Positive numbers can be written with or without a + sign in front of them. For example, +5 and 5 mean the same thing.

Negative numbers are numbers that are smaller, lower, or less than zero2. They are on the left side of the number line. For example, -1, -2, -3, -4… are negative numbers. Negative numbers always have a - sign in front of them. For example, -6 means negative six.


Python Code :

The below Python program is used to check if a number is positive, negative, or 0:


# Get input number from user
num = float(input("Enter a number: "))

# Check if the number is positive, negative, or 0
if num > 0:
    print("The number", num, "is positive")
elif num < 0:
    print("The number", num, "is negative")
else:
    print("The number is 0")

In this program, the user is prompted to enter a number. The program then checks whether the number is positive, negative, or 0 using a series of if and elif statements. If the number is greater than 0, it is positive. If the number is less than 0, it is negative. If the number is neither greater nor less than 0, it is 0.

Finally, the program prints out the result using the print function.

Note that this program assumes that the user enters a valid floating-point number. If the user enters an invalid input, such as a string or a non-numeric value, the program will raise a ValueError or produce an incorrect result.

For different values of num, below is the output :


Enter a number: -32
The number -32.0 is negative

Enter a number: 100
The number 100.0 is positive

Enter a number: 0
The number is 0


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co