How to find absolute value in python?

Absolute value is defined as if point mapped on two dimensional XY axis; it is the distance from the origin. Remember, it is always positive. In this article, we will discuss how we can find absolute value in python.

Short Tutorial: Introduction to Python

Now which function we used to identify absolute number in python?

abs() is the function available in python to calculate absolute number.

math.fabs() is another function available to calculate the absolute number but as a floating type.

Syntax

>>abs(value) #value is a parameter 
>>math.fabs(value) #value is a parameter

Short Tutorial: Variable and Data Types in Python

Examples of finding absolute value in python

  • Absolute number of positive integer
>>abs(87)
  • Absolute number of negative integer - it returns positive integer by dropping negative sign
>>abs(-67)
  • Absolute value of complex number - it returns magnitude of complex number
>>abs(4+5j)

  • What if calculation happens inside abs() function - it solves the equation and return positive numeral
>>abs((3*-4)+37)
  • absolute value of positive floating value - it returns same value
>>abs(3234.323)
  • absolute value of negative floating value - it returns same value by dropping negative sign)
>>abs(-234.432)

Short Tutorial: Tuples, Sets and Dictionaries in Python

math.fabs() examples

>>import math
>>math.fabs(68)
>>math.fabs(-987)
>>math.fabs((3*4)+37))
>>math.fabs(98.763)
>>math.fabs(-378.3232)

Note: math.fabs() not able to identify absolute value of complex number.

Conclusion

abs() and math.fabs() are two in-built python function which we can use to calculate the absolute number.

Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

You may also like...

Leave a Reply

Your email address will not be published.