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   # 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()函数结果具有不同类型的值,请考虑以下给定的表,

XYZ
Negative or Non Negative IntegerNon-negative IntegerMay or may not be present
Negative or Non Negative IntegerNegative IntegerShould 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)

  1. By using simple approach: (x**y)

    通过使用简单的方法:(x ** y)

  2. By using pow() function: pow(x,y)

    通过使用pow()函数:pow(x,y)

  3. 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函数

  • 13
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值