如何在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()**函式列印一個數的結果冪,即inputNumber ^ inputPower,將數字和指數值作為引數傳遞給它,並列印結果冪。

示例

下面的程式使用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

使用**運算子

**指數運算子(**)** 可以用來計算一個數的冪。

由於不需要匯入模組或呼叫函式,因此這是最方便使用的方法。

演算法(步驟)

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

  • 建立一個變數來儲存輸入數字。

  • 建立一個變數來儲存指數/冪值。

  • 使用指數運算子(**)列印一個數的結果冪,即inputNumber ^ inputPower。

示例

下面的程式使用**運算子返回一個數的冪:

# 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日

13K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.