abs() Function in Python

In this tutorial, I will explain the abs() function in Python, including its syntax and examples.

What is the abs() Function?

The abs() function in Python returns the absolute value of a given number. In simple terms, the absolute value is the distance of a number from zero, regardless of its sign. Whether the number is positive or negative, abs() will always return a non-negative value.

Syntax

The syntax for the abs() function is:

abs(number)
  • number: This is the only parameter and it represents the number for which you want to find the absolute value. It can be an integer, float, or complex number.

The abs() function is particularly useful when you need to ensure that the value you’re working with is non-negative. This can be crucial in various mathematical computations, data analysis, and even in certain financial calculations.

Suppose you’re working on a project that involves geographical distances or financial metrics; you’ll often need to work with absolute values to avoid negative distances or losses.

Check out Log() Function in Python

absolute value function in Python Examples

Let me show you some practical examples to see how abs() function works in different scenarios.

Example 1: Absolute Value of an Integer

Below is the Python code you can use to get the absolute value of an integer in Python.

number = -42
absolute_value = abs(number)
print(f"The absolute value of {number} is {absolute_value}")

Output:

The absolute value of -42 is 42

I executed the above Python code using VS code, and you can see the exact output in the screenshot below:

abs function in python

Example 2: Absolute Value of a Float

Now, let me show you another example of using the abs() function in Python. Below is Python code you can use to get the absolute value of a float in Python.

temperature_difference = -15.7
absolute_difference = abs(temperature_difference)
print(f"The absolute temperature difference is {absolute_difference}")

Output:

The absolute temperature difference is 15.7

You can also see the screenshot below for the exact output:

absolute value function in python

Check out Len() Function in Python

Example 3: Absolute Value of a Complex Number

For complex numbers, the abs() function returns the magnitude in Python. Below is the complete code.

complex_number = 3 + 4j
magnitude = abs(complex_number)
print(f"The magnitude of the complex number {complex_number} is {magnitude}")

Output:

The magnitude of the complex number (3+4j) is 5.0

Here is the exact output in the screenshot below:

python absolute value function

Check out reduce() Function in Python

Related Methods for Absolute Value in Python

While abs() is the most common way to get absolute values; however, depending on your specific needs, there are other methods and functions you might find useful.

Using math.fabs()

The math module provides the fabs() function, which also returns the absolute value but is specifically designed for floating-point numbers.

import math

negative_float = -23.67
absolute_float = math.fabs(negative_float)
print(f"The absolute value of {negative_float} using math.fabs() is {absolute_float}")

Output:

The absolute value of -23.67 using math.fabs() is 23.67

Using numpy.abs()

If you’re working with arrays or matrices, the numpy library offers the numpy.abs() function, which can handle element-wise absolute value calculations.

import numpy as np

array = np.array([-1, -2, -3, 4, 5, -6])
absolute_array = np.abs(array)
print(f"The absolute values of the array are {absolute_array}")

Output:

The absolute values of the array are [1 2 3 4 5 6]

Conclusion

In this tutorial, I explained the abs() function in Python. Whether working with simple integers, floating-point numbers, or even complex numbers, abs() ensures you get the non-negative value you need. Additionally, we saw some related methods, like math.fabs() and numpy.abs() to work with absolute values in Python.

You may also like:

Leave a Comment