Python abs() Function Example
Let's look at a few more examples of using the abs() function with different number types.
abs() Function with an Integer Argument:
When you pass an integer to the abs() function, it returns the absolute value of that integer.
Python
num1 = -10
num2 = 25
print(abs(num1))
print(abs(num2))

You can also try this code with Online Python Compiler
Run Code
Output:
10
25
In this example, we have two integer variables, "num1" with a value of -10 & "num2" with a value of 25. The abs() function returns 10 for "num1" since the absolute value of -10 is 10.
For "num2", the abs() function returns 25 because the absolute value of a positive number is the same as the original number.
abs() Function with a Floating-Point Number:
The abs() function can also handle floating-point numbers. It returns the absolute value of the given float.
Python
num1 = -3.14
num2 = 2.718
print(abs(num1))
print(abs(num2))

You can also try this code with Online Python Compiler
Run Code
Output
3.14
2.718
In this example, we have two floating-point variables, "num1" with a value of -3.14 & "num2" with a value of 2.718. The abs() function returns 3.14 for "num1" since the absolute value of -3.14 is 3.14. For "num2", the abs() function returns 2.718 because the absolute value of a positive float remains the same.
Floating-point numbers can also be expressed in scientific notation using the "e" or "E" character. The abs() function handles these as well.
Python
num3 = -4.2e-3
print(abs(num3))

You can also try this code with Online Python Compiler
Run Code
Output:
0.0042
Here, "num3" has a value of -4.2e-3, which is equivalent to -0.0042. The abs() function returns 0.0042, the absolute value of -4.2e-3.
abs() Function with a Complex Number
In Python, complex numbers are written in the form "a + bj", where "a" is the real part, "b" is the imaginary part, & "j" represents the imaginary unit (square root of -1). The abs() function can also find the absolute value (magnitude) of a complex number.
Python
z1 = 3 + 4j
z2 = -2 - 5j
print(abs(z1))
print(abs(z2))

You can also try this code with Online Python Compiler
Run Code
Output:
5.0
5.385164807134504
For complex numbers, the abs() function calculates the magnitude using the formula: sqrt(a^2 + b^2), where "a" is the real part & "b" is the imaginary part.
In the example above, "z1" has a real part of 3 & an imaginary part of 4. The abs() function calculates the magnitude as sqrt(3^2 + 4^2), which equals 5.0.
Similarly, for "z2", which has a real part of -2 & an imaginary part of -5, the abs() function computes the magnitude as sqrt((-2)^2 + (-5)^2), resulting in 5.385164807134504.
The abs() function returns the magnitude as a floating-point number.
Time-Distance calculation using Python abs() Function
One practical application of the abs() function is in calculating the distance between two points or the time difference between two events.
Let's say you have two points on a 2D plane, (x1, y1) & (x2, y2). You can find the Manhattan distance (the distance measured along the axes) between these points using the abs() function.
Python
x1, y1 = 3, 4
x2, y2 = -1, 6
distance = abs(x2 - x1) + abs(y2 - y1)
print(distance)

You can also try this code with Online Python Compiler
Run Code
Output:
6
In this example, we calculate the Delhi distance by taking the absolute difference between the x-coordinates (abs(x2 - x1)) & the absolute difference between the y-coordinates (abs(y2 - y1)), then summing up these differences.
Similarly, you can use the abs() function to find the time difference between two events.
Python
start_time = 10 # 10:00 AM
end_time = 13 # 1:00 PM
time_diff = abs(end_time - start_time)
print(time_diff)

You can also try this code with Online Python Compiler
Run Code
Output:
3
Here, we have a start time of 10:00 AM (represented as 10) & an end time of 1:00 PM (represented as 13). By taking the absolute difference between these times using abs(end_time - start_time), we get the time difference of 3 hours.
Frequently Asked Questions
Can the abs() function handle negative numbers?
Yes, the abs() function returns the absolute value of a number, which is always positive, regardless of whether the input is positive or negative.
Does the abs() function work with complex numbers?
Yes, the abs() function can calculate the magnitude (absolute value) of a complex number, which is the distance from the origin to the point representing the complex number on the complex plane.
Is the abs() function the same as the math.fabs() function?
The abs() function & the math.fabs() function both return the absolute value of a number, but the abs() function is built-in & can handle different numeric types (int, float, complex), while math.fabs() is specific to floating-point numbers.
Conclusion
In this article, we learned about the Python abs() function & how it can be used to calculate the absolute value of numbers. We discussed the syntax of the abs() function & saw examples of using it with integers, floating-point numbers, & complex numbers. We also used a practical application of the abs() function in calculating distances & time differences. The abs() function is a versatile tool that can be used in various mathematical & real-world scenarios.
You can refer to our guided paths on Code 360. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.