To get the absolute value of a number in Python use the
abs() function. The absolute value of a number is the number’s distance from 0. This makes any negative number positive, while positive numbers are unaffected. For example,
abs(-9) would return 9, while
abs(2) would return 2.
When you’re working in Python, you may want to return an absolute value. For instance, you may be building an app that sends speeding tickets to anyone driving too slowly or too quickly in a certain zone where the only thing that matters is the difference between the driver’s speed and the speed limit. In this case, you’ll only want a whole number representing the difference, even if the difference is negative—in other words, you’ll require an absolute value.
That’s where the
abs() built-in function comes in. The
abs() method can be used to calculate the absolute value of a particular number.
In this tutorial, we will discuss how to use the
abs() method in Python. We will also explore a few examples that illustrate the function in action.
Absolute Value Refresher
In math, an absolute value refers to how far away a number is from zero on the number line. Regardless of whether a number is positive or negative, that number’s absolute value will always be positive.
For example, the absolute value of
5, and the absolute value of
-5 is also
5. This is the case because both
-5 are five spaces away from
on the number line. Here’s an example number line to illustrate how this works:
As you can see,
5 spaces away from zero, and
5 is also
5 spaces away from zero.
Absolute value is an important concept in both mathematics and physics, and it has a wide variety of applications in these fields. Absolute values are also often used in data analysis.
Python Absolute Value
abs() method calculates the absolute value of a number. The
abs() method takes in one parameter: the number whose absolute value you want to calculate.
Here’s the syntax for the
Let’s use an example to illustrate how the
abs() method works. Let’s say that you are working for the local police department, and they ask you to build an app that calculates whether drivers on a certain road are due for a speeding ticket. On this road, drivers should be going at around the same speed as the average driver.
This is the case because there was a series of accidents on the road, and many of the accidents were due to excessive speed variability among drivers. The police department determined that going over 10 miles per hour (mph) faster or slower than the average driver on this road is presenting a hazard and is therefore due a ticket.
Here’s an example program that uses the average speed (in mph) of drivers and a specific driver’s current speed (in mph) to determine whether that driver is due a ticket:
average_speed = 50 current_speed = 38 difference_between_average_and_current_speed = average_speed - current_speed absolute_value = abs(difference_between_average_and_current_speed) print(absolute_value)
Our code returns:
Let’s break down how this program works. On the first line, we declared a variable that stores the average speed of cars on the road, which in this case was 50 mph. Then, we declared a variable that stores a specific driver’s current speed—38 mph in this case.
On the next line, we calculated the difference between the specific drivers’ current speed and the average speed of cars on the road. Then, we took this value and used the
abs() method to get the absolute value.
Technically, the difference between the driver’s speed and the average speed is -12 mph, but we only wanted to know the difference between the specific driver’s speed and the average speed of cars on the road. So, we used abs() to convert our value into an absolute value, and our program returned
Absolute values are commonly used in distance calculations. For example, let’s say that we are trying to travel somewhere 20 miles away from our home, and we accidentally travel 31 miles. If we wanted to know how many miles we have left until we reach our destination, we would end up with a negative number. In order to convert that negative number to a positive one, we could use
Here’s the code for this example in action:
miles_from_home = 20 travelled = 31 miles_left = miles_from_home - travelled print(abs(miles_left))
Our code returns:
If we had not specified
abs(), our program would have returned
-11 because the difference between 20 and 31 is -11. However, because we used
abs(), our program returned an absolute value.
abs() also works with zero values and positive numbers (including floating numbers and integers). However, for these, the
abs() function will return the same value as the value initially parsed through the function because positive numbers and zero values are not negative numbers. Here’s an example of
abs() used on a positive number and a zero value:
Our code returns:
abs() method in Python can be used to convert a floating-point number or an integer into an absolute value. This is especially useful when you want to know the difference between two values.
In this tutorial, we explored how to use
abs() in Python. We also discussed how the function can be used in two example cases: when calculating how much faster or slower than the average speed a certain driver is travelling, and when calculating the distance between two places.
Now you’re ready to start using
abs() to calculate absolute values in Python like a pro!