python中pow函数_pow()函数以及Python中的示例
python中pow函數(shù)
Python pow()函數(shù) (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()函數(shù)是Python中的一個(gè)庫(kù)函數(shù),用于將x賦予y的冪,其中x是基數(shù), y是冪–換句話說(shuō),我們可以說(shuō)pow()函數(shù)計(jì)算了冪,即x ** y –表示x升為y的冪。
Syntax:
句法:
pow(x, y [,z])Parameter(s):
參數(shù):
x – A base number which is to be powered
x –要加電的基數(shù)
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 –這是一個(gè)可選參數(shù),用于定義/優(yōu)化(x ** y)結(jié)果的模塊。
Return value: numer – returns result.
返回值: numer –返回結(jié)果。
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
具有兩個(gè)參數(shù)(x,y)的 pow()函數(shù) –等效于x ** y
pow() function with three arguments (x,y,z) – it is equivalent to (x**y) % z
具有三個(gè)參數(shù)(x,y,z)的 pow()函數(shù) –等效于(x ** y)%z
pow() function results with different types of the values, consider the below given table,
pow()函數(shù)結(jié)果具有不同類型的值,請(qǐng)考慮以下給定的表,
| 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 |
| 負(fù)整數(shù)或非負(fù)整數(shù) | 非負(fù)整數(shù) | 可能存在或可能不存在 |
| 負(fù)整數(shù)或非負(fù)整數(shù) | 負(fù)整數(shù) | 不應(yīng)該出現(xiàn) |
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()可以返回整數(shù),并且兩個(gè)浮點(diǎn)數(shù)都取決于條件/值。
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計(jì)算任何數(shù)量?jī)绲牟煌椒?(Different approaches to calculate the power of any number)
By using simple approach: (x**y)
通過(guò)使用簡(jiǎn)單的方法:(x ** y)
By using pow() function: pow(x,y)
通過(guò)使用pow()函數(shù):pow(x,y)
By using math.pow() function – which is a function of "math" library
通過(guò)使用math.pow()函數(shù) –這是“數(shù)學(xué)”庫(kù)的函數(shù)
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()函數(shù)帶有三個(gè)參數(shù)(最后一個(gè)是可選的),其中math.pow()僅帶有兩個(gè)參數(shù)。 在此,不存在第三個(gè)參數(shù),否則所有功能都與simple pow()相同。 但是math.pow()始終返回浮點(diǎn)值。 因此,如果由于某種原因要確保返回結(jié)果為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:帶有第三個(gè)參數(shù)的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函數(shù)
總結(jié)
以上是生活随笔為你收集整理的python中pow函数_pow()函数以及Python中的示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 天然气一吨多少钱啊?
- 下一篇: 扶摇剧情介绍