Table of contents
1.
Introduction
2.
Python abs() Function Syntax
2.1.
Python
3.
Python abs() Function Example
3.1.
Python
3.2.
abs() Function with a Floating-Point Number:
3.3.
Python
3.4.
Python
3.5.
abs() Function with a Complex Number
3.6.
Python
3.7.
Time-Distance calculation using Python abs() Function
3.8.
Python
3.9.
Python
4.
Frequently Asked Questions
4.1.
Can the abs() function handle negative numbers?
4.2.
Does the abs() function work with complex numbers?
4.3.
Is the abs() function the same as the math.fabs() function?
5.
Conclusion
Last Updated: Jun 20, 2024
Medium

abs in Python

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python, the abs() function is used to get the absolute value of a number. An absolute value is the distance of a number from 0 on the number line, regardless of whether it is positive or negative. The abs() function is a built-in Python function that works with integers, floating-point numbers, & complex numbers. 

abs in Python

In this article, we'll learn the syntax of the abs() function, with examples of how to use it with different number types. We will also see how to calculate Time-distance using this function.

Python abs() Function Syntax

The basic syntax of the Python abs() function is:

abs(num)


Here, "num" is the number you want to find the absolute value of. It can be an integer, floating-point number, or complex number.

The abs() function returns the absolute value of the given number. For positive numbers, the absolute value is the same as the original number. For negative numbers, the absolute value is the positive version of the number. And for complex numbers, the abs() function returns the magnitude of the complex number.

Here's a simple example:

  • Python

Python

x = -5

print(abs(x)) 
You can also try this code with Online Python Compiler
Run Code


Output

5


In this code, we have a variable "x" with the value -5. When we pass "x" to the abs() function, it returns the absolute value of -5, which is 5.

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

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

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass