abs() Absolute value of number
abs function returns absolute value of input number ( with same dtype ).
abs() is a built-in function of Python so no need to import any module.
Here are some examples with different type of numbers.
Data type of the output is same as input number.
print(abs(-4)) # 4
print(abs(-4.6)) # 4.6
print(abs(4)) # 4
print(abs(4.6)) # 4.6
We can use built in function abs() to get absolute value of complex number.
print(type(abs(-4))) # <class 'int'>
print(type(abs(-4.6))) # <class 'float'>
print(type(abs(4))) # <class 'int'>
print(type(abs(4.6))) # <class 'float'>
print(abs(4+8j)) # 8.94427190999916
print(type(abs(4+8j))) # <class 'float'>
Difference between fabs() and abs()
fabs() is included in Python math module so we have to import it before using. Where as abs() is part of built in functions of core Python so no need to import any library.
fabs() returns always float dtype. Whenever input number can’t be converted to float , fabs() generates error. However abs() returns the same dtype of input number.
Variables in Python
Keywords in Python
All Built in Functions in Python
This article is written by plus2net.com team.