You are reading solutions / Python
alfie-grace-headshot-square2.jpg
Author: Alfie Grace
Data Scientist

Python Absolute Value – abs() for real and complex numbers

The absolute value of a number refers to that value's magnitude, regardless of whether it is positive or negative. For example, the absolute value of -5 is 5.

Below is the syntax for using the abs() function to determine the absolute value of a number in Python:

abs(-5)
Out:
5

Today we'll look at some examples of where the abs() function could be applied, looking particularly at real and complex numbers.

Application 1: Integers and Floats

Applying the abs() function to a real number returns the magnitude of that number. We can define a real number as being on the real number line, represented in the image below. The magnitude of a real number refers to its distance along the line from the origin.

The sign of the number alludes to which direction along the line the number is; positive values are along the positive axis, and negative ones are along the negative axis. In the quick example shown in the introduction, -5 is a real number.

real_numbers.png

In regards to Python, real numbers are numbers that are integers or floats. The following example demonstrates how we can apply the abs function to a list of integers:

real_number_list = [-12, -6, 0, 4, 8]

for number in real_number_list:
    absolute_value = abs(number)
    print(f'Number: {number}, Absolute Value: {absolute_value}')
Out:
Number: -12, Absolute Value: 12
Number: -6, Absolute Value: 6
Number: 0, Absolute Value: 0
Number: 4, Absolute Value: 4
Number: 8, Absolute Value: 8

The use of the abs() function has converted the negative numbers into positive ones. For the positive numbers, there has been no change.

Remember that the absolute value of a real number refers to its distance from 0 and is known as magnitude. As magnitude is just a distance, it will always be positive.

We can also use the abs() function on floats. See below for an example of this:

real_number_list = [-4.16, -3.12, 11.88, 16.32]

for number in real_number_list:
    absolute_value = abs(number)
    print(f'Number: {number}, Absolute Value: {absolute_value}')
Out:
Number: -4.16, Absolute Value: 4.16
Number: -3.12, Absolute Value: 3.12
Number: 11.88, Absolute Value: 11.88
Number: 16.32, Absolute Value: 16.32

Application 2: Complex Numbers

We can also apply the abs() function to complex numbers.

A complex number is a combination of real and imaginary numbers. We can define an imaginary number as being expressed in terms of the square root of a negative number. They are usually expressed in terms of the value $i$ or $j$, which means the square root of -1.

Imaginary numbers help fill a lot of gaps in mathematics. As a result, they are used quite commonly in maths-heavy industries, particularly in electrical engineering. The image below shows an example of a complex number:

complex-numbers.jpg

The following code shows how you can create a complex number in Python and then apply the abs() function to get its magnitude:

c = (6 + 7j)
abs(c)
Out:
9.219544457292887

In Python, we use j to represent the imaginary number because i is often used to represent indexes. For complex numbers, we use the Pythagorean theorem to calculate magnitude, like so:

$$\large \vert 6 + 7j \vert = \sqrt{6^2+7^2} \approx 9.22$$

Summary

Getting the absolute value of a value in Python is quick and easy to do using the abs() function. The abs() function works with integers, floats, and complex numbers, so it should keep you covered in any situation where you may need to apply it.

Take the internet's best data science courses Learn More

Meet the Authors

alfie-grace-headshot-square2.jpg

Alfie graduated with a Master's degree in Mechanical Engineering from University College London. He's currently working as a top-rated data scientist on Upwork. Find him on LinkedIn.

Brendan Martin
Editor: Brendan Martin
Founder of LearnDataSci

Get updates in your inbox

Join over 7,500 data science learners.