用于计算圆柱体体积和面积的 Python 程序

pythonserver side programmingprogramming更新于 2024/2/22 13:41:00

在本文中,我们将研究一个用于计算圆柱体体积和面积的 Python 程序。

圆柱体 被定义为具有两个与矩形表面相连的圆的 3D 对象。圆柱体的特殊之处在于,即使仅使用两个维度(即高度半径)进行测量,圆柱体仍被视为三维图形,因为它是在 xyz 坐标轴上测量的。

圆柱体的面积有两种计算方式:侧表面面积和总表面面积。 侧表面积只是连接底圆的矩形表面的面积,而总表面积是整个圆柱体的面积。

圆柱体的体积定义为该物体所包含的空间。

计算圆柱体表面积的数学公式如下 -

侧表面积:2πrh
总表面积:2πr(r + h)

圆柱形物体的体积使用以下公式计算 -

体积:πr2h

输入输出场景

用于计算圆柱形物体表面积和体积的python程序将提供以下输入输出场景−

假设圆柱体的高度和半径如下所示,则输出结果为 −

输入:(6, 5) // 6 是高度,5 是半径
结果:圆柱体的侧表面积:188.49555921538757
圆柱体的总表面积:345.57519189487726
圆柱体的体积:471.2388980384689

使用数学公式

使用标准数学公式来计算圆柱体的面积和体积。我们需要圆柱体的高度和半径值,将它们代入公式中,得到 3D 对象的表面积和体积。

示例

在下面的示例代码中,我们从 Python 导入数学库,以在求解面积和体积时使用 pi 常数。输入被视为圆柱图形的高度和半径。

import math #height and radius of the cylinder height = 6 radius = 5 #calculating the lateral surface area cyl_lsa = 2*(math.pi)*(radius)*(height) #calculating the total surface area cyl_tsa = 2*(math.pi)*(radius)*(radius + height) #calculating the volume cyl_volume = (math.pi)*(radius)*(radius)*(height) #displaying the area and volume print("Lateral Surface Area of the cylinder: ", str(cyl_lsa)) print("Total Surface Area of the cylinder: ", str(cyl_tsa)) print("Volume of the cylinder: ", str(cyl_volume))

输出

执行上述代码后,输出显示为 −

圆柱的侧表面积:188.49555921538757
圆柱的总表面积:345.57519189487726
圆柱的体积:471.23889803846896

计算面积和体积的函数

我们还可以利用 python 中的用户定义函数来计算面积和体积。这些 python 函数使用 def 关键字声明,传递的参数是圆柱的高度和半径。让我们看下面的例子。

示例

以下 python 程序利用函数计算圆柱的表面积和体积。

import math def cyl_surfaceareas(height, radius): #calculating the lateral surface area cyl_lsa = 2*(math.pi)*(radius)*(height) print("Lateral Surface Area of the cylinder: ", str(cyl_lsa)) #calculating the total surface area cyl_tsa = 2*(math.pi)*(radius)*(radius + height) print("Total Surface Area of the cylinder: ", str(cyl_tsa)) def cyl_volume(height, radius): #calculating the volume cyl_volume = (math.pi)*(radius)*(radius)*(height) #displaying the area and volume print("Volume of the cylinder: ", str(cyl_volume)) #height and radius of the cylinder height = 7 radius = 4 cyl_surfaceareas(height, radius) cyl_volume(height, radius)

输出

执行python代码后,输出显示表面积和体积−

圆柱的侧面表面积:175.92918860102841
圆柱的总表面积:276.46015351590177
圆柱的体积:351.85837720205683

相关文章