Python Absolute Value - Everything You Need to Know

Posted in /   /  

Python Absolute Value - Everything You Need to Know
vinaykhatri

Vinay Khatri
Last updated on November 9, 2024

    In Python, we often deal with negative and positive numeric values. Python provides different data types, and numeric values, integers, and floats are the most commonly used Python numeric data types . Many times in Python programming, we just want the positive numeric value from the result. In this Python tutorial, we will discuss how we can change the sign of a numeric value with the help of the Python absolute function.

    How to Get Absolute Value in Python?

    An absolute value in mathematic refers to a non-negative or positive number. For example, the absolute value of -12 is 12 and the absolute value for 12 is also 12. The mathematic representation of absolute value is as follows: |-12| =12 In Python,  we can find out the absolute value of a number with the following two methods:

    • Python abs() method
    • Python math.abs() method

    In most cases, you will be using the abs() method.

    Find the Absolute Value in Python Using abs() Function

    The Python abs() method is used to find out the absolute value for a specified number. If the specified number is a floating-point number , the return value will also be a floating-point number. Similarly, if the specified number is an integer, the return value will also be an integer.

    abs() Method Example:

    num_1 = -12
    num_2 = -12.23
    num_3 = -12.456
    num_4 = 12.4555
    
    print("The absolute value of num_1 is :", abs(num_1))
    print("The absolute value of num_2 is :", abs(num_2))
    print("The absolute value of num_3 is :", abs(num_3))
    print("The absolute value of num_4 is :", abs(num_4))

    Output:

    The absolute value of num_1 is : 12
    The absolute value of num_2 is : 12.23
    The absolute value of num_3 is : 12.456
    The absolute value of num_4 is : 12.4555 

    Find the Absolute Value in Python Using math.fabs() Function

    Python has an inbuilt module called math, and it contains a method .fabs() that you can use to find the absolute value for a numerical value. The working of math.fabs() is similar to abs() , however, it always returns a floating-point number as an absolute value.

    math . fabs() Method Example:

    import math
    num_1 = -12
    num_2 = -12.23
    num_3 = -12.456
    num_4 = 12
    
    print("The absolute value of num_1 is :", math.fabs(num_1))
    print("The absolute value of num_2 is :", math.fabs(num_2))
    print("The absolute value of num_3 is :", math.fabs(num_3))
    print("The absolute value of num_4 is :", math.fabs(num_4))

    Output:

    The absolute value of num_1 is : 12.0
    The absolute value of num_2 is : 12.23
    The absolute value of num_3 is : 12.456
    The absolute value of num_4 is : 12.0

    <Note> : In the above example, you can see that for -12 and 12 the absolute value return by the math.fabs() is 12.0 not 12.

    Get Absolute Values from a Python list/array

    If you have a Python list or Python array that has multiple numbers and you want to get absolute value for every number, you can simply use the abs() or math.fabs() method and map() function. This way you can find absolute value for every number. Following are some examples that demonstrate how you can use abs () and math.fabs() functions with map() function to find the absolute value for each number in a list or array.

    Example 1:

    my_nums = [-12, -34, 11, 28, -78, 2, -123]
    abs_values = list(map(abs, my_nums))
    print("The absolute values are:", abs_values)

    Output:

    The absolute values are: [12, 34, 11, 28, 78, 2, 123]

    Example 2:

    import math
    my_nums = [-12, -34, 11, 28, -78, 2, -123]
    abs_values = list(map(math.fabs, my_nums))
    print("The absolute values are:", abs_values)

    Output:

    The absolute values are: [12.0, 34.0, 11.0, 28.0, 78.0, 2.0, 123.0]

    Change Number Sign using Python Unary Operator

    The Python unary operator allows us to change the sign of a numeric value. For instance, if the number is positive, then by using the unary operator, we can change it to negative. Similarly, if the number is negative, we can change it to positive.

    unary negative operator syntax

    num =- num

    Example

    >>> num = 1
    >>> num =- num
    >>> num
    -1

    <Note> Do not confuse the Python unary operator a =-a with Python compound Assignment operator a -= a .

    Conclusion

    The abs() method is used more often as compared to the math.fabs() method because it returns a similar data type. On the other hand,  the fabs() method returns a floating data value for every number. In this Python tutorial, you learned how to find the absolute value in Python using abs() and math.fabs() methods. Also, you learned how to use the abs and math.fabs() methods with map() function to find out the absolute value for every number on a list or array.

    People are also reading:

    Leave a Comment on this Post

    0 Comments