Python abs() Function: Absolute Value Examples
Python abs()
Python abs() is a built-in function available with the standard library of python. It returns the absolute value for the given number. Absolute value of a number is the value without considering its sign. The number can be integer, floating point number or complex number. If the given number is complex, then it will return its magnitude.
Syntax
abs(value)
Parameters (value)
The input value to be given to abs() to get the absolute value. It can be an integer, a float, or a complex number.
Return Value:
It will return the absolute value for the given number.
- If the input is an integer, the return value also will be an integer.
- If the input is a float, the return value will also be float.
- If the input is a complex number, the return value will be the magnitude of the input.
Examples
Code Example 1: Integer and Float number
To get the absolute value of an integer and float number check this code:
# testing abs() for an integer and float int_num = -25 float_num = -10.50 print("The absolute value of an integer number is:", abs(int_num)) print("The absolute value of a float number is:", abs(float_num))
Output:
The absolute value of an integer number is: 25 The absolute value of a float number is: 10.5
Example 2: Complex Number
To get absolute value of complex number
# testing abs() for a complex number complex_num = (3+10j) print("The magnitude of the complex number is:", abs(complex_num))
Output:
The magnitude of the complex number is: 10.44030650891055
Summary
- Abs() is a built-in function available with python, and it will return you the absolute value for the input given.
- Value is an input value to be given to abs() to get the absolute value. It can be an integer, a float, or a complex number.
- The abs() method takes one argument, i.e. the value you want to get the absolute.
- The abs function returns the absolute value for the given number.