首页 > 编程知识 正文

Python 程序:计算圆柱体体积和表面积

时间:2023-11-25 09:12:54 阅读:309021 作者:SEJH

用例子编写 Python 程序求圆柱体的体积和表面积。在我们进入 Python 程序寻找圆柱体的体积和表面积之前,让我们看看圆柱体的侧面表面积、顶部或底部表面积和体积后面的定义和公式。

圆柱体的表面积

如果我们知道圆柱体的半径和高度,那么我们可以使用公式计算圆柱体的表面积:

圆柱体的表面积= 2πr + 2πrh(其中 r 是半径,h 是圆柱体的高度)。

圆柱体的体积

圆柱体内部的空间量称为体积。如果我们知道圆柱体的高度,那么我们可以用公式计算圆柱体的体积:

圆柱体的体积= πr h

圆柱体的侧面面积= 2πrh

我们可以计算圆柱的顶面或底面面积= πr

寻找圆柱体体积和表面积的 Python 程序

这个 Python 程序允许用户输入半径和高度的值。使用这些值,这个 Python 程序将根据公式计算圆柱体的体积、圆柱体的表面积、圆柱体的横向表面积、圆柱体的顶部或底部表面积。

# Python Program to find Volume & Surface Area of a Cylinder

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 圆柱体的体积=π半径半径高度 圆柱体的体积= 3.14 3 3 5 圆柱体的体积= 141.3

圆柱体的侧面面积为

l = 2rh l = 2 pi radius height l = 2 3.14 3 5 l = 94.2t5

圆柱体的顶面或底面面积为

T =πr T =π半径半径 T = 3.14 3 3 T = 28.26

注:为了计算的目的,我们取π = 3.14 而不是(3.142857142..).因此,以上所有值几乎等于程序输出,但可能相差 0.01。

用函数求圆柱体体积和表面积的 Python 程序

这个 python 程序允许用户输入半径和高度的值。我们将半径值传递给函数参数,然后它将根据公式计算圆柱体的体积、圆柱体的表面积、圆柱体的侧面表面积、圆柱体的顶部或底部表面积。

# Python Program to find Volume & Surface Area of a Cylinder using Functions

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)

Python 圆柱体输出的体积和表面积

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

首先,我们使用以下语句导入了数学库。这将允许我们使用数学函数,如数学π。如果你没有包括这一行,那么数学π将通过一个错误。

import math

步骤 2:我们使用 def 关键字定义了带有两个参数的函数。这意味着,用户将输入圆柱体的半径和高度。

步骤 3:我们正在计算圆柱体的体积、表面积、侧面表面积、顶部或底部表面积,正如我们在第一个例子中所解释的

注意:我们可以用中的参数调用函数。或者我们可以从 python shell 中调用它。请不要忘记函数参数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。