Methods and Functions

Python abs()

Python abs() returns the absolute value of any given number. It is possible for the number to be an integer, a floating-point number, or a complex number. We can get the python absolute value of any number by utilizing one of the built-in functions, Python abs ().

The absolute value of a number in any programming language is the value without regard to its sign. It converts all numeric numbers to positive (or zero).

python absolute value

Definition of  Python absolute value

  • Python abs() built-in function is used to return an absolute value of any numeric value input as an argument to the function.
  • The Python abs() function is responsible to return an absolute value of the given number and if the number is a complex number, then it returns its magnitude.

Python abs() Syntax

As Python abs() is a built-in function we do not need to import any math module to be able to use this function. The syntax for Python abs() is as follows:

Parameters of absolute value python

The abs() method accepts only a single parameter:

num = can be an integer, floating-point number, complex number

Return value from abs()

The python abs() function returns the absolute value of the passed number.

  • For integers as inputs – an integer absolute value is returned
  • For floating-point numbers as inputs – a floating absolute value number is returned
  • If the inputs are complex numbers – the magnitude of the complex numbers is returned. Eg. Complex number = a+bj, Magnitude = √(a2+b2)

Example 1: Get the python absolute value of an integer

                    

# Python program to illustrate abs() on integers
num1 = 10
print('Absolute value of 10 is:', abs(num1))

num2 = -38
print('Absolute value of -38 is:', abs(num2))

num3 = 0
print('Absolute value of 0 is:', abs(num3))

Output

                    

Absolute value of 10 is: 10
Absolute value of -38 is: 38
Absolute value of 0 is: 0

Example 2: Get absolute value in python of a floating-point number

                    

# Python program to illustrate abs() on floating-point numbers
num1 = 10.3428
print('Absolute value of 10.342 is:', abs(num1))

num2 = -38.451
print('Absolute value of -38.845 is:', abs(num2))

num3 = -123.569443
print('Absolute value of -123.569443 is:', abs(num3))

Output

                    

Absolute value of 10.342 is: 10.3428
Absolute value of -38.845 is: 38.451
Absolute value of -123.569443 is: 123.569443

Example 3: Get the magnitude of a complex number

                    

# Python program to illustrate abs() on complex numbers
complex1 = (10 + 5j)
print('Absolute value of 10+5j is:', abs(complex1))

complex2 = (-8 + 4j)
print('Absolute value of -8+4j is:', abs(complex2))

complex3 = (2 - 7j)
print('Absolute value of 2-7j is:', abs(complex3))

complex4 = (-15 - 3j)
print('Absolute value of -15-3j is:', abs(complex4))

Output

                    

Absolute value of 10+5j is: 11.180339887498949
Absolute value of -8+4j is: 8.94427190999916
Absolute value of 2-7j is: 7.280109889280518
Absolute value of -15-3j is: 15.297058540778355

Example 4: Python absolute value for a list & string

Python’s abs() function works only for a numeric type of parameter. If we want the absolute values of a list passed as an argument to the abs() function contains numbers, we will first have to map every element in the list to the abs() function and then print the output. This can be done using the Python map() function. If you try to find the absolute value of a text, character, or a string, the Python interpreter will throw a TypeError exception.

                    

num_list = [-43, -5.76, 6+2j, -283]
mappedlist = map(abs, num_list)
print('Absolute value of a list is:', list(mappedlist))
print('')

# throws an error
txt = 'Hello'
print('Absolute value of a string:', abs(txt))

Output

                    

Absolute value of a list is: [43, 5.76, 6.324555320336759, 283]

Traceback (most recent call last):
File "", line 8, in 
TypeError: bad operand type for abs(): 'str'

Example 5: abs() python with different format numbers

Python abs() also works with different formats of numeric values such as binary, hexadecimal, octal, exponential, etc.

                    

# exponential number format
x = 5e1/2
print('Absolute value of 5e1/2 is:', abs(x))

# binary number format
x = 0b0010
print('Absolute value of 0b0010 is:', abs(x))

# octal number format
x = 0o15
print('Absolute value of 0o15 is:', abs(x))

# hexadecimal number format
x = 0xA
print('Absolute value of 0xA is:', abs(x))

Output

                    

Absolute value of 5e1/2 is: 25.0
Absolute value of 0b0010 is: 2
Absolute value of 0o15 is: 13
Absolute value of 0xA is: 10

abs() python vs fabs()

Why does Python have 2 functions abs() and fabs() which perform the same functionalities? Both the Python abs() and Python fabs() function calculates the absolute value of the numeric argument. But the abs() and fabs() techniques differ primarily in two ways.

  1. Python abs() is a built-in function that does not require any module to be imported. Whereas the fabs() is defined in the math module and requires the math module to be imported before using the function.
  2. Python’s abs() function returns either an integer value or a float value based on the function input. Whereas, any type of the numeric value passed to the fabs() function will always return an absolute value which is a float value. The name ‘fabs’ is short for ‘float absolute value’.

FAQs on python absolute value

Q1. How do you get absolute value in Python 3?

The absolute value of a number in any programming language is the value without regard to its sign. We use Python’s abs() built-in function to return an absolute value of any numeric input passed as an argument to the function. It converts all numeric numbers to positive (or zero).

Syntax

Example

                    

int_num = -55
a = abs(int_num)
print('Absolute value of -55 is:', a)

flt_num = -23.45872
print('Absolute value of -23.45872 is:', abs(flt_num))

cmplx_num = 3 + 5j
print('Absolute value of 3+5j is:', abs(cmplx_num))

Output

                    

Absolute value of -55 is: 55
Absolute value of -23.45872 is: 23.45872
Absolute value of 3+5j is: 5.830951894845301

Q2. What is Zero’s absolute value?

A number’s absolute value is its distance from zero. Because 0 is zero units away from itself, its absolute value is zero. Let us look at an example to confirm this.

Example

                    

num1 = -0
print('Absolute value of -0 is:', abs(num1))

num2 = 0
print('Absolute value of 0 is:', abs(num2))

Output

                    

Absolute value of -0 is: 0
Absolute value of 0 is: 0

Q3. What is fabs in Python?

The fabs() method in Python is used to calculate the absolute value of a number as a float. Even if the parameter is an integer, math.fabs(num) will always return a floating-point number. This function, unlike Python abs(), always converts the value to a float value. The name ‘fabs’ is short for ‘float absolute value’. The fabs() method is defined in the math module and requires the math module to be imported before using the function.

Example

                    

# Python program to find absolute values using fabs()
from math import *

num1 = -10
print('Absolute value for -10 is:', fabs(num1))

num2 = -20.34
print('Absolute value for -20.34 is:', fabs(num2))
print('')

# fabs() does not compute absolute values of complex numbers 
# it returns TypeError
cmplx_num = 4 + 8j
print('Absolute value for 4 + 8j is:', fabs(cmplx_num))

Output

                    

Absolute value for -10 is: 10.0
Absolute value for -20.34 is: 20.34

Traceback (most recent call last):
File "", line 14, in 
TypeError: can't convert complex to float

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.