Python fabs: How to Get Absolute Value of Number

Python fabs() function is one of the Math functions that returns the absolute value ((Positive value) of the particular expression or a specific number.

Python fabs()

Python fabs() is a built-in math function used to get the absolute value of a number. It first finds the absolute value, and it converts the number into a float type number, no matter whether the given number is an integer or float.

The fabs() method is under the math library in Python, which means we have first to import math to use this method.




The fabs() method takes one parameter, the number whose absolute value will be returned.

number: It can be a number or a valid numerical expression for which you want to find an absolute value in Python.

  • If a numeric argument is a positive or negative number, the fabs() function will return the absolute value.
  • If a number argument is not a number, fabs() function will return TypeError.

Return Value

The method returns the absolute value of the given number and returns in float type.

Programming Example

import math
# Take two integer from user
num1 = float(input("Enter a float number: "))
num2 = int(input("Enter an integer: "))

# Now we will check what happens when we call fabs()
print("Absolute value of float number is: ", math.fabs(num1))
print("Absolute value of integer is: ", math.fabs(num2))

# Sum of these two number
sum1 = num1+float(num2)
# Printing sum and absolute sum
print("Sum is: ", sum1)
print("Absolute value of sum is: ", math.fabs(sum1))


Enter a float number: -26.4
Enter an integer: 12
Absolute value of float number is:  26.4
Absolute value of integer is:  12.0
Sum is:  -14.399999999999999
Absolute value of sum is: 14.399999999999999

In the above program, we have taken two inputs: float (negative), and the other is a positive integer. Then we have printed their absolute value.

Here we can see that the integer number (12) is converted into a float type number. After that, we have calculated the sum of these two numbers; for this, we first converted the integer into the float.

At last, we have printed the absolute value of the sum.

Example 2

See the following programming.


import math

Tup = (11, 21, 30, -46, 19)  # Tuple Declaration
Lis = [-11, 22, -3.3, -44, 55]  # List Declaration

print('Absolute value of Positive Number = %.2f' % math.fabs(10))
print('Absolute value of Negative Number = %.2f' % math.fabs(-15))

print('Absolute value of Tuple Item = %.2f' % math.fabs(Tup[3]))
print('Absolute value of List Item = %.2f' % math.fabs(Lis[2]))

print('Absolute value of Multiple Number = %.2f' % math.fabs(10 + 20 - 40))
print('Absolute value of String Number = ', math.fabs('AppDividend'))


Absolute value of Positive Number = 11.00
Absolute value of Negative Number = 19.00
Absolute value of Tuple Item = 46.00
Absolute value of List Item = 3.30
Absolute value of Multiple Number = 10.00
Traceback (most recent call last):
  File "", line 13, in <module>
    print('Absolute value of String Number = ', math.fabs('AppDividend'))
TypeError: must be real number, not str

In the above example, we have used the string, tuple, and list.

In the case of a string, it returns TypeError because we need an argument as an integer.

Difference between fabs() and abs() in Python

Both the abs() and fabs() functions used to find a number absolute value. Both will return the absolute value of the number.

The difference is that math.fabs(number) will always return a floating-point number even if the argument is an integer. In contrast, Python abs() will return the floating-point or an integer depending upon the argument.

If the argument is a complex number, abs() will return the magnitude part, whereas fabs() will return an error.


Python math.fabs(number) will always return a floating-point number even if the argument is an integer, if you pass the complex number or other than the number it returns an error.

See also

Python math copysign()

Python math functions

Python math.sqrt()

Python math.floor()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.