The Python abs() method returns the absolute value of a number. 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.
An absolute value is the distance between a number and 0 on the number line. These values are useful in a number of mathematical equations. For instance, absolute values are used in distance calculations.
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 in Python: An Explanation
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 is 5. The absolute value of -5 is also 5. This is the case because both 5 and -5 are five spaces away from on the number line. Here’s an example number line to illustrate how this works:
-5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 |
-5 is 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
The Python 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 abs() method:
abs(value)
The abs() method returns either a floating-point number or an integer depending on the value you pass through the method:
- -9 would return 9.
- -2.2 would return 2.2.
The floating-point value is 2.2, denoted by the period that appears in the number. Because abs() accepts both integers and floating-points, no numerical conversion is necessary to use this method.
abs() works with zero values and positive numbers (including floating numbers and integers). However, the abs() function will return the same value as the value initially parsed through the function. This is positive numbers and zero values are not negative numbers.
Let’s use an example to illustrate how the abs() method works.
Python abs() Method Example
Let’s say that you are working for the local police department. 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.
The police department does not want people exceeding this limit because there was a series of accidents on the road. Many of the accidents were due to excessive speed variability among drivers.
Driving over 10 miles (ca. 16 km) per hour (ca. 16 km/h) faster or slower than the average driver presents a hazard. Drivers who exceed this limit should receive a ticket.
Evaluating the Speed
We’re going to build a program to determine whether a driver should receive a ticket based on their speed:
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: 12.
On the first line of code, we declared a Python variable. This variable stores the average speed of cars on the road, which in this case was 50 mph (ca. 80 km/h). Then, we declared a variable that stores a specific driver’s current speed—38 mph (ca. 61 km/h) 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 (ca. 19 km/h). But, we only wanted to know the difference between the specific driver’s speed and the average speed of cars on the road. We used abs() to convert our value into an absolute value, and our program returned 12.
Displaying a Message
We want our program to display a message if a driver is due a ticket. To do this, we can add a Python if statement to the end of our last program”
print("Speed Difference: " + absolute_value) if absolute_value > 10: print("This driver is exceeding the speed limit. Write them a ticket.") else: print("This driver is not exceeding the speed limit.")
First, we print the driver’s speed difference to the console. Then, we use an if statement to evaluate whether the driver is exceeding the speed limit. A different Python print statement executes depending on whether the driver exceeds or is driving within the speed limit.
Absolute Value Python: Distance Calculations
Absolute values are commonly used in distance calculations.
Let’s say that we are trying to travel somewhere 20 miles (ca. 32 km) away from our home. 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 abs().
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Here’s the code for an example that uses the abs() function in Python:
miles_from_home = 20 travelled = 31 miles_left = miles_from_home - travelled print(abs(miles_left))
Our code returns: 11.
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.
Conclusion
The 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, with reference to two examples.
Do you want to advance your Python knowledge? Check out our complete How to Learn Python guide. In this guide, you’ll find advice on how to learn Python and a list of top learning resources to look at.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.