AppDividend
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.

### 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 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.

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.

## 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))

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 say, and I have a list of number 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

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.

# 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 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

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