Python abs() is a standard built-in python function, which is used to returns the absolute value of the given number. If the number is complex, the abs() method returns its magnitude.
Here at LinuxAPT, as part of our Scripting Support Services, we regularly help our Customers to perform related Python fuction queries.
In this context, we shall look into abs() function in Python.
The abs() function returns the absolute value of the given number.
It's syntax is given below:
abs(num)
abs() Parameters involves a single argument.
num - a number whose absolute value is to be returned. The number can be:
1. Lets take a look at the below function:
number = -20
absolute_number = abs(number)
print(absolute_number)
The output will be:
20
2. Get absolute value of a number:
# random integer
integer = -20
print('Absolute value of -20 is:', abs(integer))
#random floating number
floating = -30.33
print('Absolute value of -30.33 is:', abs(floating))
The output will be:
Absolute value of -20 is: 20
Absolute value of -30.33 is: 30.33
3. Get magnitude of a complex number:
# random complex number
complex = (3 - 4j)
print('Magnitude of 3 - 4j is:', abs(complex))
It's output will look like:
Magnitude of 3 - 4j is: 5.0
This article covers how to use Python abs() function. In fact, The Python abs() method calculates the absolute value of a number. The abs() method takes in one parameter: the number whose absolute value you want to calculate.