Python abs() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

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

What is Python abs() Function?

abs() is a mathematical function. 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, while if the given number is a complex number, then it returns its magnitude.

How Does the Python abs() Function Work?

The abs() functions take a single argument and return its absolute(magnitude) values. The absolute value of a number is its distance from zero on the number line, regardless of the direction. example - the distance of -5 from origin(zero) is 5, the distance of 3+4i from origin is 5.

Examples of Python abs() Function

  • Get absolute value of a number we can use abs in python to get absolute value of a number, as shown below:

Output

  • Get 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}

Output

Syntax of abs() in Python

The abs in python takes a single argument. It will take either an actual or complex number and return the absolute and magnitude values, respectively.

 y= abs(x)

Parameters of abs() in Python

The abs in python 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 abs in python takes are of two types, either integer/float value or complex numbers, the return value of the it is also of 2 types :

  1. If the input is integer or floating-point, then the return value is the absolute value (magnitude, or 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.

Exception of abs() function in Python

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 one is given as input to the abs() function.

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 in python 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; otherwise, float always.

Conclusion

  • The abs() function returns the absolute value of the input passed to it.
  • Only one parameter is passed to the abs() function.
  • If the input is a complex number, then the magnitude of the number is returned.
  • You can make use of the abs() function in Python when writing code for the following scenarios:
    • 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).

See Also