Python中的pow()

Python pow()函数 返回给定数字的幂。这个函数 计算x**y 。此函数首先将其参数转换为float,然后计算幂。

null

语法: 战俘(x,y)

参数:

  • x: 必须计算其功率的数字。
  • y: 提升值以计算功率。

返回值: 以浮点形式返回值x**y。

Python pow()示例

示例1:pow()的工作

Python3

# Python code to demonstrate pow()
# version 1
print ( "The value of 3**4 is : " , end = "")
# Returns 81
print ( pow ( 3 , 4 ))


输出:

The value of 3**4 is : 81.0

示例2:Python pow()与 三个论点

浮动功率(x、y、mod) 作用 计算(x**y)%mod 。此函数首先将其参数转换为float,然后计算幂。

语法: 浮动功率(x、y、mod)

参数:

  • x: 必须计算其功率的数字。
  • y: 提升值以计算功率。
  • 国防部: 必须计算模量的值。

返回值: 以浮点形式返回值(x**y)%mod。

Python3

# Python code to demonstrate pow()
# version 2
print ( "The value of (3**4) % 10 is : " , end = "")
# Returns 81%10
# Returns 1
print ( pow ( 3 , 4 , 10 ))


输出:

The value of (3**4) % 10 is : 1

pow()中的实现案例:

图片[1]-Python中的pow()-yiteyi-C++库

Python3

# Python code to discuss negative
# and non-negative cases
# positive x, positive y (x**y)
print ( "Positive x and positive y : " , end = "")
print ( pow ( 4 , 3 ))
print ( "Negative x and positive y : " , end = "")
# negative x, positive y (-x**y)
print ( pow ( - 4 , 3 ))
print ( "Positive x and negative y : " , end = "")
# positive x, negative y (x**-y)
print ( pow ( 4 , - 3 ))
print ( "Negative x and negative y : " , end = "")
# negative x, negative y (-x**-y)
print ( pow ( - 4 , - 3 ))


输出:

Positive x and positive y : 64Negative x and positive y : -64Positive x and negative y : 0.015625Negative x and negative y : -0.015625

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享