python中pow函数_pow()函数以及Python中的示例
python中pow函數
Python pow()函數 (Python pow() function)
pow() function is a library function in Python, is used to get the x to the power of y, where x is the base and y is the power – in other words we can say that pow() function calculates the power i.e. x**y – it means x raised to the power y.
pow()函數是Python中的一個庫函數,用于將x賦予y的冪,其中x是基數, y是冪–換句話說,我們可以說pow()函數計算了冪,即x ** y –表示x升為y的冪。
Syntax:
句法:
pow(x, y [,z])Parameter(s):
參數:
x – A base number which is to be powered
x –要加電的基數
y – A value of the power
y –冪的值
z – It's an optional parameter, it is used to define/fine the modules of the result of (x**y).
z –這是一個可選參數,用于定義/優化(x ** y)結果的模塊。
Return value: numer – returns result.
返回值: numer –返回結果。
Example:
例:
Input:x = 2 # basey = 3 # powerz = 3 # value for modulusprint(pow(x, y))print(pow(x, y, z))Output:82Note:
注意:
pow() function with two arguments (x,y) – it is equivalent to x**y
具有兩個參數(x,y)的 pow()函數 –等效于x ** y
pow() function with three arguments (x,y,z) – it is equivalent to (x**y) % z
具有三個參數(x,y,z)的 pow()函數 –等效于(x ** y)%z
pow() function results with different types of the values, consider the below given table,
pow()函數結果具有不同類型的值,請考慮以下給定的表,
| Negative or Non Negative Integer | Non-negative Integer | May or may not be present |
| Negative or Non Negative Integer | Negative Integer | Should not be present |
| 負整數或非負整數 | 非負整數 | 可能存在或可能不存在 |
| 負整數或非負整數 | 負整數 | 不應該出現 |
Example1:
范例1:
# python code to demonstrate example of # pow() function x = 2 # base y = 3 # power z = 3 # value for modulus# calcilating power with two arguments result1 = pow(x, y) # calcilating power & modulus with three arguments result2 = pow(x, y, z)# printing the values print("result1: ", result1) print("result2: ", result2)Output
輸出量
result1: 8 result2: 2Note: pow() can return integer and float both values depend on the condition/ values.
注意: pow()可以返回整數,并且兩個浮點數都取決于條件/值。
Example2:
范例2:
# python code to demonstrate example of # pow() function x = 4 # base y = 3 # power z = 6 # value for modulusprint("With 2 args:", pow(x,y)) #first taking 2 args only print("With 3 args:", pow(x,y,z)) #then all the 3 argsprint("Return float values:", pow(2,-3))print('Random numbers power:' , pow(5,5,6))Output
輸出量
With 2 args: 64 With 3 args: 4 Return float values: 0.125 Random numbers power: 5計算任何數量冪的不同方法 (Different approaches to calculate the power of any number)
By using simple approach: (x**y)
通過使用簡單的方法:(x ** y)
By using pow() function: pow(x,y)
通過使用pow()函數:pow(x,y)
By using math.pow() function – which is a function of "math" library
通過使用math.pow()函數 –這是“數學”庫的函數
Note: pow() function takes three arguments (the last one is optional), where math.pow() takes only two arguments. In this, there is no third parameter present else all the functionality is the same as simple pow(). But the math.pow() always returns float values. So if you, for some reason, want to make sure you get float as a result back, then math.pow() will provide that benefit to the user.
注意: pow()函數帶有三個參數(最后一個是可選的),其中math.pow()僅帶有兩個參數。 在此,不存在第三個參數,否則所有功能都與simple pow()相同。 但是math.pow()始終返回浮點值。 因此,如果由于某種原因要確保返回結果為float,則math.pow()將為用戶提供這一好處。
Example 1: Calculating X to the power Y using different approaches
示例1:使用不同的方法將X乘以Y
# python code to demonstrate different # approaches to calculate x to the power yimport math x = 2 # base y = 3 # powerresult1 = x**y result2 = pow(x,y) result3 = math.pow(x,y)print("result1 (normal approach): ", result1) print("result2 (using pow() function): ", result2) print("result3 (using math.pow() function): ", result3)Output
輸出量
result1 (normal approach): 8 result2 (using pow() function): 8 result3 (using math.pow() function): 8.0Example2: pow() vs math.pow() with third parameter
示例2:帶有第三個參數的pow()vs math.pow()
# python code to demonstrate different # approaches to calculate x to the power yimport math x = 2 # base y = 3 # power z = 3 # for modulsprint("pow(x,y,z): ", pow(x,y,z))# following statement will throw an error print("math.pow(x,y,z): ", math.pow(x,y,z))Output
輸出量
pow(x,y,z): 2 Traceback (most recent call last):File "/home/main.py", line 12, in print("math.pow(x,y,z): ", math.pow(x,y,z)) TypeError: pow expected 2 arguments, got 3翻譯自: https://www.includehelp.com/python/pow-function-with-example-in-python.aspx
python中pow函數
總結
以上是生活随笔為你收集整理的python中pow函数_pow()函数以及Python中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天然气一吨多少钱啊?
- 下一篇: math.atan2_带有Python示