Python absolute value: The Complete Guide

Python abs() method returns the absolute value of the given number or list of numbers. If the number is complex, the abs() returns its magnitude. In the case of a complex number, abs() returns only the magnitude part, which can also be a floating-point number.

Python absolute value

To find an absolute value in Python, use the abs() function. Python abs() is a built-in function that returns the absolute value of the given number argument. The abs() method returns its magnitude if the number is complex.  The abs() accepts only one argument.

The parameter can be an integer, a floating-point number, or a complex numberIf an argument is an integer or floating-point number, abs() returns the absolute value in integer or float.


Python abs() method syntax is the following.



The n is the required parameter, and it is the number. It can be a float or a complex number.

The abs() method returns the absolute value of the given number.

  1. For integers – The absolute integer value is returned.
  2. For floating numbers – floating absolute value is returned.
  3. For complex numbers – the magnitude of the number is returned.

See the following example.


x = abs(-19.21)


See the output.

Python Absolute Value Tutorial

Python abs() with complex number

Let’s see the case where we pass the complex number as an argument. See the following code.


y = abs(19 + 21j)


See the output.

Python abs() Function

In the above example, we get the magnitude of the complex number.

You can get the magnitude of complex number z = x + yj using the following formula.

\textstyle r=|z|=\sqrt{x^2+y^2}.\,

Python abs() with different format numbers

See the following example.


x = 19.21e1/2 # exponential

y = 0b1100 # binary

z = 0o11 # octal

w = 0xF # hexadecimal

See the following output.

➜  pyt python3
➜  pyt

Python absolute value of list

Let’s see how to obtain the absolute value of numbers of a list.

Let’s say I have a list of numbers that looks like the one below.


inputList = [21, 1, -19, 46]

Now, we will use the map() and list() function to convert it to the absolute value.


inputList = [21, 1, -19, 46]
mappedList = map(abs, inputList)

See the output.

➜  pyt python3
[21, 1, 19, 46]
➜  pyt

We can also use the Python list comprehension.


print([abs(number) for number in inputList])

It will return the same output.

Find the absolute value for an array in python

To calculate the absolute value element-wise in the array, we need to import the numpy library and use its np.abs() function. 


import numpy as np

arr = [21, 1, -19, 46]
absArr = np.abs(arr)

See the output.

➜  pyt python3
[21  1 19 46]
➜  pyt

Conclusively, Python Absolute Value Example is over.

Recommended Posts

Python def

Python pow()

Python sum

Python next()

Python Not Equal Operator

1 thought on “Python absolute value: The Complete Guide”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.