In Python, you can find the absolute value of a number with the built-in abs() function.
For example, let’s figure out the absolute value of the number -10:
print(abs(-10))
Output:
10
This is a quick but comprehensive guide to finding absolute values in Python. This guide teaches you what is an absolute value and how to find it using Python. Also, you will learn what the special method __abs__() does.
What Is an Absolute Value?
In mathematics, the absolute value of a number is the number itself without the possible negative sign.
A more proper way to express it is that the absolute value of a number is its distance from the origin.
The absolute value of number X is denoted with |X|, such that:
- |X| = X if X is a positive number.
- |X| = -X if X is a negative number.
For example:
- The absolute value of -10 is 10. This is because |-10| = –10 = 10.
- The absolute value of 5 is 5. This is because |5| = 5.
How to Find Absolute Value in Python?
Python makes finding absolute values really easy. All you need to do is call the built-in abs() function on a number.
abs(number)
This returns the absolute value of the number.
You can use the abs() function with both real numbers as well as imaginary numbers.
1. Absolute Value of a Number
You can use the abs() function to find the absolute value of a number in Python. In other words, you can call the method on integers and floats.
For example, let’s call the abs() function on a float:
print(abs(-3.1415))
Output:
3.1415
As another example, let’s call the abs() function on an integer:
print(abs(-3))
Output:
3
Example
Given a list of numbers, print out all the numbers’ absolute values.
Solution:
Let’s use a for loop to print a list of numbers as a list of absolute values of the numbers:
nums = [1, -5, 2, -10, -6, -100, -3, 12] for number in nums: print(abs(number))
Output:
1 5 2 10 6 100 3 12
2. Absolute Value of a Complex Number
Absolute values are traditionally referred to as the non-negative counterparts of numbers.
But absolute values have meanings in other mathematical settings. The most common example is the absolute value of an imaginary number (the number system in which the square root of -1 is valid).
In case you’re unfamiliar with complex numbers, you can skip this section!
Similar to real numbers, the absolute value of a complex number is associated with the distance from the origin.
When dealing with complex numbers, the absolute value is the distance from the imaginary number to the origin in the complex plane.
In Python, imaginary numbers are denoted with j, which is the square root of -1.
Example
For example, let’s calculate the absolute value of 1 – j in the imaginary number space:
print(abs(1 - 1j))
Output:
1.4142135623730951
This result is obtained with the Pythagorean theorem where a = -1 and b = 1.
What Is the __abs__() Method in Python
Whenever you call abs() on a number in Python, you’re actually calling the __abs__() method of the class behind the scenes.
You can even try it yourself:
print(abs(-10)) print((-10).__abs__())
Output:
10 10
This suggests that the int type has a method called __abs__() somewhere in the implementation code. And that’s indeed the case.
More importantly, this type of special method is something you can add to your custom classes as well. In other words, you can specify what happens when you call the abs() function on a custom object by implementing the __abs__() method in the class definition.
Example
class numstr: def __init__(self, value): self.value = value def __abs__(self): absolute = self.value.replace("minus", "") return absolute
Let’s test the class by specifying some positive and negative numbers as strings and taking their absolute values:
v1 = numstr("minus three") v2 = numstr("ten") v3 = numstr("minus five") print(abs(v1)) print(abs(v2)) print(abs(v3))
Output:
three ten five
The abs() method removes the “minus” from the beginning of the number strings!
This just shows you how the special __abs__() method works and that you can customize what happens when abs() is called on an object.
Find out more about the __abs__() method in Python.
Summary
Today you learned how to calculate the absolute value in Python.
To take home, simply use the built-in abs() function by passing the number as an argument to the function.
You can calculate the absolute value for both real and imaginary numbers in Python.
Thanks for reading. Happy coding!