Get python absolute value | Python abs() Function with examples

Question: how do you do absolute value in python?, how do you find the absolute value in python?

Definition and Usage: The abs() inbuilt function is used to return the same great absolute value of a each number.

Python abs() Function

abs() is a built-in method accessible with python, as well as it will return you the absolute data value for the user input specified.

You can also read me Prev Article for How To Get Current Date & Time In Python?

abs() in Python The abs() extracts only single parameter, a number whose absolute data value is to be go back. The parameter can be an integer, a floating point number or a complex number. If the parameter is an integer or floating point number, abs() returns the absolute data value in integer or float.

Magnitude formula for the complex numbers

python absolute value of list,python absolute value numpy,absolute value python 3,what is absolute value,python round,python sqrt,python floor,python max,absolute value python numpy,python absolute value of list,modulus of value in python,how to do square root in python,pandas absolute value,how to round in python
Magnitude formula for the complex numbers

Syntax:
abs(value)

abs() Parameters: (value)

The input data value to be specified to abs() to get the absolute data value. Such as a integer Data value, a float value, or a some complex number.

The abs() method extracts a single parameter: num – number whose absolute data value is to be go back. The number can be:

  1. integer
  2. floating number
  3. complex number

Return value from abs()

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

  • For integers – integer absolute data value is go back
  • For floating numbers – floating absolute data value is go back
  • For complex numbers – magnitude of the number is go back

Let’s test the abs() method with different inputs:

Example 1: Integer and Float number

To get the absolute data value of an integer as well as float number check this source code:

# evaluate abs() for an integer with float

int_num = -25

float_num = -10.50

print("an integer number is:", abs(int_num))
print("a float number is:", abs(float_num))

watch the output:
an integer number is: 25
a float number is: 10.5

Example 2: Complex Number

To get absolute data value of complex number

# evaluate abs() for a complex number

complex_num = (3+10j)

print("The magnitude of the complex number is:", abs(complex_num))

watch the output

The magnitude of the complex number is: 10.44030650891055

Example 3: Find the absolute value for an array in python

If you want to evaluate the absolute data value element Based in the an array, then we required to import the numpy library as well as use This is a np.abs function.

# absroot.py

import numpy as np

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

Watch the output.

➜  pyt python3 absroot.py
[21  1 19 46]
➜  pyt

Passing an integer number as a parameter:

int_number = -50
abs_number = abs(int_number)
print(abs_number)

Passing a floating number as a parameter:

float_number = 50.5
abs_number = abs(float_number)
print(abs_number)

Passing a complex number as a parameter:

complex_number = 3-4j
abs_number = abs(complex_number)
print(abs_number)

Python absolute value of list

Let’s watch how to applay the absolute data value of numbers of a list.Let say, as well as We have a list of number that looks like the one below.

# absroot.py

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

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

# absroot.py

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

watch the output.

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

We can also use the Python list comprehension.

# absroot.py

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

Python abs() with different format numbers

watch the below example.

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

watch the below output.

➜  pyt python3 absroot.py
96.05
12
12
15
➜  pyt

How to calculate absolute value in Python?

The abs() method of Python’s standard library data returns the absolute data value of the specified number. Absolute data value of a number is the data value without considering its sign. Hence absolute of 10 is 10, -10 is also 10. If the number is a complex number, abs() data returns its magnitude.

Example

>>> abs(11.11)
11.11
>>> abs(-11.11)
11.11
>>> abs(2+3j)
3.605551275463989
>>> abs(3-6j)
6.708203932499369
>>> abs(3-4j)
5.0

Summary:

  • Abs() is a built-in method accessible with python, as well as it will return you the absolute data value for the input specified.
  • data Value is users an input value to be specified to abs() to retrieve the absolute data value. like as an integer data, a float data value, or a any type of the complex number.
  • The abs() method extract’s single parameter, i.e. the value you want to retrieve the data absolute.
  • The simple abs method data returns the absolute data value for the specified number.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Rendering Lists in VueJs Example
  2.   Like Dislike system with PHP and MySQL
  3.   Laravel 6 Show images from storage folder
  4.   Class 'PhpParser\Parser\Php7' not found
  5.   how to check empty array in laravel controller?
  6.   Laravel Clear Cache Using Artisan Command (CLI)
  7.   WordPress AJAX URL GET POST Call PHP Function
  8.   Download File from URL Using PHP
  9.   PHP Object Cloning Magic Methods Example
  10.   Simple DateTime range with two inputs Example