Python Absolute Value | abs() Function With Examples

Python absolute value means removing any negative sign in front of a number and thinking of all numeric values as positive (or zero). However, in Python, we can get the absolute value of any number by inbuilt functions, which are abs() and fabs().

In this article, you will learn about the abs() function. The main purpose of this function is to return the absolute value of the number(variable) passed as an argument to it.

How Can We Find Absolute Value Using abs() Function?

The abs() function in Python is used for obtaining the Python absolute value or the positive value of a number.

We can get the absolute value of an integer, complex number, or a floating number using the abs() function. If the argument x (integral value) is a float or integer, then the resultant absolute value will be an integer or float, respectively.

If the argument x (integral value) is a complex number, the return value will only be the magnitude part that can be a floating-point.

Python Absolute Value Function abs() function works on?

The abs() function works on the following numbers:
1. Integers, for example 6, -6, 1 etc.
2. Floating point numbers, for example, 5.34, -1.44 etc
3. Complex numbers, for example 3+4j, 4+6j etc.

Basic Syntax of abs() Function in Python

The syntax of abs() function is:

abs( x )

where x can be a number or expression that evaluates to a number.

Parameters

This function only takes one argument: the number whose absolute value you want to find out. This argument’s data type can be any of the following listed.

  • Integer
  • Float
  • Complex Number

Return Value

abs() function returns absolute value for the given number.

  • For an integer value, it will return an integer
  • For float value, it will return a floating-point value.
  • And if the complex number, it will return the magnitude part, which can also be a floating-point number.

abs() Function Compatibility

These absolute value function abs() is available and compatible with both Python 2.x and 3.x

Python Versionabs() defined
Python 2.xYes
Python 3.xYes

Absolute Value Examples using abs() Function in Python

We can get Python absolute value of generally three data types:

  • int
  • float
  • complex

Note: In Python, the imaginary part of the complex number is denoted by j.

Example 1: Python program to return the absolute value of an integer-

int_num = -40

print("The absolute value of an integer number is:", abs(int_num))

Output:

The absolute value of an integer number is: 40

Example 2: Python program to return the absolute value of a floating-point number-

float_num = -65.50

print("The absolute value of a float number is:", abs(float_num))

Output:

The absolute value of a float number is: 65.5

Example 3: Python program to return the absolute value of a complex number-

This example shows the magnitude of a complex number.

#A Complex Number
complexNumber = 12 + 9j
 
#Finding Magnitude of the Complex Number
magnitude = abs(complexNumber)
 
#Printing magnitude to console
print("The Magnitude of " + str(complexNumber) + " is " + str(magnitude))

Output:

The magnitude of (12+9j) is 15.0

Example 4: Python absolute value of a list

inputList = [12, 11, -9, 36]
mappedList = map(abs, inputList)
print(list(mappedList))

Output:

[12, 11, 9, 36]

Do abs() function Work for String Data Type?

If you provide any other argument of type other than a number, you will get TypeError.

Example:

str_abs = 'PythonPool'
absVal = abs(str_abs)

print("Absolute value of this string is", "is", absVal)

Output:

You will get an error like

Python Absolute Value Error String

Is There Any Other Function to Get The Absolute Value in Python?

Yes, We can get the absolute value of a number using the fabs() function. fabs() method is also a built-in function but it is defined in the math module, so to use fabs() method, we need to import math module first.

Syntax:

math.fabs(n)

Difference between abs() and fabs() methods

There are mainly two differences between abs() and fabs() methods,

  1. abs() method is a standard built-in method, for this, there is no need to import a module. But, the fabs() method is defined in the math module, for this, we need to import the math module first.
  2. abs() method returns either an integer value or a float value based on given number type. But, fabs() method returns only float value, no matter given number is an integer type or a float type

Must Read:

Conclusion

We learned to find the Python absolute value of a number or a numeric expression using abs() function of Python.

With absolute values, we convert negative numbers to positive ones. This helps when computing “distances” from positions. With abs, a built-in, we do this with no extra code.

If you still have any doubt or suggestions do comment down below.

Happy Coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments