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
complexnumbers,abs()function returns the magnitude part only.
Magnitude formula for the complex numbers
The absolute value of a complex number, is defined as the distance between the origin and the point in the complex plane and the formula is:
In case of :
Syntax
The syntax of abs() function is:
abs(number)
Parameter
number- Can be anint, afloat, or acomplexnumber.
Examples
Let’s test the abs() function with different inputs:
- Passing an integer number as a parameter:
int_number = -50abs_number = abs(int_number)print(abs_number)
- Passing a floating number as a parameter:
float_number = 50.5abs_number = abs(float_number)print(abs_number)
- Passing a complex number as a parameter:
complex_number = 3-4jabs_number = abs(complex_number)print(abs_number)
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved