How to Check If a Number Is Positive in Python
Determining whether a number is positive, negative, or zero is a fundamental operation in programming logic. Whether you are validating user input, calculating physical properties (like speed or mass), or managing financial data, checking the sign of a number is essential.
This guide explains how to check if a number is positive using Python's conditional statements and how to handle different numeric types like integers and floats.
Understanding Positive Numbers
In mathematics and computing, a positive number is any number strictly greater than zero (> 0).
- Examples:
1,5,100,0.5,3.14. - Zero (
0) is typically considered neither positive nor negative (neutral), though definitions can vary in specific contexts (e.g., "non-negative" includes zero).
Method 1: Basic Positive Check (Greater Than Zero)
To check if a number is positive, use the greater-than operator (>).
number = 10
# ✅ Correct: Check if strictly greater than 0
if number > 0:
print(f"{number} is a positive number")
Output:
10 is a positive number
If you change the number to -5 or 0, the code inside the if block will simply not execute.
Method 2: Handling Zero and Negative Numbers
In most real-world scenarios, you need to know exactly which category a number falls into: positive, negative, or zero. You can handle all three cases using an if-elif-else structure.
number = 0
if number > 0:
print(f"{number} is a positive number")
elif number == 0:
print(f"{number} is zero")
else:
# If it's not positive and not zero, it must be negative
print(f"{number} is a negative number")
Output:
0 is zero
Test Cases:
- If
number = 5: Output is "5 is a positive number". - If
number = -5: Output is "-5 is a negative number". - If
number = 0: Output is "0 is zero".
Order matters. If you used >= 0 (greater than or equal to) in the first check, zero would be classified as positive in that context. Usually, strict inequality (>) is preferred to distinguish zero separately.
Handling Different Numeric Types (Int vs Float)
Python handles comparison operators seamlessly across different numeric types. You do not need to convert integers to floats or vice versa to check their sign.
Checking Floating-Point Numbers
Decimals work exactly the same way.
# A positive float
num_float = 3.14
if num_float > 0:
print(f"{num_float} is a positive number")
# A negative float
num_negative_float = -2.5
if num_negative_float < 0:
print(f"{num_negative_float} is a negative number")
Output:
3.14 is a positive number
-2.5 is a negative number
Mixing Types
You can compare an integer variable against 0.0 (float zero) or a float variable against 0 (integer zero) without issues.
val_a = 10 # int
val_b = 0.0 # float
# Python automatically handles the type conversion for comparison
if val_a > val_b:
print("10 is greater than 0.0")
Output:
10 is greater than 0.0
Conclusion
To check if a number is positive in Python:
- Use
if number > 0:for a standard positive check. - Use
elif number == 0:if you need to handle zero as a distinct case. - Rely on Python's dynamic typing to handle both integers (
10) and floats (3.14) with the same logic.