The absolute value of an integer in Python Programming Language can be found by using a fabs()
function from Python Standard math Library.
math.fabs()
function in Python
fabs()
function exists in Standard math Library of Python Programming Language. The purpose of this function is to return an absolute (positive) value of any numeric expression.
Python math.fabs()
Function Syntax
The syntax of
math.fabs()
function in Python is:math.fabs( x )
Python fabs()
Function Parameters
x
– Any valid Python positive or negative number. This parameter is required.Note: if the
x
parameter is not a number, fabs()
function will return an error.
Python fabs()
Function Compatibility
Python 2.x – Yes
Python 3.x – Yes
Python 3.x – Yes
Python fabs() Function
Return Value
This function will return an absolute or positive value.
Python fabs()
Function Examples
# import math library
import math
print(math.fabs(27))
print(math.fabs(-2.9))
print(math.fabs(2.9))
Output of Python fabs()
Function:
27.0
2.9
2.9
Note that in output all the numbers whether they are negative or positive are converted into a positive number by using fabs()
function.