Python程序:计算球体的体积和表面积
球体(实心球)通常被认为是二维图形,即使该图形从其中心在三个平面上可见。其主要原因是球体仅使用其半径进行测量。
然而,空心球被认为是三维图形,因为它在其球形壁内包含空间,并具有两个不同的半径来测量其尺寸。
球形图形只有总表面积,因为只有一个维度可以测量整个物体。计算球体表面积的公式为:
实心球的表面积 − 4πr2
球体的体积被认为是物体圆形壁所容纳的质量。计算球体体积的公式为:
实心球的体积 − 43πr3
空心球的体积 − 43π(R−r)3
其中,R是外球的半径,r是内球的半径。
输入输出场景
让我们来看一些计算球体面积和体积的输入输出场景:
假设要查找的面积和体积是实心球的:
Input: 7 // 7 is the radius Result: Area - 314.1592653589793 Volume - 359.188760060433
假设要查找的面积和体积是空心球的:
Input: (7, 5) // 7 is the outer radius, 5 is the inner radius Result: Area - 314.1592653589793 // Area is same Volume - 100.53096491487338
使用数学公式
在Python程序中,我们使用讨论过的数学公式来计算球体的面积和体积。我们导入math库来使用pi常量。
示例
以下是查找球形3D图形的面积和体积的示例:
import math R = 7 #outer radius of sphere r = 5 # inner radius of sphere #calculating area of solid sphere area = 4*(math.pi)*(r)*(r) #calculating volume of hollow sphere volume_hollow = 4*(math.pi)*(R - r)*(R - r)*(R - r) #calculating volume of solid sphere volume_solid = (1/3)*(math.pi)*(R)*(R)*(R) #displaying output print("Area of the sphere: ", str(area)) print("Volume of the hollow sphere: ", str(volume_hollow)) print("Volume of the solid sphere: ", str(volume_solid))
输出
输出结果如下所示:
Area of the sphere: 314.1592653589793 Volume of the hollow sphere: 100.53096491487338 Volume of the solid sphere: 359.188760060433
计算面积和体积的函数
Python还使用函数来提高程序的模块化。在这种情况下,我们使用一个计算球体面积和体积的函数。
示例
在下面的Python程序中,我们使用用户定义的函数计算实心球和空心球的面积和体积:
import math def sphere_area_volume(R, r): #calculating area of solid sphere area = 4*(math.pi)*(r)*(r) #calculating volume of hollow sphere volume_hollow = 4*(math.pi)*(R - r)*(R - r)*(R - r) #calculating volume of solid sphere volume_solid = (1/3)*(math.pi)*(R)*(R)*(R) #displaying output print("Area of the sphere: ", str(area)) print("Volume of the hollow sphere: ", str(volume_hollow)) print("Volume of the solid sphere: ", str(volume_solid)) R = 7 #outer radius of sphere r = 5 # inner radius of sphere sphere_area_volume(R, r)
输出
执行上述代码后,将获得以下输出:
Area of the sphere: 314.1592653589793 Volume of the hollow sphere: 100.53096491487338 Volume of the solid sphere: 359.188760060433
广告