Python Code Example: Circumference and circular area

The user is first prompted to enter a radius. Then the circumference and the area of the circle are calculated, rounded and output on the screen.

Code Explanation

LineDescription
1Import the math module to access the mathematical functions and numbers, in this case the number PI
3Prompts the user to enter a radius as an integer value. The value is stored in the variable named r
5Calculates the circumference of the circle with the formula 2 * math.pi * r. The number PI can be retrieved via the math module. The result of this calculation is stored in the variable circumference
7Calculates the circular area of the circle with the formula math.pi * pow(r, 2). The pow() function calculates the power of a number by raising the first argument to the second argument. The result of this calculation is stored in the variable area
9Outputs the circumference rounded to 2 decimal places
10Outputs the circular area rounded to 2 decimal places
import math

r = int(input("Please enter radius: "))

circumference = 2 * math.pi * r

area = math.pi * pow(r, 2)

print("Circumference: " + repr(round(circumference, 2)))
print("Circular Area: " + repr(round(area, 2)))
Output
Please enter radius: 5
Circumference: 31.42
Circular Area: 78.54