Related Tags

python
abs()
absolute value

# 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+bi$ is defined as the distance between the origin $(0,0)$ and the point $(a,b)$ in the complex plane and the formula is:

$\sqrt{a^2 + b^2}$

In case of $3-4j$:

$\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)

RELATED TAGS

python
abs()
absolute value
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time