Python cmath 模块的 cmath.pi 常数定义为圆周长与直径的比率。这是使用球体、三角函数和其他几何计算进行数学计算的。
这表示为 Π(pi)。此值大约等于 3.14159。如果你把圆的周长除以它的直径,那么我们总是会得到一个 Π。
这个 Π 常数不能用分数表示,因为它是一个无理数,它的十进制表示是无限的,没有重复。
语法
以下是 Python cmath.pi常量的基本语法 -
cmath.pi 函数
cmath.pi
返回值
此常量返回 pi 的值,即 3.14159。
示例 1
在下面的示例中,我们使用 cmath.pi 常数计算 pi 值。
import cmath
x = cmath.pi
print(x)
输出
以下是上述代码的输出 -
3.141592653589793
示例 2
在下面的示例中,我们使用 cmath.pi 常量计算圆的面积。
import cmath
r = 4
area = cmath.pi * (r ** 2)
print("The area of a circle with radius", r, "is:", area)
输出
结果如下所示 -
The area of a circle with radius 4 is: 50.26548245743669
示例 3
在此示例中,我们使用 cmath.pi 常数计算球体的体积。
import cmath
r = 5
volume = (4/3) * cmath.pi *(r ** 3)
print("The volume of the sphere with radius", r, "is:", volume)
输出
输出的获取方式如下 -
The volume of the sphere with radius 5 is: 523.5987755982989
示例 4
在这里,我们使用 cmath.pi 常数计算圆的周长。
import cmath
r = 8
circumference = 2 * cmath.pi * r
print("The circumference of the circle with radius", r, "is:", circumference)
输出
以下是结果 -
The circumference of the circle with radius 8 is: 50.26548245743669