Python 如何计算一个数的平方根

Python 如何计算一个数的平方根

在本文中,我们将演示如何在Python中计算一个数的平方根。以下是实现这个任务的各种方法:

  • 使用sqrt()函数计算平方根
  • 使用pow()函数计算平方根
  • 使用指数运算符计算平方根
  • 使用cmath模块计算平方根

一个数的平方根是什么意思

一个数的平方根是一个值,当它与自身相乘时返回相同的数。

示例 − 5 x 5 = 25, 所以25的平方根是5。但是-5 x -5也等于25,所以-5也是25的平方根。

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

使用Python库中math模块定义的sqrt()函数是计算一个数的平方根的最简单方法。

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字导入math模块。
  • 创建一个变量来存储输入的数。
  • 使用math模块的sqrt()函数,通过将数字作为参数传递给它来得到输入数字的平方根。
  • 打印输入数字的平方根结果。

示例

以下程序使用math.sqrt()函数返回一个数的平方根:

# importing math module
import math

# input number
inputNumber = 25

# getting the square root of an input number
squareRoot = math.sqrt(inputNumber)

# printing the square root of a number
print ("Square root of",inputNumber,"is: ", squareRoot)

输出

在执行上述程序后,将生成以下输出结果 –

Square root of 25 is: 5.0

使用pow()函数计算平方根

在Python中,可以使用pow()函数快速计算平方根。

它返回x的y次幂的值(x^y)。

语法

pow(x,y)

参数

  • x - 这是数字值(基值)
  • y - 这是数字值的幂次方(指数值)

步骤

以下是执行所需任务的算法/步骤:

  • 使用 math 模块的 pow() 函数传入数字值(基值)和幂次方(指数值)作为参数来获取输入数字的平方根。这里的0.5是表示平方根的 指数 值。
  • 打印输入数字的平方根结果。

示例

以下程序使用 pow() 函数返回一个数字的平方根:

# importing math module
import math

# input number
inputNumber = 25

# getting the square root of an input number
# here 0.5 is the exponent value which represents the square root
squareRoot = math.pow(inputNumber, 0.5)

# printing the square root of a number
print ("Square root of",inputNumber,"is: ", squareRoot)

输出

执行上述程序后,将生成以下输出-

Square root of 25 is: 5.0

使用指数运算符计算平方根

与pow()函数类似,指数运算符**执行平方根运算。

步骤

以下是执行所需任务的算法/步骤:

  • 使用def关键字创建一个函数 squareRoot() ,该函数返回传递给它作为参数的数字的平方根。

  • 使用if条件语句检查数是否小于0(负数)。

  • 如果条件为真,即数字小于0,则使用return语句返回nothing。

  • 否则,使用指数运算符(**)返回数字的平方根。

  • 创建一个变量来存储输入的数字。

  • 通过传递输入的数字作为参数调用上述创建的squareRoot()函数,并打印结果的平方根。

示例

以下程序使用指数运算符(**)返回数字的平方根。

# creating a function that returns the square root of a number passed to it
def squareRoot(num):

   # returning nothing if the number is less than 0(negative number)
   if num < 0:
      return
   else:
      # else returning the square root of a number using the exponential operator(**)
      return num**0.5

# input number
inputNumber = 16
# calling the squareRoot() function by passing the input number as an argument
print ("Square root of",inputNumber,"is: ", squareRoot(inputNumber))

输出

执行上述程序后,将生成以下输出结果:

Square root of 16 is: 4.0

使用cmath模块计算平方根

在Python中,可以使用cmath模块确定实数或复数的平方根。

对于所有正实数,我们在这里使用的各种方法都能正常工作。但是,对于负数或复数,cmath模块似乎很有用。

cmath.sqrt() 也用于获得负数的平方根。

步骤

以下是要执行所需任务的算法/步骤:

  • 使用import关键字导入cmath模块。
  • 创建一个变量来存储输入的复数。
  • 使用 cmath 模块的 sqrt() 函数通过将该数字作为参数传递给它来获得复数的平方根。
  • 打印输入复数的平方根。

示例

以下程序使用cmath.sqrt()函数返回复数的平方根-

# importing cmath module
import cmath

# input complex number
complexNumber = 2 + 3j

# getting the square root of a complex number using cmath.sqrt() function
squareRoot = cmath.sqrt(complexNumber)

# printing the square root of an input complex number
print ("Square root of",complexNumber,"is: ", squareRoot)

输出

执行以上程序后将生成以下输出结果−

Square root of (2+3j) is: (1.6741492280355401+0.8959774761298381j)

注意 − 您也可以使用eval()函数将输入转换为复数。

对于负数

示例

# importing cmath module
import cmath

# input complex number(negative number)
complexNumber = -16

# getting the square root of a complex number using cmath.sqrt() function
squareRoot = cmath.sqrt(complexNumber)

# printing the square root of an input complex number
print ("Square root of",complexNumber,"is: ", squareRoot)

输出

在执行上述程序后,将生成以下输出结果:

Square root of -16 is: 4j

结论

在本文中,我们学习了四种不同的Python方法,用于计算给定数字的平方根。我们还学习了如何使用cmath模块来计算负数的平方根。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程