Python 如何找到一个数的幂次

Python 如何找到一个数的幂次

在本文中,我们将展示如何在Python中找到一个数的幂次。以下是完成此任务的各种方法:

  • 使用for循环

  • 使用递归

  • 使用pow()函数

  • 使用**运算符

使用for循环

步骤

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

  • 创建一个名为findPower()的函数,它返回一个数的幂次。函数接受数值和指数值作为参数。

  • 设定一个变量来存储结果并将其值初始化为1。

  • 为了得到幂次,将给定的数乘以结果的指数。我们使用以下循环将指数的数量进行乘法运算。

  • 使用for循环遍历范围为1到给定的指数+1。

  • 在循环内部,将结果乘以给定的数。

  • 使用返回关键字返回结果的幂次。

  • 创建2个单独的变量来存储输入的数和指数值。

  • 通过将输入的数和指数值作为参数调用上述定义的findPower()函数,并打印函数的返回结果。

  • 以相同的方式计算其他数的幂次。

示例

以下程序使用for循环返回一个数的幂次-

# creating a function that returns the power of a Number
# It accepts Number, and exponent values as parameters
def findPower(number, exponent):
   # intializing a variable with 1 (it stores the resultant power)
   resultPower =1
   # traversing in the range from 1 to given exponent+1
   for i in range(1, exponent+1):
      # Multiplying the result with the given number
      resultPower=resultPower*number
   # returning the resultant power
   return resultPower
# input number, exponent values
number = 5
exponent = 3
# calling the findPower() function by passing the number,exponent values
# as arguments
print("Value of",number,"raised to the power",exponent,"(",number,"^",exponent,") =", findPower(number,exponent))
#, In the same way, calculating for other numbers
number = 4
exponent = 0
print("Value of",number,"raised to the power",exponent,"(",number,"^",exponent,") =", findPower(number,exponent))

输出

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

Value of 5 raised to the power 3 ( 5 ^ 3 ) = 125
Value of 4 raised to the power 0 ( 4 ^ 0 ) = 1

使用递归

步骤

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

  • 创建一个名为findPower()的函数,该函数返回一个数的幂。函数接受数值和幂/指数值作为参数。

  • 使用 if条件语句 来检查传入的指数值是否等于0。

  • 如果条件为真,即指数值为0,则返回 1

  • 将指数值减1,并使用递归逻辑返回结果的幂。

  • 通过将输入的数和指数值作为参数传递给上述定义的findPower()函数,并打印函数的返回结果。

示例

以下程序使用递归逻辑返回一个数的幂:

# creating a function that returns the power of a Number
# It accepts Number, power/exponent value as parameters
def findPower(number, exponent):
   # checking whether the exponent value passed is equal to 0
   if exponent == 0:
   # returning 1 if the condition is true
      return 1
   # Subtract the exponent value by 1 and return the resultant power using recursive logic
      return number * findPower(number, exponent-1)
# input number, exponent values
number = 5
exponent = 3
# calling the findPower() function by passing the number, exponent values
# as arguments
print("Value of",number,"raised to the power",exponent,"(",number,"^",exponent,") =", findPower(number,exponent))

输出

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

Value of 5 raised to the power 3 ( 5 ^ 3 ) = 125

使用pow()函数

在Python中, pow() 函数计算任何正整数的幂次。

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

语法

pow(x,y)

参数

  • x - 这是数值(基准值)

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

步骤

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

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

  • 创建另一个变量来存储幂次方/指数值。

  • 使用 pow() 函数打印数值的幂次方,即通过将数值和指数值作为参数传递给它,并打印结果。

示例

以下程序使用 pow() 函数返回数值的幂次方:

# input number
inputNumber = 5
# input exponent value
inputPower = 3
# printing the resultant power value using the pow() function
print("Value of 5 raised to the power 3(5^3)=", pow(inputNumber, inputPower))

输出

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

Value of 5 raised to the power 3(5^3)= 125

使用**运算符

指数运算符(**)可用于计算一个数的幂。

由于不需要导入模块或调用函数,这是使用最方便的方法。

步骤

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

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

  • 创建一个变量来存储指数/幂值。

  • 使用指数运算符(**)打印结果数的幂,即输入的数字^输入的幂。

示例

以下程序使用**运算符返回一个数的幂:

# input number
inputNumber = 6
# input exponent value
inputPower = 2
# printing the resultant power of a number using exponential operator(**)
print("Value of 6 raised to the power 2(6^2)=", inputNumber**inputPower)

输出

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

Value of 6 raised to the power 2(6^2)= 36

结论

在本文中,我们学习了四种不同的Python方法来计算给定数字和指数的幂。我们还学习了如何在不使用内置函数的情况下计算幂。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程