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 # base
y = 3 # power
z = 3 # value for modulus
print(pow(x, y))
print(pow(x, y, z))
Output:
8
2
Note:
注意:
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()函数结果具有不同类型的值,请考虑以下给定的表,
X | Y | Z |
---|---|---|
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 |
X | ÿ | ž |
---|---|---|
负整数或非负整数 | 非负整数 | 可能存在或可能不存在 |
负整数或非负整数 | 负整数 | 不应该出现 |
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: 2
Note: 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 modulus
print("With 2 args:", pow(x,y)) #first taking 2 args only
print("With 3 args:", pow(x,y,z)) #then all the 3 args
print("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 y
import math
x = 2 # base
y = 3 # power
result1 = 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.0
Example2: pow() vs math.pow() with third parameter
示例2:带有第三个参数的pow()vs math.pow()
# python code to demonstrate different
# approaches to calculate x to the power y
import math
x = 2 # base
y = 3 # power
z = 3 # for moduls
print("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函数