What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Calculate Absolute Value in Python Using abs() Function

  • Apr 30, 2022
  • 5 Minutes Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Shivali Bhadaniya
Calculate Absolute Value in Python Using abs() Function

Python, an interpreted language, is one of the most accessible programming languages because it has simplistic syntax and not complicated code. Due to its ease of usage and learning, Python codes can be written and executed more quickly than any other programming language. As python provides multiple in-build functions to make your programming easy, it is necessary for you to learn and understand them in order to make your coding efficient. To help you with the same, in this article, we will study how you can find the absolute value in python with various in-build methods and external functions along with its example and output. So, let's get started!

What Does the Python Absolute Value Do?

The Python programming language defines an absolute value function, which transforms negative numbers into positive ones. For any positive number, the absolute value is equal to the original number; however, for negative numbers, the absolute value is equal to (-1) multiplied by the negative number.

How to Find Python Absolute Value?

In Python, there are several ways to interact with positive and negative values. However, there are situations when we only need to ensure that our value does not have a negative sign. Below are two methods to find python absolute value:

Method 1: Using abs() function

Python provides a large collection of in-build methods especially when it comes to dealing with mathematical operations. abs() is one such in-build method in python that helps in converting negative integers into absolute values (without negative signs). To perform this conversion, abs() takes a single parameter, i.e., the number which needs to be converted into absolute value, and returns the positive integer without a negative sign. To understand the working of this method in detail, check out the example below:

For example:

no = -10
absolute_no = abs(no)
print(absolute_no)

 

Output:

10

 

In the above example, we saw the results to convert the whole number into absolute value. But what if the number is floating type? Check out the below example to answer your question:

For example:

no = -10.2
absolute_no = abs(no)
print(absolute_no)

 

Output:

10.2

 

In the case of a complex number, the abs() function return the magnitude of the number as shown in the below example:

For example:

no = (3-4i)
absolute_no = abs(no)
print(absolute_no)

 

Output:

5.0

 

Method 2: Using math.fabs() function

Just like the abs() function, Python also possesses the fabs() function which returns the absolute value for the argument you pass. As this module is from the "math" library, it is necessary to import the math library using the “import” keyword as shown in the below example.

For Example:

import math

no = 10
absolute_no = math.fabs(no)
print(absolute_no)

 

Output:

10.0

 

Looking at the above example, you’ll notice the key difference between the "fabs()" and "abs()" method. When it comes to the "abs()" method, the function returns the value as it is without the negative sign. But when working with the "fabs()" method, the function returns the floating-point value irrespective of the data type of the parameter you passed.

Conclusion

Sometimes we just need to utilize positive values, and the absolute value is a great tool for this. When you encounter an absolute value in a problem or equation, it signifies that whatever is included within it is always positive. Absolute values are frequently used in situations concerning distance, and they are sometimes used in combination with inequalities.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.