This article covers the use of the Python abs Function.

The Python abs Function is an built-in function that returns the absolute value for a given number. In the case of a complex number, the absolute value is the magnitude of it’s real and imaginary parts.

Syntax

The abs() Function takes a single number as a parameter. Number can either a float, integer or complex number.

abs(number)

Integer

The abs() function removes any negative sign that may be present on a negative integer.

result = abs(5)
result = abs(-55)
#OUTPUT
5
55

Float

The abs() function removes any negative sign that may be present on a negative float number.

result = abs(5.4)
result = abs(-5.67)
#OUTPUT
5.4
5.67

Complex Number

The abs() function calculates the magnitude using the formula magnitude = (Re^2 + Im^2)^1/2. For this reason, negative signs do not have any effect on the output. Any negative sign is removed when the real and imaginary parts are squared.

result = abs(3 + 4i)
result = abs(-3 - 4i)
#OUTPUT
5
5

This marks the end of the Python abs Function. Any suggestions or contributions for CodersLegacy are more than welcome. Questions can be asked in the comment section below.

Here’s a link back to the main Python Built-in Functions page.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments