# 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:

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.

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:

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:

## 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:

The following code shows how you can create a complex number in Python and then apply the `abs()`

function to get its magnitude:

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.