Skip to content

Python abs() Function

Last updated on August 8, 2022

In this tutorial, you will learn about the Python abs() function with the help of examples.

The abs() function of Python’s standard library returns the absolute value of the given number. If the number is a complex number, abs() returns its magnitude.

The syntax of the abs() function looks like this:

abs(x)

where x may be an integer, a floating-point number, or an object implementing abs().

Below are the examples.

# Random integer
integer_number = 20
print('Absolute value of 20 is:', abs(integer_number))
# Output: 20

# Random integer
integer_number = -20
print('Absolute value of -20 is:', abs(integer_number))
# Output: 20

# Random floating number
floating_number = -20.22
print('Absolute value of -20.22 is:', abs(floating_number))
# Output: Absolute value of -20.22 is: 20.22

# Random complex number
complex = (2+3j)
print('Magnitude of 2+3j is:', abs(complex))
Magnitude of 2+3j is: 3.605551275463989



Comments are closed, but trackbacks and pingbacks are open.