编写 Python 程序以找到圆柱体的体积和表面积,并附有示例。在开始编写 Python 程序以找到圆柱体的体积和表面积之前,让我们先了解圆柱体的侧面积、顶部或底部表面积以及体积背后的定义和公式。
圆柱体的表面积
如果我们知道圆柱体的半径和高度,那么我们可以使用以下公式计算圆柱体的表面积:
圆柱体的表面积 = 2πr² + 2πrh (其中 r 是半径,h 是圆柱体的高度)。
圆柱体的体积
圆柱体内部的空间称为体积。如果我们知道圆柱体的高度,那么我们可以使用以下公式计算圆柱体的体积:
- 圆柱体体积 = πr²h
- 圆柱体侧面积 = 2πrh
- 我们可以计算圆柱体顶部或底部表面积 = πr²
Python 程序计算圆柱体的体积和表面积
此 Python 程序允许用户输入半径和高度的值。使用这些值,此 Python 程序将根据公式计算圆柱体的体积、表面积、侧面积、顶部或底部表面积。
PI = 3.14
radius = float(input('Please Enter the Radius of a Cylinder: '))
height = float(input('Please Enter the Height of a Cylinder: '))
sa = 2 * PI * radius * (radius + height)
Volume = PI * radius * radius * height
L = 2 * PI * radius * height
T = PI * radius * radius
print("\n The Surface area of a Cylinder = %.2f" %sa)
print(" The Volume of a Cylinder = %.2f" %Volume)
print(" Lateral Surface Area of a Cylinder = %.2f" %L);
print(" Top OR Bottom Surface Area of a Cylinder = %.2f" %T)
首先,我们声明了 PI 变量并将值赋为 3.14。下面的语句将要求用户输入半径和高度值,并将用户输入的值分配给相应的变量。例如,第一个值将分配给半径,第二个值分配给高度。
radius = float(input('Please Enter the Radius of a Cylinder: '))
height = float(input('Please Enter the Height of a Cylinder: '))
接下来,我们使用各自的公式计算圆柱体的体积、表面积、侧面积、顶部或底部表面积。
sa = 2 * PI * radius * (radius + height) Volume = PI * radius * radius * height L = 2 * PI * radius * height T = PI * radius * radius
以下 Python 打印语句将帮助我们打印圆柱体的体积和表面积。
print("\n The Surface area of a Cylinder = %.2f" %sa)
print(" The Volume of a Cylinder = %.2f" %Volume)
print(" Lateral Surface Area of a Cylinder = %.2f" %L);
print(" Top OR Bottom Surface Area of a Cylinder = %.2f" %T)

对于这个计算圆柱体体积和表面积的 Python 程序示例,我们输入了圆柱体的半径 = 3,高度 = 5。
圆柱体的表面积是
圆柱体表面积 = 2πr² + 2πrh
也可以写成
圆柱体表面积 = 2πr (r+h)
圆柱体的表面积 = 2 * PI * 半径 * (半径 + 高度)
圆柱体的表面积 = 2 * 3.14 * 3 * (3+5);
圆柱体的表面积 = 150.72
圆柱体的体积是
圆柱体体积 = πr²h
圆柱体的体积 = PI * 半径 * 半径 * 高度
圆柱体的体积 = 3.14 * 3 * 3 * 5
圆柱体的体积 = 141.3
圆柱体的侧面积是
L = 2πrh
L = 2 * PI * 半径 * 高度
L = 2 * 3.14 * 3 * 5
L = 94.2
圆柱体顶部或底部表面积是
T = πr²
T = PI * 半径 * 半径
T = 3.14 * 3 * 3
T = 28.26
注意:为计算目的,我们取 π = 3.14 而不是 (3.142857142..)。因此,以上所有值都接近程序输出,但可能在 0.01 处有所不同。
使用函数计算圆柱体体积和表面积的 Python 程序
此 Python 程序允许用户输入半径和高度的值。我们将半径值传递给函数参数,然后它将根据公式计算圆柱体的体积、圆柱体的表面积、圆柱体的侧面积、圆柱体的顶部或底部表面积。
import math
def Vol_Sa_Cylinder(radius, height):
sa = 2 * math.pi * radius * (radius + height)
Volume = math.pi * radius * radius * height
L = 2 * math.pi * radius * height
T = math.pi * radius * radius
print("\n The Surface area of a Cylinder = %.2f" %sa)
print(" The Volume of a Cylinder = %.2f" %Volume)
print(" Lateral Surface Area of a Cylinder = %.2f" %L)
print(" Top OR Bottom Surface Area of a Cylinder = %.2f" %T)
Vol_Sa_Cylinder(6, 4)
圆柱体的体积和表面积输出
The Surface area of a Cylinder = 376.99
The Volume of a Cylinder = 452.39
Lateral Surface Area of a Cylinder = 150.80
Top OR Bottom Surface Area of a Cylinder = 113.10
>>> Vol_Sa_Cylinder(3, 5)
The Surface area of a Cylinder = 150.80
The Volume of a Cylinder = 141.37
Lateral Surface Area of a Cylinder = 94.25
Top OR Bottom Surface Area of a Cylinder = 28.27
>>>
首先,我们使用以下语句导入了 math 库。这将使我们能够使用 math.pi 等数学函数。如果您未包含此行,则 math.pi 将引发错误。
import math
步骤 2:我们使用 def 关键字定义了带有两个参数的函数。这意味着用户将输入圆柱体的半径和高度。
步骤 3:我们计算了圆柱体的体积、表面积、侧面积、顶部或底部表面积,正如我们在第一个示例中解释的那样。
注意:我们可以直接在 .py 文件中调用带有参数的函数,也可以从 Python shell 调用它。请不要忘记函数参数。