用例子編寫 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 中調用它。請不要忘記函數參數
原創文章,作者:VZKYO,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/130640.html