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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  Get Days in Month from Date in pandas DataFrame
  • 2.  pandas set_value – Using at() Function to Set a Value in DataFrame
  • 3.  How to Get the Size of a List in Python Using len() Function
  • 4.  Python tan – Find Tangent of Number in Radians Using math.tan()
  • 5.  Using Python to Return Two Values from Function
  • 6.  Using Lambda Expression with max() in Python
  • 7.  Get First Character of String in Python
  • 8.  Replace Forwardslashes in String Using Python
  • 9.  Python Try Until Success with Loop or Recursion
  • 10.  Date Format YYYYMMDD in 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