Skip to main content

How to Check If a Number Is Negative in Python

Determining the sign of a number (whether it is positive, negative, or zero) is a fundamental operation in programming. This logic is essential for applications ranging from banking (checking for debt) to physics simulations (directional velocity) and processing user input.

This guide explains how to check for negative numbers using conditional statements, how to handle the special case of zero, and how to avoid common type errors.

Understanding Negative Numbers in Python

In Python, a negative number is any integer or floating-point number that is less than zero. They are represented with a minus sign (-) prefix.

  • Integer: -5, -100
  • Float: -3.14, -0.001

Python automatically handles the math for these types, allowing you to use standard comparison operators.

Method 1: Using the Less Than Operator (Standard)

The most direct way to check if a variable is negative is using the less than comparison operator (<).

Basic Check

This method returns True if the number is strictly smaller than 0.

temperature = -10

# ✅ Correct: Check if strictly less than 0
if temperature < 0:
print(f"{temperature} is a negative number.")
else:
print(f"{temperature} is NOT negative.")

Output:

-10 is a negative number.

Checking Float Values

The logic remains identical for floating-point numbers.

balance = -0.50

if balance < 0:
print("Account is overdrawn.")

Output:

Account is overdrawn.

Method 2: Handling Zero Explicitly

A common logical pitfall is forgetting about Zero (0). Zero is mathematically neither positive nor negative. If you only use if and else, zero will fall into the "not negative" bucket, which is technically correct, but often you need to distinguish it specifically.

Use an if-elif-else block to cover all three states: Negative, Positive, and Zero.

number = 0

# ✅ Correct: Explicitly handling all three states
if number < 0:
print("The number is Negative.")
elif number > 0:
print("The number is Positive.")
else:
print("The number is Zero.")

Output:

The number is Zero.
note

In Python (and many other languages), 0 and -0 are usually treated as equal (0 == -0 returns True). However, in floating-point representations, -0.0 exists. The < 0 check correctly treats 0 and 0.0 as not negative.

Common Error: Comparing Strings

Inputs from users (via input()) or text files are read as strings. Attempting to compare a string directly with an integer raises a TypeError.

user_input = "-5"

try:
# ⛔️ Error: Cannot compare a string with an integer
if user_input < 0:
print("Negative")
except TypeError as e:
print(f"Error: {e}")

# ✅ Correct: Convert the string to a number first
number = float(user_input) # Use float to handle decimals too
if number < 0:
print("Negative detected after conversion.")

Output:

Error: '<' not supported between instances of 'str' and 'int'
Negative detected after conversion.
warning

Always sanitize and convert inputs using int() or float() before performing mathematical comparisons.

Conclusion

To determine if a number is negative in Python:

  1. Use if x < 0: for a simple boolean check.
  2. Use elif x > 0: and else: if you need to specifically handle positive numbers and zero.
  3. Convert inputs from strings to numeric types before comparing to avoid runtime errors.