您的位置:

Python 程序:求圆的直径周长和面积

用实例编写 Python 程序求圆的直径周长和面积。数学公式为:

  1. 圆的直径= 2r = 2 *半径
  2. 圆的面积是:A = πr = π 半径半径
  3. 圆的周长= 2πr = 2 π 半径

找到圆的直径周长和面积的 Python 程序示例 1

这个 Python 程序允许用户输入圆的半径。通过使用半径值,这个 Python 代码可以找到圆的周长、直径和面积。

提示:参考寻找圆的面积文章,了解寻找蟒圆的面积的各种方法。

# Python Program to find Diameter, Circumference, and Area Of a Circle

PI = 3.14
radius = float(input(' Please Enter the radius of a circle: '))

diameter = 2 * radius
circumference = 2 * PI * radius
area = PI * radius * radius

print(" \nDiameter Of a Circle = %.2f" %diameter)
print(" Circumference Of a Circle = %.2f" %circumference)
print(" Area Of a Circle = %.2f" %area)

计算圆的直径、周长和面积的 Python 程序示例 2

这个 python 代码查找圆的直径、周长和面积同上。但是,我们使用 Python 函数分离了逻辑。

# Python Program to find Diameter, Circumference, and Area Of a Circle
import math

def find_Diameter(radius):
    return 2 * radius

def find_Circumference(radius):
    return 2 * math.pi * radius

def find_Area(radius):
    return math.pi * radius * radius

r = float(input(' Please Enter the radius of a circle: '))

diameter = find_Diameter(r)
circumference = find_Circumference(r)
area = find_Area(r)

print("\n Diameter Of a Circle = %.2f" %diameter)
print(" Circumference Of a Circle = %.2f" %circumference)
print(" Area Of a Circle = %.2f" %area)

蟒蛇直径和圆周输出

 Please Enter the radius of a circle: 12

 Diameter Of a Circle = 24.00
 Circumference Of a Circle = 75.40
 Area Of a Circle = 452.39