Python math.fabs() Method

The math.fabs() method is used to return the absolute value of a number as a float.

This method eliminates the negative sign from a value if it exists.

Syntax

import math
math.fabs(num)

Parameter

num(required): A number, either integer or float, from which you want to get the absolute value.

Return Value

It returns a float even if the argument is an integer.

If the input number is not a number , it returns TypeError.

Visual Representation

Visual Representation of Python math.fabs() Method

Example 1: How to Use math.fabs() Method

import math

print(math.fabs(25))
print(math.fabs(-25)) 
print(math.fabs(2.67)) 
print(math.fabs(-2.67)) 

Output

25.0
25.0
2.67
2.67

Example 2: Passing a tuple and list elements as arguments

import math

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

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

Output

Absolute value of Tuple Item = 46.00
Absolute value of List Item = 3.30

Leave a Comment

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