AppDividend
Latest Code Tutorials

Python Absolute Value Example | Python abs() Function

1

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 number.   If an argument is an integer or floating-point number, abs() returns the absolute value in integer or float.

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 example

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.

Python abs() method syntax is the following.

abs(n)

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

Leave A Reply

Your email address will not be published.

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