python的绝对值函数abs()用法和参数类型

python的绝对值函数abs()

python的绝对值内置函数为abs(),参数只要一个,参数的类型为数值类型,可以是int、float和complex复数的类型。如果传入的是其它类型的参数,比如str字符串类型的话,python的IDE将会报错。python绝对值函数abs()的用法和实例代码如下:

>>> abs(-1)
1
>>> abs(0.5)
0.5
>>> abs(1+0.5j) #该参数为复数类型的值
1.118033988749895
>>> abs('1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
>>> abs([1]) #该参数为列表list的类型,python同样会报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'list'

全栈后端 / python教程 :


























Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.