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:
- Greater than 0 → positive.
- Less than 0 → negative.
- 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
pythonif 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 comparison2
if-elif-else handles mutually exclusive cases3
Zero is a special case (neither positive nor negative)
4
This pattern is used in many real-world applications
Step-by-Step Breakdown
- 1Read a number from the user.
- 2Check if it is greater than 0 and print positive.
- 3Else if it is less than 0, print negative.
- 4Otherwise, print that it is zero.