Absolute value in Python: abs can calculate absolute of complex numbers

Python built-in function abs returns the absolute value.

print(abs(2))  # 2
print(abs(3.1))  # 3.1
print(abs(-1))  # 1
print(abs(-5.5))  # 5.5
print(abs(1 / 2))  # 0.5
print(abs(-1 / 4))  # 0.25
print(abs((-3) ** 3))  # 27


Fractions are automatically converted to floats by abs.

Absolute of complex number

j is a special symbol meaning an imaginary unit "i" that is a solution of $x^2 + 1 = 0$. Python abs can calculate the absolute value of a complex number.

c = 3 + 4j

print(abs(c))  # 5.0


c is just 5 but the function returns 5.0 (float).