Trusted answers to developer questions

Educative Answers Team

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.

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$

The syntax of `abs()`

function is:

```
abs(number)
```

`number`

- Can be an`int`

, a`float`

, or a`complex`

number.

Let’s test the `abs()`

function with different inputs:

- Passing an integer number as a parameter:

int_number = -50 abs_number = abs(int_number) print(abs_number)

- Passing a floating number as a parameter:

float_number = 50.5 abs_number = abs(float_number) print(abs_number)

- 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

Copyright ©2022 Educative, Inc. All rights reserved

RELATED COURSES

View all Courses

Keep Exploring

Related Courses