But, fabs() method returns only float value, no matter given number is an integer type or a float type abs() method returns either an integer value or a float value based on given number type.But, the fabs() method is defined in the math module, for this, we need to import the math module first. abs() method is a standard built-in method, for this, there is no need to import a module.There are mainly two differences between abs() and fabs() methods, Syntax: math.fabs(n) Difference between abs() and fabs() methods 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. Yes, We can get the absolute value of a number using the fabs() function. You will get an error like Is There Any Other Function to Get The Absolute Value in Python? Print("Absolute value of this string is", "is", absVal) If you provide any other argument of type other than a number, you will get TypeError. Output: Do abs() function Work for String Data Type? The magnitude of (12+9j) is 15.0 Example 4: Python absolute value of a list inputList = Print("The Magnitude of " + str(complexNumber) + " is " + str(magnitude)) This example shows the magnitude of a complex number. The absolute value of a float number is: 65.5 Example 3: Python program to return the absolute value of a complex number.
Print("The absolute value of a float number is:", abs(float_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 an integer number is:", abs(int_num)) Example 1: Python program to return the absolute value of an integer- int_num = -40 Note: In Python imaginary part of the complex number is denoted by j. We can get Python absolute value of generally three data types: This absolute value function abs() function is available and compatible with both Python 2.x and 3.x Python 2.xĪbsolute Value Examples using abs() Function in Python And if the complex number it will return the magnitude part which can also be a floating-point number.For float value, it will return a floating-point value.For an integer value, it will return an integer.The data type of this argument can be any one of the following listed.Ībs() function returns absolute value for the given number. This function only takes one argument which is the number whose absolute value you want to find out. Where x can a number, or expression that evaluates to a number. The syntax of abs() function is: abs( x ) Complex numbers, for example 3+4j, 4+6j etc. Floating point numbers, for example, 5.34, -1.44 etcģ. The abs() function works on the following numbers:Ģ. Python Absolute Value Function abs() function works on? 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. If the argument x (integral value) is a float or integer, then the resultant absolute value will be an integer or float respectively. We can get the absolute value of an integer, complex number or a floating number using the abs() function. The abs() function in Python is used for obtaining the Python absolute value or the positive value of a number. How Can We Find Absolute Value Using abs() Function? Difference between abs() and fabs() methods.Is There Any Other Function to Get The Absolute Value in Python?.Do abs() function Work for String Data Type?.Example 4: Python absolute value of a list.Example 3: Python program to return the absolute value of a complex number.Example 2: Python program to return the absolute value of a floating-point number.Example 1: Python program to return the absolute value of an integer.Absolute Value Examples using abs() Function in Python.Basic Syntax of abs() Function in Python.Python Absolute Value Function abs() function works on?.