Python Absolute Value
A common question that comes in is "How do I find absolute value in Python?". This comes up a lot. There is a built-in function that handles this. You can just use the abs() function to convert numbers to their absolute value. The abs() function takes only one argument. It will return the absolute value of the number you pass to it.
Quick Answer - Just do this::
print(abs(-25))
For more detailed information, keep reading or just watch the video below.
The absolute value of a number is basically that numbers distance from zero. It represents the magnitude of the number. The absolute value of a positive number is itself. The absolute value of a negative number can be found by just removing the '-' sign and making it a positive number. Consider these examples. The absolute value of 5 is five. The absolute value of -5 is 5.
Examples
Here is how you can find absolute value in python. Just use the abs() function. Python makes this super easy. Here is the simplest example I could think of.
x = abs(-3)
print(x)
We can also just print them directly without using variables.
print(abs(25))
print(abs(-25))
The original variable won't be affected by the abs() function. The absolute value is returned while the original is left intact.
x = -5
y = abs(x)
print("Original Value: " + str(x))
print("Absolute Value: " + str(y))
The absolute value of zero is going to be zero. Anything under zero will need to be changed to positive. Anything zero and above will just return the same value without any change. There isn't a whole lot else to say about that but here is an example.
abs(0)
The abs() Function and Variable Types
The abs() function is somewhat versatile and can handle multiple different types of variables. It can handle integers and floating point numbers. This is expected. I couldn't imagine implementing a function like this and not supporting those types. It can also handle complex numbers which is nice. I can't think of a case where I would actually need to do this with a complex number but it is nice to know that I can if I want to. The functionality is there. It is just an added bonus. One limitation to the abs() function is that it can't take string input. This is not a surprise but isn't a huge drawback either. It is normal to have to convert strings to numbers and numbers to strings. This is pretty standard in Python.
Here are a few examples of finding the absolute value of different types of variables. We also show the returned type for each.
Here is an example using a floating point number:
float = -78.19
print('Absolute value: ', abs(float))
print('Type: ', type(abs(float)))
Here is an example using an integer:
int = -56
print('Absolute value: ', abs(int))
print('Type: ', type(abs(int)))
Here is an example using a complex number:
complex = (7 - 9j)
print('Absolute value:', abs(complex))
print('Type: ', type(abs(complex)))
What about fabs()?
The fabs() function will also find the absolute value of a number. It will then return that number as a float. If it can't return it as a float it will throw an exception. Also note that it doesn't work with complex numbers.
Here is an example using an integer, a float, and a complex number. First we do this with the abs() function and check the type of each return value. The returned type will match the input. We then do the same with the fabs() function and show that the returned type is a float each time except for the last one. The last one will fail and throw an exception because fabs() can't handle complex numbers.
type(abs(-2))
type(abs(-2.0))
type(abs(3+4j))
type(math.fabs(-2))
type(math.fabs(-2.0))
type(math.fabs(3+4j))
Python Absolute Value Without abs()
WARNING - Don't do this. It is a bad idea. Just use the abs() function. It is much better.
Let's say you decide that you want to find the absolute value of a number but you don't want to use the abs() function for some reason. Well, you most definitely can do this. It isn't hard to do at all but it does take a bit more effort. You normally wouldn't do this because it is a waste of time since you are basically reinventing the wheel. The abs() function is easier, more flexible, and almost certainly faster.
You very well may have a legitimate reason to actually do this. The first reason that you might want to try it out because you are just curious. You might view it as a personal challenge. You might just view it as something educational that will help you learn. Another likely reason to try this is because it might be a problem on a test. The rules may state that you must implement it manually without using the library functions. You may be taking a test for school or an interview for a job. In either case it would help to know how to do this ahead of time. Keep in mind though, if you ever do need to do this for a test or interview, don't do it manually unless you are required to. Failing to use the abs() function would make you appear less competent.
def absolute_value(x):
if x < 0:
return -x
else:
return x
print(absolute_value(-25))
print(absolute_value(86))
print(absolute_value(-3))
print(absolute_value(17))
print(absolute_value(0))
As you can see this isn't hard to do. You can basically implement the absolute value function yourself. Remember, just because you can doesn't mean you should. It doesn't hurt to know how to implement this manually but don't use this in production.
A Tangent - Absolute Value Uses
Calculating absolute value is a pretty standard mathematical operation. It's just something that you should know about. You may wonder if it has a real purpose. Is this really a useful skill? It turns out that it is. Absolute value is important enough that there is a library function for it. It isn't just here to be complete. It is there because it is practical and has many real world uses cases.
Finance
One example would be in finance. You might represent monetary transactions as positive and negative values. These would represent credit and debit transactions. In some cases, you may just want to see how much money has been transferred irrespective of which way it was transferred. To do this you would just find the absolute value. Python is an extremely popular language and is often used in finance. Chances are, someone has used it in exactly this way more than once.
Stock Prices
Another example might be comparing stock prices. Let's say if you were trying to measure volatility. You might have a bunch of values for a stock at different times. You would measure how much a stock has changed. You wouldn't necessarily care if it was going up or down. You just want to know how much it has changed each time. You might be comparing the change to some threshold or rule. This is exactly what you would need to find the absolute value for. Chances are pretty good that you would be doing all of this in Python. Even if you were using another language though, you would still probably be using a built in library function to calculate absolute value.