abs()函数用于返回数字的绝对值。
用法:
abs(number)
number:Can be integer, a floating point
number or a complex number
abs()仅接受一个参数,该参数的绝对值将被返回。参数可以是整数,浮点数或复数。
如果参数是整数或浮点数,则abs()以整数或浮点数返回绝对值。
如果是复数,则abs()仅返回幅度部分,也可以是浮点数。
# Python code to illustrate
# abs() built-in function
# floating point number
float = -54.26
print('Absolute value of float is:', abs(float))
# An integer
int = -94
print('Absolute value of integer is:', abs(int))
# A complex number
complex = (3 - 4j)
print('Absolute value or Magnitude of complex is:', abs(complex))
输出:
Absolute value of float is:54.26
Absolute value of integer is:94
Absolute value or Magnitude of complex is:5.0