One of the reasons for the popularity of Python is its built-in functions. Python provides a large number of built-in functions. The is an absolute value function to find the Python abs Function of any number. While working with distance calculations we always need a positive number. As distance is always positive. Another example of the usage of absolute values is speed calculations. Let’s take a look at how to use the Python abs Function.
x = -9 print(abs(x)) |
The output is shown below.
Distance example
The take a look at the distance formula. If we take the absolute value of the difference then we
Don’t need to take the square and then square root.
point1 = [4, 0] point2 = [6, 6] dist = ((point1[0]-point2[1]))+((point1[0]-point2[1]))print(dist) print(abs(dist)) |
The output is as follows.
Complex number examples
To find the magnitude of a complex number absolute value function can be used. Let’s take a look at an example.
complex_num = 5 – 5j print(complex_num) print(abs(complex_num)) complex_num2 = 7 + 4j print(complex_num2) print( abs(complex_num2)) |