Python pow()用法及代码示例

您所在的位置:网站首页 pow函数是什么的缩写 Python pow()用法及代码示例

Python pow()用法及代码示例

2024-07-11 11:37| 来源: 网络整理| 查看: 265

Python pow() 函数返回第一个参数的第二个参数次幂的结果。

Python 中 pow() 函数的语法 用法:pow(x, y, mod)

参数:

x :必须计算其幂的数字。 y :计算能力的值提升。 模组[可选]:如果提供,则对 x**y 的结果执行 mod 取模(即:x**y % mod)

返回值:返回 float 或 int 形式的值 x**y (取决于输入操作数或第二个参数)。

Python pow() 函数示例

Python中的pow()使用起来很简单,只需传递两个值作为参数即可。

Python3 print(pow(3,2))

输出:

9 Python 中带模数的幂函数

我们可以将两个以上的参数传递给Python中的pow()。第一个参数是我们要应用Pow()函数的数字,第二个参数是幂,第三个参数是执行模运算。

It return the value of 3 to the power of 4, modulus 10 (same as (3 * 3 * 3 * 3) % 10) Python3 # Python code to demonstrate pow() 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()函数在Python中的实现案例

下表总结了应用 Python pow() 函数的不同情况。

Cases of Python pow() function

Python pow() 函数的案例

例子:

用于讨论否定和非否定情况的 Python 代码。

Python3 # 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 : 64 Negative x and positive y : -64 Positive x and negative y : 0.015625 Negative x and negative y : -0.015625



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3