本文共 463 字,大约阅读时间需要 1 分钟。
Python中计算平方根的方法
在Python中,计算数字的平方根可以通过math模块中的sqrt函数来实现。该函数返回给定数值的平方根。
语法示例
import math
math.sqrt(x)注意事项
sqrt函数属于math模块,需要先导入math模块才能使用。
参数说明
x:要计算的数值表达式
返回值
math.sqrt(x)返回x的平方根值
实例说明
示例代码:
#!/usr/bin/python
import mathprint("math.sqrt(100) : ", math.sqrt(100))print("math.sqrt(7) : ", math.sqrt(7))print("math.sqrt(math.pi) : ", math.sqrt(math.pi))输出结果:
math.sqrt(100) : 10.0
math.sqrt(7) : 2.64575131106math.sqrt(math.pi) : 1.77245385091推荐学习资源:
Python编程教程
转载地址:http://qplfk.baihongyu.com/