如何在 Python 中写平方根?

2025年3月17日 | 阅读 7 分钟

Python 有一个预定义的 sqrt() 函数,用于返回数字的平方根。它定义了一个数字的平方根,该数字与其自身相乘得到一个数字。sqrt() 函数不直接用于查找给定数字的平方根,因此我们需要使用 math 模块在 Python 中调用 sqrt() 函数。

例如,144 的平方根是 12。

现在,让我们看看用于查找 Python 中给定数字平方根的平方根函数的语法。

语法

参数

x : 这是数字。其中数字应大于 0,可以是小数或整数。

返回

输出是平方根值。

注意

  • sqrt() 方法的输出将是一个浮点值。
  • 如果给定的输入是负数,则输出将是 ValueError。返回 ValueError 是因为任何负数的平方根值不被认为是实数。
  • 如果输入不是数字,则 sqrt() 函数返回 NaN。

示例

sqrt() 函数在 Python 中的示例用法。

代码

输出

4.0  

How to write square root in Python

1. 使用 math.sqrt() 方法

sqrt() 函数是一个内置函数,用于返回任何数字的平方根。以下是查找数字平方根的步骤。

  1. 开始程序
  2. 定义任何要查找平方根的数字。
  3. 调用 sqrt() 函数并将第 2 步中定义的数字作为参数传递,并将结果存储在一个变量中。
  4. 打印平方根。
  5. 终止程序。

Python math.sqrt() 方法示例 1

Python 示例程序,用于查找给定整数的平方根。

代码

输出

Square root of number 36 is : 6.0
Square root of number 625 is : 25.0
Square root of number 144 is : 12.0
Square root of number 64 is : 8.0

Python math.sqrt() 方法示例 2

让我们创建一个 Python 程序,该程序查找小数的平方根。

代码

输出

The Square root of 4.5 is 2.1213203435596424
The Square root of 627 is 25.039968051096054
The Square root of 6.25 is 2.5
The Square root of 0 is 0.0

Python math.sqrt() 方法示例 3

在以下程序中,我们从用户那里读取了一个数字并找到了平方根。

代码

输出

Enter a number to get the Square root: 25
Square root of the number is: 5.0

1. 使用 math.pow() 函数

pow() 是一个内置函数,在 Python 中用于返回数字的幂。它有两个参数。第一个参数定义数字,第二个参数定义该数字的幂。

Python math.pow() 方法示例

让我们看一个 math.pow() 函数的示例程序。

代码

输出

Enter the number :628 
The Square Root of the given number 628.0 = 25.059928172283335

3. 使用 Numpy 模块

NumPy 模块也是在 Python 中查找平方根的一种选择。

Python Numpy 示例

让我们看一个示例程序,用于查找数组中给定数字列表的平方根。

代码

输出

Square roots of the given array are: [ 1. 2. 3. 4. 5. ]

4. 使用 ** 运算符

我们还可以使用指数运算符来查找数字的平方根。该运算符可以应用于两个操作数之间。例如,x**y。这意味着左操作数被提升到右操作数的幂。

以下是查找数字平方根的步骤。

步骤 1. 定义一个函数并将值作为参数传递。

步骤 2. 如果定义的数字小于 0 或为负数,则不返回任何内容。

步骤 3. 使用指数 ** 符号来查找数字的幂。

步骤 4. 从用户那里获取数值。

步骤 5. 调用函数并将其输出存储到变量中。

步骤 6. 在 Python 中显示数字的平方根。

步骤 7. 退出程序。

Python ** 运算符示例 1

让我们在 Python 程序中实现以上步骤并计算数字的平方根。

代码

输出

Enter a numeric value: 256 
Square Root of the 256 = 16.0

说明

如上例所示,我们首先从用户那里获取输入(数字),然后使用指数 ** 运算符来查找数字的幂。其中 0.5 等于 √(根号)以提升给定数字的幂。最后,代码使用 format() 函数打印 num 的值和比较的平方根值。如果用户输入负数,该函数将不返回任何内容,结果将为空。

Python ** 运算符示例 2

让我们创建一个 Python 程序,该程序查找指定范围内的平方根。在以下程序中,我们找到了 0 到 30 之间所有数字的平方根。

代码

输出

Square root of a number 0 = 0.0
Square root of a number 1 = 1.0
Square root of a number 2 = 1.4142135623730951
Square root of a number 3 = 1.7320508075688772
Square root of a number 4 = 2.0
Square root of a number 5 = 2.23606797749979
Square root of a number 6 = 2.449489742783178
Square root of a number 7 = 2.6457513110645907
Square root of a number 8 = 2.8284271247461903
Square root of a number 9 = 3.0
Square root of a number 10 = 3.1622776601683795
Square root of a number 11 = 3.3166247903554
Square root of a number 12 = 3.4641016151377544
Square root of a number 13 = 3.605551275463989
Square root of a number 14 = 3.7416573867739413
Square root of a number 15 = 3.872983346207417
Square root of a number 16 = 4.0
Square root of a number 17 = 4.123105625617661
Square root of a number 18 = 4.242640687119285
Square root of a number 19 = 4.358898943540674
Square root of a number 20 = 4.47213595499958
Square root of a number 21 = 4.58257569495584
Square root of a number 22 = 4.69041575982343
Square root of a number 23 = 4.795831523312719
Square root of a number 24 = 4.898979485566356
Square root of a number 25 = 5.0
Square root of a number 26 = 5.0990195135927845
Square root of a number 27 = 5.196152422706632
Square root of a number 28 = 5.291502622129181
Square root of a number 29 = 5.385164807134504
Square root of a number 30 = 5.477225575051661

结论

总而言之,在 Python 中有多种方法可以找到给定数字的平方根值。我们可以根据需要使用数字相关的 math 模块、** 运算符、pow() 方法或 NumPy 模块。