Check Positive, Negative, or Zero

Determine whether a given number is positive, negative, or zero.

PythonBeginner

What You'll Learn

  • Using relational operators > and <
  • Designing mutually exclusive condition chains
  • Handling the special case of zero
Python
# Program to check if a number is positive, negative, or zero

num = float(input("Enter a number: "))

if num > 0:
    print(num, "is positive")
elif num < 0:
    print(num, "is negative")
else:
    print("The number is zero")

Output

Enter a number: -3
-3.0 is negative

Check Positive, Negative, or Zero in Python

We divide all real numbers into three disjoint cases:

  1. Greater than 0 → positive.
  2. Less than 0 → negative.
  3. Exactly 0 → zero.

An if-elif-else chain is perfect for mutually exclusive conditions like these.

Understanding the Cases

  • Positive numbers: 1, 2, 3, 4.5, 100, etc.

  • Negative numbers: -1, -2, -3, -4.5, -100, etc.

  • Zero: 0 (neither positive nor negative)

Program Logic

python
if num > 0:
    print(num, "is positive")
elif num < 0:
    print(num, "is negative")
else:
    print("The number is zero")

Key Takeaways

1
Use > and < operators for comparison
2
if-elif-else handles mutually exclusive cases
3

Zero is a special case (neither positive nor negative)

4

This pattern is used in many real-world applications

Step-by-Step Breakdown

  1. 1Read a number from the user.
  2. 2Check if it is greater than 0 and print positive.
  3. 3Else if it is less than 0, print negative.
  4. 4Otherwise, print that it is zero.