• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Check if Number is Negative in Python

How to Check if Number is Negative in Python

June 22, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Using Python to Split String by Newline
  • 2.  Using Python to Create Empty DataFrame with pandas
  • 3.  Using Python to Find Second Smallest Value in List
  • 4.  Remove Decimal from Float in Python
  • 5.  Using Python to Get Domain from URL
  • 6.  Change Column Name in pandas DataFrame
  • 7.  Python acos – Find Arccosine and Inverse Cosine of Number
  • 8.  Using Python to Sort Two Lists Together
  • 9.  Perfect Numbers in Python
  • 10.  Convert String to List Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy