How to take absolute values in Python
In Python, the absolute value can be found using the built-in method abs()
. This function returns the absolute value of the input given to it. The argument given can be an integer, a float number or a complex number.
# integer intg = -231 abs_intg = abs(intg) print('Absolute value of {} is {}'.format(intg, abs_intg)) # floating flt = -231.789 abs_flt = abs(flt) print('Absolute value of {} is {}'.format(flt, abs_flt)) #complex cmpl = (2 - 5j) abs_cmpl = abs(cmpl) print('Magnitude of {} is {}'.format(cmpl, abs_cmpl))
Output:
Absolute value of -231 is 231 Absolute value of -231.789 is 231.789 Magnitude of (2-5j) is 5.385164807134504
Subscribe
Login
Please login to comment
0 Discussion