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

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

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

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

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

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

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

使用数学公式

应用标准数学公式来计算圆柱体的面积和体积。我们需要圆柱体的高度和半径值在公式中替换它们,并获得 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))

输出

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

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

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值