Python 计算圆柱体的体积和表面积

Python 计算圆柱体的体积和表面积

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

一个圆柱体被定义为一个由两个圆连接的矩形表面的三维对象。圆柱体的特殊之处在于,虽然它只使用两个维度进行测量,即高度和半径,但它被认为是一个三维图形,因为它是在xyz坐标轴上测量的。

Python 计算圆柱体的体积和表面积

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

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

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

Lateral Surface Area: 2πrh
Total Surface Area: 2πr(r + h)

一个圆柱体的体积通过以下公式计算 –

Volume: πr2h

输入输出场景

计算圆柱体的表面积和体积的Python程序将提供以下输入输出场景 –

假设圆柱的高度和半径如下所示,输出为 –

Input: (6, 5) // 6 is the height and 5 is the radius
Result: Lateral Surface Area of the cylinder: 188.49555921538757
Total Surface Area of the cylinder: 345.57519189487726
Volume of the cylinder: 471.2388980384689

使用数学公式

标准数学公式用于计算圆柱的表面积和体积。我们需要圆柱体的高度和半径值,将它们代入公式,以获得这个三维物体的表面积和体积。

示例

在下面示例代码中,我们从Python导入math库以使用圆周率常数来计算面积和体积。输入被视为圆柱体图形的高度和半径。

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))

输出

在执行上述代码之后,输出如下:

Lateral Surface Area of the cylinder: 188.49555921538757
Total Surface Area of the cylinder: 345.57519189487726
Volume of the cylinder: 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代码后,输出显示出表面积和体积−

Lateral Surface Area of the cylinder: 175.92918860102841
Total Surface Area of the cylinder: 276.46015351590177
Volume of the cylinder: 351.85837720205683

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程