Python abs() Function: Absolute Value Examples
Abs in Python ()
The abs() function is a built-in feature of Python and is accessible through the language’s standard library. It gives the absolute value corresponding to the number that was given. The value of a number without taking into account its sign is known as its absolute value. It’s up to you whether this number is an integer, a floating point number, or a complex number. If the number that was provided is complex, then it will return the magnitude of that number.
The input value, value, that will be provided to the abs() function in order to obtain the absolute value. It could be either an integer or a floating point number, or even a complex number.
Value Returned: The function will return the absolute value for the integer that was given.
If the value that is inputted is an integer, then the value that is returned will also be an integer.
If the value that is input is a float, then the value that is returned will also be a float.
The magnitude of the input complex number will be the result returned to you if the input is a complex number.
Code One example each of an integer and a floating-point number
Check out the following code to determine the absolute value of any integer or float number:
# testing the abs() function with a float and an integer
int num = -25
float num = -10.50
print(“The absolute value of a number is:”, abs(int num));
print(“The absolute value of a float number is:”, abs(float num)), “The absolute value of a float number is:”
The value of an integer, expressed in absolute terms, is 25.
10.5 is the value that is considered to be absolute for a float.
Example 2: Complex Number
For the purpose of obtaining the absolute value of a complicated number
# putting abs() through its paces with a complicated number
complex num = (3+10j)
output: print(“The magnitude of the complex number is:”, abs(complex num)) Input: complex num
10.44030650891055 is the magnitude of the complex number.
Python is equipped with a built-in function called abs() that, when called, will tell you the absolute value of whatever you pass it as input.
The absolute value can be obtained by passing the value value as an input to the abs() function. It could be either an integer or a floating point number, or even a complex number.
The abs() method only accepts a single argument, which is the value whose absolute you wish to obtain.
The abs function calculates and returns the number’s value in its most fundamental form, the absolute value.