Notes

`abs()` Function in Python [ English ]

< Prev Next >

1. Introduction

The abs() function in Python is a built-in function used to return the absolute value of a number. The absolute value of a number is its distance from zero on the number line, without considering the sign.

In other words, abs() converts negative numbers into positive numbers, while positive numbers remain unchanged.

This function works with integers, floating-point numbers, and complex numbers.

2. Syntax

abs(number)

Parameter

3. Return Value

4. Example with Positive Number

print(abs(10))

Output

10

Explanation

5. Example with Negative Number

print(abs(-15))

Output

15

Explanation

6. Example with Floating-Point Number

print(abs(-7.5))

Output

7.5

Explanation

7. Example with Complex Number

print(abs(3 + 4j))

Output

5.0

Explanation

[ |a + bj| = \sqrt{a^2 + b^2} ]

For example:

[ \sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt{25} = 5 ]

8. Practical Example

temperature = -12

print("Temperature difference:", abs(temperature))

Output

Temperature difference: 12

Explanation

9. Summary

< Prev Next >