This article is part of in the series
Published: Wednesday 5th March 2025

how to use python absolute value

In Python, the absolute value of a number refers to its non-negative value, regardless of its sign. The built-in "abs()" function allows you to quickly determine the absolute value of any number. Whether the number is an integer, floating-point number, or even a complex number, the abs() function can handle them all.

Today let PythonCentral take you through the basics, instructions to use Python’s absolute value function "abs()", its syntax, practical examples, and a few alternatives as well.

What is Absolute Value

First, let us learn about absolute value before we head to the advanced concepts. The absolute value of a number is its distance from zero on the number line. This means:

  • Absolute value of 5 is "5"
  • Absolute value of -5 is "5"
  • Absolute value of 0 is "0"

How to Use Python abs() Function

The "abs()" function is the easiest way to calculate absolute values in Python. Here is the basic syntax for Python absolute value function:

abs(number)

The number can be an int, float, or complex number (more on these later). Here is an example to understand better:

print(abs(-10)) # Gives you an output 10
print(abs(3.5)) # Output will be 3.5
print(abs(0)) # Output will be 0

How to Calculate the Absolute Value of Complex Numbers with Python

Let's say you have to calculate "3 + 4j". To calculate the absolute value of complex numbers, we are about to use the abs(complex_num) syntax. Here is an example:

complex_num = 3 + 4j
print(abs(complex_num)) # Output will be 5.0,  calculated as square root of (3² + 4²))

Alternative Method to Calculate Absolute Value

While the standard practice is to use the Python absolute value function, there are other methods as well that gets you same results. Let us look at few of them.

How to Calculate Absolute Value Using Conditional Statements

Here is an example syntax where we use conditional statements to calculate the absolute value of a number:

def absolute_value(n):
return n if n >= 0 else -n

print(absolute_value(-7)) # Output will be 7

How to Calculate Absolute Value using NumPy abs Function

For arrays and matrices, the numpy.abs() function is more efficient. We strongly recommend going through our NumPy article to learn more later.

import numpy as np
arr = np.array([-1, -2, 3, -4])
print(np.abs(arr)) # Output will be [1 2 3 4]

How to Handle Absolute Values in Real-World Applications

Now that we learnt the theory, let us see some practical applications.

How to Calculate Distance with Python

In case you would like to calculate the distance between two points. Here is how you can do that:

def distance(x1, x2):
return abs(x1 - x2)

print(distance(10, 3)) # Output will return 7

How to Normalize Data

Here is a sample syntax where you can calculate the normalized data:

data = [-100, -50, 0, 50, 100]
norm_data = [abs(num) for num in data]
print(norm_data) # Output will fetch [100, 50, 0, 50, 100]

Wrapping Up

The Python absolute function is a powerful and efficient way to calculate absolute values in Python. Whether you are working with integers, floating points, or complex numbers, using the Python abs() function gives you accuracy and simplicity.

We sincerely hope you are now familiar with the abs() function. We recommend you learn more from our other articles as well.

 

Related Articles

NumPy where(): Using Conditional Array Operations

How To Use NumPy Pad(): Examples And Syntax

How to Generate a Random Number in Python