新聞中心
創(chuàng)新互聯(lián)Python教程:

用例子編寫(xiě) Python 程序求圓柱體的體積和表面積。在我們進(jìn)入 Python 程序?qū)ふ覉A柱體的體積和表面積之前,讓我們看看圓柱體的側(cè)面表面積、頂部或底部表面積和體積后面的定義和公式。
圓柱體的表面積
如果我們知道圓柱體的半徑和高度,那么我們可以使用公式計(jì)算圓柱體的表面積:
圓柱體的表面積= 2πr + 2πrh(其中 r 是半徑,h 是圓柱體的高度)。
圓柱體的體積
圓柱體內(nèi)部的空間量稱為體積。如果我們知道圓柱體的高度,那么我們可以用公式計(jì)算圓柱體的體積:
圓柱體的體積= πr h
圓柱體的側(cè)面面積= 2πrh
我們可以計(jì)算圓柱的頂面或底面面積= πr
尋找圓柱體體積和表面積的 Python 程序
這個(gè) Python 程序允許用戶輸入半徑和高度的值。使用這些值,這個(gè) Python 程序?qū)⒏鶕?jù)公式計(jì)算圓柱體的體積、圓柱體的表面積、圓柱體的橫向表面積、圓柱體的頂部或底部表面積。
# 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。以下語(yǔ)句將要求用戶輸入半徑和高度值,并將用戶輸入值分配給相關(guān)變量。例如第一個(gè)值將分配給半徑,第二個(gè)值分配給高度
radius = float(input('Please Enter the Radius of a Cylinder: '))
height = float(input('Please Enter the Height of a Cylinder: '))接下來(lái),我們將使用它們各自的公式計(jì)算圓柱體的體積、表面積、側(cè)面表面積、頂部或底部表面積:
sa = 2 * PI * radius * (radius + height)
Volume = PI * radius * radius * height
L = 2 * PI * radius * height
T = PI * radius * radius遵循 Python 打印語(yǔ)句將幫助我們打印圓柱體的體積和表面積
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)在這個(gè)尋找圓柱體體積和表面積的 Python 程序中,我們輸入了圓柱體的半徑= 3,高度= 5
圓柱體的表面積為
圓柱體的表面積= 2πr + 2πrh
也可以寫(xiě)成
圓柱體表面積= 2πr (r+h) 圓柱體表面積= 2 PI 半徑(半徑+高度) 圓柱體表面積= 2 3.14 3 (3+5); 圓柱體的表面積= 150.72
圓柱體的體積是
圓柱體的體積= πr h 圓柱體的體積=π半徑半徑高度 圓柱體的體積= 3.14 3 3 5 圓柱體的體積= 141.3
圓柱體的側(cè)面面積為
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
注:為了計(jì)算的目的,我們?nèi)ˇ?= 3.14 而不是(3.142857142..).因此,以上所有值幾乎等于程序輸出,但可能相差 0.01。
用函數(shù)求圓柱體體積和表面積的 Python 程序
這個(gè) python 程序允許用戶輸入半徑和高度的值。我們將半徑值傳遞給函數(shù)參數(shù),然后它將根據(jù)公式計(jì)算圓柱體的體積、圓柱體的表面積、圓柱體的側(cè)面表面積、圓柱體的頂部或底部表面積。
# 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
>>> 首先,我們使用以下語(yǔ)句導(dǎo)入了數(shù)學(xué)庫(kù)。這將允許我們使用數(shù)學(xué)函數(shù),如數(shù)學(xué)π。如果你沒(méi)有包括這一行,那么數(shù)學(xué)π將通過(guò)一個(gè)錯(cuò)誤。
import math步驟 2:我們使用 def 關(guān)鍵字定義了帶有兩個(gè)參數(shù)的函數(shù)。這意味著,用戶將輸入圓柱體的半徑和高度。
步驟 3:我們正在計(jì)算圓柱體的體積、表面積、側(cè)面表面積、頂部或底部表面積,正如我們?cè)诘谝粋€(gè)例子中所解釋的
注意:我們可以用中的參數(shù)調(diào)用函數(shù)。或者我們可以從 python shell 中調(diào)用它。請(qǐng)不要忘記函數(shù)參數(shù)
文章標(biāo)題:Python程序:計(jì)算圓柱體體積和表面積
文章位置:http://m.fisionsoft.com.cn/article/cojcohe.html


咨詢
建站咨詢
