To check if a number is negative using Python, you can use the less than operator < to check if a number is less than 0.

a = -5

print(a < 0)

#Output:
True

When working with numbers, the ability to check for certain properties of those numbers easily can be valuable.

One such case is if you want to check if a number is negative. Checking to see if a number is a negative number can be useful in error handling or data cleansing, for example.

A number is negative if it is strictly less than 0.

Therefore, to check if a number is negative using Python, you can use the less than operator < to check if a number is less than 0.

Below are some examples showing you how to check if a number is negative using Python.

a = -5
b = 8 
c = -10

print(a < 0)
print(b < 0)
print(c < 0)

#Output:
True
False
True

If you want to perform some actions if a number is negative, then you can use an if statement as shown below.

a = -5

if a < 0: 
   do_something()

Check if Number is Positive with Python

To check if a number is positive using Python, you can use the greater than operator > to check if a number is greater than 0.

Below are some examples showing you how to check if a number is positive using Python.

a = -5
b = 8 
c = -10

print(a > 0)
print(b > 0)
print(c > 0)

#Output:
False
True
False

Hopefully this article has been useful for you to check if a number is negative in your Python code.

Categorized in:

Python,

Last Update: March 12, 2024