PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Program to Check if a Number is Positive, Negative, or 0

Python Program to Check if a Number is Positive, Negative, or 0

Updated on: March 31, 2025 | Leave a Comment

This is a simple problem statement in which we need to find whether a number is positive, negative or zero using Python. Any number greater than zero is positive, and any number less than zero is called a negative number. The number may be a float or an integer.

This article discusses simple ways to check the number using Python examples.

Table of contents

  • 1. Using if-elif-else Statements
  • 2. Using Ternary (Conditional) Operator
  • 3. Using Lambda function
  • 4. Using NumPy’s sign Function
  • Summary

1. Using if-elif-else Statements

This is the most straightforward way to check the sign of a number in Python using if-elif-else.

  • If num is greater than 0, it is positive.
  • Else if num is less than 0, it is negative.
  • Else num is equal to 0, it is zero.

Code Example

num = 10

if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")
     
# Output:
# PositiveCode language: Python (python)

2. Using Ternary (Conditional) Operator

Python doesn’t have a direct ternary operator like other languages, but you can simulate it with conditional expressions to find if the number is positive, negative or zero.

This is a more compact way of writing conditions. The conditional expressions are evaluated from left to right.

  • If num > 0, it prints “Positive”.
  • Else if num < 0, it prints “Negative”.
  • Otherwise, it prints “Zero”.

Code Example

num = -10
sign = "Positive" if num > 0 else "Negative" if num < 0 else "Zero"
print(sign)

# Output:
# NegativeCode language: Python (python)

3. Using Lambda function

A lambda function in Python is a small, anonymous function that is defined using the lambda keyword. It can have multiple arguments but only one expression, which is evaluated and returned.

lambda arguments: expression

  • lambda → Keyword to define the function.
  • arguments → Input parameters (similar to function arguments).
  • expression → A single operation that returns a result.

This is more concise way to perform the same check in a more functional programming style. It works similar to the ternary operator but wrapped inside a lambda.

Code Example

num = -10
check_sign = lambda num: "Positive" if num > 0 else "Negative" if num < 0 else "Zero"
print(check_sign(num))

# Output:
# NegativeCode language: Python (python)

4. Using NumPy’s sign Function

If you are working with NumPy, a popular library for numerical computing, you can use numpy.sign() that returns the sign of a number or an array of numbers.

numpy.sign(num) returns 1 for positive numbers, -1 for negative numbers, and 0 for zero. Based on this, you can print the corresponding message.

Note: The Numpy library is not bundled with the default Python installation. We need to install it separately.

Code Example

import numpy as np

num = 10
sign = np.sign(num)

if sign == 1:
    print("Positive")
elif sign == -1:
    print("Negative")
else:
    print("Zero")

# Output:
# PositiveCode language: Python (python)

Summary

There are various ways in Python to check if a number is positive, negative, or zero, ranging from basic conditional checks to more functional and library-based approaches.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com