In Python or any other Programming language, Python absolute value means to remove any negative sign in front of a number and to think 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 )
x can a number, or expression that evaluates to a number.
This function only takes one argument which is the number whose absolute value you want to find out. The data type of this argument can be any one of the following listed.
- Complex Number
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
This absolute value function abs() function is available and compatible with both Python 2.x and 3.x
|Python 2.x||Python 3.x|
Absolute Value Examples using abs() Function in Python
We can get Python absolute value of generally three data types:
Note: In Python 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))
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))
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))
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))
[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.
str_abs = 'PythonPool' absVal = abs(str_abs) print("Absolute value of this string is", "is", absVal)
You will get an error like
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.
Difference between abs() and fabs() methods
There are mainly two differences between abs() and fabs() methods,
- 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.
- 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
- Python Data Types
- Implementation of Stack in Python
- Substring operations in Python
- Best Book to Learn Python in 2020
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.