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.

Syntax

Python abs() method syntax is the following.

abs(n)

Parameters

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.

# app.py

x = abs(-19.21)

print(x)

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.

# app.py

y = abs(19 + 21j)

print(y)

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.

# app.py

x = 19.21e1/2 # exponential
print(abs(x))

y = 0b1100 # binary
print(abs(y))

z = 0o11 # octal
print(abs(y))

w = 0xF # hexadecimal
print(abs(w))

See the following output.

➜  pyt python3 app.py
96.05
12
12
15
➜  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.

# app.py

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

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

# app.py

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

See the output.

➜  pyt python3 app.py
[21, 1, 19, 46]
➜  pyt

We can also use the Python list comprehension.

# app.py

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. 

# app.py

import numpy as np

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

See the output.

➜  pyt python3 app.py
[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.