• 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 / Sign Function in Python – Get Sign of Number

Sign Function in Python – Get Sign of Number

April 28, 2022 Leave a Comment

In Python, there is no sign function, but it is easy to get the sign of a number by defining our own sign function.

def sign_function(x):
    if x > 0:
        return 1
    elif x == 0:
        return 0
    else:
        return -1

print(sign_function(-10))

# Output:
-1

You can also use the Math module copysign() function – the second parameter is the number which you want to check the sign.

print(math.copysign(1, -3))

# Output:
-1

The numpy module also has a sign function you can use to get the sign of numbers in an array.

import numpy as np

print(np.sign([10,0,-10])

# Output:
[1, 0, -1]

In mathematics and computer science, the sign function is simple yet very common and useful for many applications.

By definition, the sign function is a function where numbers greater than 0 are assigned a value of 1, numbers equal to 0 are assigned a value of 0, and numbers less than 1 are assigned a value of -1.

When working in Python, it may be useful to be able to easily find the sign of numbers.

Unfortunately, there is not a built-in sign function, but we can easily define one ourselves.

Below is an example of how you can create your own sign function in Python.

def sign_function(x):
    if x > 0:
        return 1
    elif x == 0:
        return 0
    else:
        return -1

print(sign_function(10))
print(sign_function(-10))

# Output:
1
-1

How to Find the Sign of a Number in Python with the Math Module

If you don’t want to define your own sign function, you can use a function from the math module.

While the math module doesn’t have a sign function, the copysign() function from the math module can return the sign of a number.

The copysign() function takes two inputs. The idea with copysign() is it returns the first input with the sign of the second input.

So if all you want is the sign of a number, you can pass ‘1’ for the first input and your number for the second input.

Below is a simple example showing how to use the math module copysign() function in Python.

print(math.copysign(1, -3))
print(math.copysign(1, 5))

# Output:
-1
1

Using the numpy Module Sign Function to Find the Signs of Numbers in an Array

The numpy module also has a sign function.

You can use the numpy module sign function to find the sign of numbers in an array. Just pass an array of numbers and you will get back an array of their signs.

Below is an example of how you can find the signs of numbers in an array in Python with numpy.

import numpy as np

print(np.sign([10,0,-10])

# Output:
[1, 0, -1]

Hopefully this article has been useful for you to learn how to create a Python sign function and find the sign of numbers in Python.

Other Articles You'll Also Like:

  • 1.  Python tanh – Find Hyperbolic Tangent of Number Using math.tanh()
  • 2.  Using Python to Compare Strings Alphabetically
  • 3.  pandas idxmax – Find Index of Maximum Value of Series or DataFrame
  • 4.  Add Key Value Pair to Dictionary in Python
  • 5.  Examples of Recursion in Python
  • 6.  Calculate Sum of Dictionary Values in Python
  • 7.  Check if List is Subset of Another List in Python
  • 8.  Using Python to Iterate Over Two Lists
  • 9.  Python Turtle Fonts – How to Write Text with Different Fonts in Python
  • 10.  Convert List to Set with 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