Python Absolute Value

By | April 12, 2021
Python Absolute Value

In Python, we often deal with negative and positive numeric values. Python provides different data types for numeric values and integers and floats are one of the commonly used Python numeric data types. Many times in Python programming we deal with positive and negative numeric values and sometimes we just want the positive numeric value from our result. Let’s see how can we change the sign of our numeric value with the help of the python absolute function.

How to Get absolute value in Python

An absolute value in mathematic refers to a non-negative or positive number. For example, the absolute value of -12 is 12 and the absolute value for 12 is also 12.

Vamware

Mathematic representation of absolute value

|-12| =12.

In Python, an absolute value of a Number can find out using two methods.

  • Python abs() method
  • Python math math.fabs() method.

For most of the cases, you will be using abs() method, in Python programming.

Find the absolute value in Python using abs() function

The Python abs() method is used to find out the absolute value for the specified number. If the specified number is a floating-point number then the return value will also be a floating-point number. And if the specified number is an Integer, the return value will also be an Integer.

abs() method example

num_1 = -12
num_2 = -12.23
num_3 = -12.456
num_4 = 12.4555

print("The absolute value of num_1 is :", abs(num_1))
print("The absolute value of num_2 is :", abs(num_2))
print("The absolute value of num_3 is :", abs(num_3))
print("The absolute value of num_4 is :", abs(num_4))

Output

The absolute value of num_1 is : 12
The absolute value of num_2 is : 12.23
The absolute value of num_3 is : 12.456
The absolute value of num_4 is : 12.4555 

Find the absolute value in Python using math.fabs() function

Python has an inbuilt module called math, and it contains a method .fabs() which can also be used to find the absolute value for a numerical value.

The working of math.fabs() is similar to abs() but it always returns a floating-point number as an absolute value.

 math.fabs() method example

import math
num_1 = -12
num_2 = -12.23
num_3 = -12.456
num_4 = 12

print("The absolute value of num_1 is :", math.fabs(num_1))
print("The absolute value of num_2 is :", math.fabs(num_2))
print("The absolute value of num_3 is :", math.fabs(num_3))
print("The absolute value of num_4 is :", math.fabs(num_4))

Output

The absolute value of num_1 is : 12.0
The absolute value of num_2 is : 12.23
The absolute value of num_3 is : 12.456
The absolute value of num_4 is : 12.0

<Note>: In the above example, you can see that for -12 and 12 the absolute value return by the math.fabs() is 12.0 not 12.

Get absolute values from a Python list/array

If you get a list of numbers and want to get absolute value for every number then using the abs() or math.fabs() method and map() function you can find absolute value for every number.

 Example 1

my_nums = [-12, -34, 11, 28, -78, 2, -123]
abs_values = list(map(abs, my_nums))
print("The absolute values are:", abs_values)

Output

The absolute values are: [12, 34, 11, 28, 78, 2, 123]

Example 2

import math
my_nums = [-12, -34, 11, 28, -78, 2, -123]
abs_values = list(map(math.fabs, my_nums))
print("The absolute values are:", abs_values)

Output

The absolute values are: [12.0, 34.0, 11.0, 28.0, 78.0, 2.0, 123.0]

Change Number sign using Python unary operator

The Python unary operator is used to change the sign of a numeric value. For instance, if the number is positive then using the unary operator, we can change it to negative and if the number is negative, we can change it to positive.

unary negative operator syntax

num =- num

Example

>>> num = 1
>>> num =- num
>>> num
-1

<Note> Do not confuse the Python unary operator a =-a with Python compound Assignment operator a -= a.

Conclusion

In this Python tutorial, you learned how to find the absolute value in Python using Python abs and math.fabs() method.

The abs() method is more often used as compared to the math.fabs() method, because it returns a similar data type whereas the fabs() return the floating data value for every number. Here you also learned how can you use the abs and math.fabs() methods with map() function to find out the absolute value for every list value.

Leave a Reply

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