📜  circle python programe - Python (1)

📅  最后修改于: 2023-12-03 14:40:05.662000             🧑  作者: Mango

Circle Python Program - Python

Introduction

In this Python program, we will calculate the area and circumference of a circle. The user will be prompted to enter the radius of the circle.

Code
import math

# prompt user to enter radius of circle
radius = float(input("Enter radius of circle: "))

# calculate area of circle
area = math.pi * radius ** 2

# calculate circumference of circle
circumference = 2 * math.pi * radius

# display area and circumference of circle
print(f"Area of circle with radius {radius} = {area:.2f}")
print(f"Circumference of circle with radius {radius} = {circumference:.2f}")
Explanation

The above Python program calculates the area and circumference of a circle. We start by importing the math module to be able to use the value of pi.

Next, we prompt the user to enter the radius of the circle using the input() function. We convert the input to a float using the float() function and store it in the variable radius.

We then use the formula area = pi * r^2 to calculate the area of the circle and store it in the variable area. We use the ** operator to raise the value of radius to the power of 2.

We then use the formula circumference = 2 * pi * r to calculate the circumference of the circle and store it in the variable circumference.

Finally, we display the area and circumference of the circle using the print() function. We use f-strings to include the values of radius, area, and circumference in the output. We use the :.2f format specifier to display the values of area and circumference with 2 decimal places.