ABS in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

The standard Python library has a function abs(), which is short for "absolute" value. It returns the value given as input without the sign with it, that is, the positive value.

Syntax of abs() in Python

The syntax of abs in Python is as follows:

Here the value passed as input to the abs() function is the number whose absolute value is needed.

Parameters of abs() in Python

The abs function takes the following parameter:

  • value: It can be an integer, float, or complex type.

Return values of abs() in Python

Return Type: int or float or complex

Since the only inputs it takes are of two types, either integer/float value or complex numbers, the return value of the abs() function is also of 2 types :

  1. If the input is integer or floating-point, then the return value is the absolute value (magnitude, or the same value without the sign)

  2. If the input is a complex number, then abs() returns the magnitude of the complex number. It can be an integer or a floating point.

Example

Output:

Explanation: As you can see here, the negative values, whether integer(-21) or float (-16.51) have been returned as positive(float returned as: 16.51, int returned as 21) from the abs() function in python, their signs have been removed.

What is the abs() function in Python?

The function abs() is just short for absolute which returns the absolute value of the input passed in it. What does that mean? Absolute value means the value of the input passed in, without its sign or essentially the value with the sign removed or can say positive value.

Real-world Analogy

Suppose your house and a few other landmarks are located on a number line. Your house is at point 4, your school is at point 2, and your favourite cafe is at point 7. To calculate the distance from your school to the cafe, you might write: "school - cafe". Right? But doing so returns a negative value: -5. The distance can never be negative!

Or maybe, you know that a particular mathematical calculation might result in a negative value, such as calculating the distance, area, etc. But you aren't interested in the fact that it is negative, you're only concerned with the value, as in just "5" according to the last example. That is where the abs() function comes in handy. It is used widely in numerical computations while programming.

Notes

What happens if any other data type is passed?

Output

As we can see, we passed the 'str' data type to abs() function, and the result is an error - bad operand type. That's the exact output we will get if any datatype other than the expected ones is given as input to the abs() function.

What is the magnitude of a complex number?

In a plane of complex numbers, magnitude is the distance between the origin and the point on the plane. So for a complex number a + bi, its magnitude is :

a2+b2\sqrt{a^2+b^2}

Did you know?

We can also use another function to calculate the absolute value. It is the fabs() function. This function is a part of the math module in python, and it must be imported from there before use.

There are two differences between the fabs() and abs() function.

  • The abs() function is part of the standard template library of python, whereas fabs() has to be imported from the math module
  • fabs() will always return a float value, no matter what type of number is given as input. However, as we noticed above, the abs() function returns an integer if the input is an integer and a float if the input is float/complex.

More Examples

Example 1: Getting magnitude of a complex number

Output:

Explanation: The result of the complex number when it is passed to the method abs() in python is it's magnitude itself

32+(5)2\sqrt{3^2+(-5)^2}

Example 2: Evaluating Acceleration

How can the abs() function in python be helpful in this case? Suppose you're driving on the road, and you encounter a speed breaker. You now need to calculate the acceleration of your car at this point. When a car is slowing down, the value of acceleration is negative because current speed < previous speed! It would result in a negative value, but the abs() function in python would be used here to get just the magnitude of the acceleration.

Output:

Conclusion

  • The abs() function returns the absolute value of the input passed to it.
  • Only 1 parameter is passed to the abs() function.
  • If a negative number is put in as input, the result is the same value but positive.
  • The same number is returned if a positive number is passed as input.
  • If the input is a complex number, then the magnitude of the number is returned.
  • The abs() function can be used wherever you expect a calculation to return a negative number but require a positive number.
  • You can make use of the abs() function in python when writing code for the following scenarios:
    • Generally used for determining the magnitude of a number.
    • Used for distance measurements (like the example in the beginning).
    • Determining speed from velocity (velocity is just speed with direction so that it can have different signs).
    • Financial scenarios: If some money is being credited to your account, then it can be written as positive, while if some money is debited, then it can be written as negative. However, the transaction process must be positive to determine the money that has been moved; hence, the abs() value can be used.