• 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
  • VBA
  • 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.  How to Create Array from 1 to n in Python
  • 2.  Cartesian Product of Two Lists in Python
  • 3.  Count Primes Python – How to Count Number of Primes in a List
  • 4.  Check if Word is Palindrome Using Recursion with Python
  • 5.  pandas startswith() – Check if String Starts With Certain Characters
  • 6.  Rename Key in Dictionary in Python
  • 7.  pandas drop – Drop Rows or Columns from DataFrame
  • 8.  Remove Empty Strings from List in Python
  • 9.  Python Turtle Fonts – How to Write Text with Different Fonts in Python
  • 10.  pandas covariance – Calculate Covariance Matrix Using cov() Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy