abs() in Python

PythonServer Side ProgrammingProgramming

The abs() function in python represents the absolute value of a numeric input. The absolute value is concerned with the value or magnitude of a number instead of the sign attached to the number. It is even more useful for complex numbers where the calculation of magnitude involves many steps.

The syntax of the function is −


where num can be integer,floating number or a complex number.


In the below example we take all the above types of numbers and calculate their magnitude.

n = -112
print('Absolute value of a integer', abs(n))

f = -39.222
print('Absolute value of a float: ', abs(f))

c = (11 - 21j)
print('Absolute value or Magnitude of a complex number:', abs(c))


Running the above code gives us the following result:

('Absolute value of a integer', 112)
('Absolute value of a float: ', 39.222)
('Absolute value or Magnitude of a complex number:', 23.706539182259394)
Published on 08-Jul-2019 18:01:46