Latest Code Tutorials

Python abs() Method: How to Find Absolute Value

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, and that can also be a floating-point number.

Python absolute value

Python abs() is an inbuilt function that returns the absolute value of the given number argument. If the number is complex, the abs() method returns its magnitude.  The abs() takes only one argument. The parameter can be the 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 say, and I have a list of number 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

If you want to calculate the absolute value element-wise in the array, then 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 next()

Python iter()

Null Object In Python

Python Recursion

Python Operators

Python Not Equal Operator

Python open()

Python ord()

Python sum

Python Time sleep()

1 Comment
  1. arya says

    THANK YOU SO MUCH……….It helped me

Leave A Reply

Your email address will not be published.

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