Unlock the Power of Absolute Values
What Does the abs() Function Do?
The abs()
function returns the absolute value of a given number, which is the distance of that number from zero. Additionally, it works with complex numbers, returning their magnitude.
The Syntax and Parameters
The abs()
function takes a single argument, num
, which can be an integer, floating-point number, or complex number. The syntax is straightforward:
abs(num)
What to Expect: The Return Values
The abs()
function returns the absolute value of the given number, which depends on its type:
- Integers: The integer absolute value is returned.
- Floating-point numbers: The floating absolute value is returned.
- Complex numbers: The magnitude of the number is returned.
Putting it into Practice
Let’s see the abs()
function in action with some examples:
Example 1: Get the Absolute Value of a Number
num = -5
result = abs(num)
print(result) # Output: 5
Example 2: Get the Magnitude of a Complex Number
num = 3 + 4j
result = abs(num)
print(result) # Output: 5.0
Taking it Further
Want to learn more about working with absolute values in popular libraries? Check out our articles on Pandas abs() and NumPy absolute() for more examples and insights!