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.
print(abs(-4))    # 4
print(abs(-4.6))  # 4.6
print(abs(4))     # 4
print(abs(4.6))   # 4.6
Data type of the output is same as input 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'>
We can use built in function abs() to get absolute value of complex number.
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
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer