How to calculate an absolute value in Python

The abs() function in Python returns the absolute value of the given argument. This argument can be an int, a float, or a complex number.

In case of complex numbers, abs() function returns the magnitude part only.

Magnitude formula for the complex numbers

The absolute value of a complex number, a+bia+bi is defined as the distance between the origin (0,0)(0,0) and the point (a,b)(a,b) in the complex plane and the formula is:

a2+b2\sqrt{a^2 + b^2}

In case of 34j3-4j:

32+42=5.0\sqrt{3^2 + 4^2} = 5.0

Syntax

The syntax of abs() function is:

 abs(number) 

Parameter

  • number - Can be an int, a float, or a complex number.

Examples

Let’s test the abs() function with different inputs:


  1. Passing an integer number as a parameter:
int_number = -50
abs_number = abs(int_number)
print(abs_number)

  1. Passing a floating number as a parameter:
float_number = 50.5
abs_number = abs(float_number)
print(abs_number)

  1. Passing a complex number as a parameter:
complex_number = 3-4j
abs_number = abs(complex_number)
print(abs_number)

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved