当前位置:首页>维修大全>综合>

pythonsqrt函数的使用方法

pythonsqrt函数的使用方法

更新时间:2023-05-22 23:24:32

pythonsqrt函数的使用方法

Python中的sqrt函数用于计算给定数值的平方根,具体使用方法如下:

1. 导入math模块

import math

2. 使用math.sqrt()函数计算平方根

result = math.sqrt(16) # 计算16的平方根

print(result) # 输出4.0

注意:sqrt函数的参数需要是一个数字,如果传入的参数不是数字类型,将会抛出TypeError异常。

完整示例代码如下:

import math

result = math.sqrt(16)

print(result)

更多栏目