Python - AI 助手

Python cmath.pi 常量



Python 的 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
python_modules.htm
廣告