如何在 Python 中求一個數的冪?


在本文中,我們將向您展示如何在 python 中求一個數的冪。以下是完成此任務的各種方法:

  • 使用 for 迴圈

  • 使用遞迴

  • 使用 pow() 函式

  • 使用 ** 運算子

使用 for 迴圈

演算法(步驟)

以下是執行所需任務的演算法/步驟。

  • 建立一個名為 findPower() 的函式,該函式返回一個數的冪。該函式接受數字和指數值作為引數。

  • 取一個儲存結果的變數,並將其值初始化為 1。

  • 為了獲得冪,將給定數字乘以結果指數。我們使用下面的迴圈將指數乘以指定次數

  • 使用 for 迴圈,遍歷範圍 **1** 到給定的 **指數+1**。

  • 在迴圈內部,將結果乘以給定數字

  • 使用 return 關鍵字返回結果冪。

  • 建立兩個單獨的變數來儲存輸入數字和指數值。

  • 透過將輸入數字和指數值作為引數傳遞給它來呼叫上面定義的 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()** 函式計算任何正整數的冪。

它返回 **x** 的 **y** 次冪 (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 方法來計算給定數字和指數的冪。我們還學習瞭如何在不使用內建函式的情況下計算冪。

更新於: 2022年10月28日

14K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.