Signup/Sign In

Python abs() Method - Python Library Function

Posted in Programming   LAST UPDATED: MAY 18, 2023

    In Python there are many built-in functions and Python abs() function is one of them. The main purpose of this function is to return the absolute value of the number(variable) passed as argument to it.

    This is a detailed post on Python abs() that touches every aspect of it.

    Python abs()


    Python abs()

    The abs() function in Python is used to obtain the absolute value of a number. It returns the magnitude of a number without considering its sign. In other words, it gives you the positive value of a number, regardless of whether the original value was positive or negative.

    For example, if you have the number -5, calling abs(-5) will give you 5. Similarly, if you have the number 10, calling abs(10) will still give you 10 because the absolute value of a positive number remains the same.

    The abs() function is incredibly handy when you need to ensure that you're working with positive values or when you need to compare magnitudes without worrying about the sign.

    Syntax of abs() function

    The abs() function takes only one argument and hence the syntax is pretty simple,

    # X can be an integer or a float or a complex number
    abs(X)

    If, X (the argument) is an Integer or Float then the method will return the absolute value in Integer or Float

    else if X is a Complex Number then the method will return the magnitude of that complex number.


    What is an Absolute value?

    The absolute value for a given number is the numerical value of that number without considering whether the sign of the number is positive (+ve) or negative (-ve ).

    Simply put, an absolute number is always a positive(+ve) number.

    For complex numbers, the absolute value is their magnitude.

    Example for Integer:

    The absolute value of -5 is 5.

    Example for Float:

    The absolute value of -5.5 is 5.5

    Example for Complex Numbers:

    The absolute value of 3+4j is 5.0

    Let's see, how to find the magnitude of a complex number,

    The magnitude of the complex number a+bj is sqrt( a^2 + b^2 ).

    For example: The magnitude of 3+4j is sqrt( 3^2 + 4^2 ) = sqrt( 9 + 16 ) = sqrt(25) = 5


    Example 1

    Code: Calculates and prints the absolute value of a given number.

    num = -7
    absolute_value = abs(num)
    print(absolute_value)
    


    7

    Example 2:

    Code: Finds the absolute difference between two numbers and prints the result.

    num1 = 10
    num2 = -3
    difference = abs(num1 - num2)
    print(difference)
    


    13

    Python Program using abs() Function

    Below we have a simple program where we have used the abs() method,

    Pheww! This is it. I hope that you enjoyed the post and learned about the implementation of abs() the built-in function available in Python Standard Language. If you feel that this post is useful, please share it with your friends and colleagues.

    Thanks for reading it till the end.

    Freqeuntly Asked Questions(FAQs)

    1. What is round () and abs () in Python?

    • The round() function is used to round a number to a specified precision or to the nearest integer.
    • The abs() function is used to obtain the absolute value of a number, returning its positive magnitude.

    2. What is the absolute difference in Python?

    The absolute difference in Python is the positive value obtained by subtracting one number from another, regardless of the order. It measures the numerical distance between two values.

    3. What is absolute difference used for?

    The absolute difference is used to compare the magnitude or distance between two values without considering their signs. It allows for comparison or measurement of numerical dissimilarity, neglecting the direction of the difference.

    4. What is the difference between abs and absolute in Python?

    In Python, there is no difference between abs() and absolute(). They both refer to the same concept of obtaining the positive magnitude of a number. abs() is the commonly used function, while absolute() is not a built-in function in Python.

    About the author:
    I am a self-taught Blogger and Programmer. I have good sort of knowledge in Technology, PHP, Javascript, HTML, CSS and Python.
    Tags:python
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS