← All coding practice

Coding Practice · Beginner · October 11, 2026 · 5 min read

Python Coding Exercise #22: Area and Circumference of a Circle

Part of the series Python Practice Exercise — part 22

Before you start: Know input() and float() · Know arithmetic operators and f-strings

The question

Write a program that inputs the radius of a circle, and displays its area and circumference.

  • Area of a circle = π * radius * radius
  • Circumference = 2 * π * radius

Import the value of pi (π) from the math module.

For example, for a circle with a radius of 5:

What is radius of a circle: 5
Area of a circle = 78.54 square units
Circumference = 31.42 units

My Solution

from math import pi

radius_of_circle = float(input("What is radius of a circle: "))
area = round(pi * radius_of_circle ** 2, 2)
circumference = round(2 * pi * radius_of_circle, 2)
print(f"Area of a circle = {area} square units")
print(f"Circumference = {circumference} units")

How My Solution Works

1. Import pi from the math module

from math import pi

A module is a file of ready-made Python code that you can bring into your program. Python’s math module contains mathematical tools, including the constant pi. The line from math import pi takes just that one value from the module and lets you use it by the plain name pi. Its value is 3.141592653589793, which is far more precise than typing 3.14 yourself.

There is a second way to do the same thing:

import math

print(math.pi)   # 3.141592653589793

Here you import the whole module and write math.pi each time. Both styles are common. The from math import pi form is shorter when you only need one or two things.

2. Read the radius as a float

float(input("What is radius of a circle: ")) converts the typed text to a decimal number. A float makes sense here, because a radius doesn’t have to be a whole number. The user can type 5 or 2.5.

3. Calculate the area

area = round(pi * radius_of_circle ** 2, 2)

The ** operator means “to the power of”, so radius_of_circle ** 2 is the radius squared, the same as radius_of_circle * radius_of_circle. Both give the same answer. The book writes the formula as π × radius × radius, and ** 2 is a compact way to say the same thing.

Python follows the usual order of operations, and ** is done before *. So pi * radius_of_circle ** 2 is read as pi * (radius_of_circle ** 2), which is exactly the formula π × r².

4. Calculate the circumference

circumference = round(2 * pi * radius_of_circle, 2)

This is the formula 2 * π * radius written directly. The circumference is the distance around the circle, so it’s a length, and the area is the amount of surface inside it.

5. Round the results

Without round(), a radius of 5 gives long numbers:

print(pi * 5 ** 2)    # 78.53981633974483
print(2 * pi * 5)     # 31.41592653589793

round(value, 2) keeps two decimal places (round() is explained in Built-in Functions in Python), giving the tidy 78.54 and 31.42. (Rounding was also used for prices in Exercise #14.)

6. Print with units

The output says square units for the area and just units for the circumference. That’s deliberate. An area is measured in square units (for example square centimetres), while a length like the circumference is measured in plain units.

Here are three more runs:

RadiusArea (π * r * r)Circumference (2 * π * r)
13.146.28
2.519.6315.71
10314.1662.83

A mistake to avoid: the order of operations

It’s easy to get the area formula wrong by adding brackets in the wrong place:

print(round((pi * 5) ** 2, 2))   # 246.74  (wrong)
print(round(pi * 5 ** 2, 2))     # 78.54   (correct)

In the wrong version, (pi * 5) ** 2 squares π times the radius, which means π is squared too, so the answer is far too large. The formula needs only the radius squared. When a calculation gives a surprising number, check where the brackets are.

An alternative: format the output instead of rounding

round() has one quirk: it drops trailing zeros. For a radius of 6, the program prints:

Area of a circle = 113.1 square units
Circumference = 37.7 units

The numbers are correct, but you might prefer 113.10 and 37.70, with two decimals every time. An f-string can do this with the format :.2f, which means “show this as a float with exactly two decimal places”:

import math

radius = float(input("What is radius of a circle: "))
area = math.pi * radius * radius
circumference = 2 * math.pi * radius

print(f"Area of a circle = {area:.2f} square units")
print(f"Circumference = {circumference:.2f} units")

For a radius of 6, this prints 113.10 and 37.70. For radius 5, it prints the same 78.54 and 31.42 as My Solution. This version also shows the second import style, math.pi, and writes the area as radius * radius. (The :.2f format also appeared in Exercise #11.)

The difference is subtle. round() changes the value, while :.2f only changes how the value is displayed. For this exercise, either is fine.

Key takeaways

  • A module is a collection of ready-made code. from math import pi brings in just the value of π.
  • import math followed by math.pi is the other common way to use it.
  • ** means “to the power of”. radius ** 2 is the same as radius * radius.
  • ** is calculated before *, so pi * radius ** 2 is π × r². Putting brackets around pi * radius would be wrong.
  • Use float() for a radius, since it doesn’t have to be a whole number.
  • round(value, 2) keeps two decimal places but drops trailing zeros. f"{value:.2f}" always shows exactly two.
  • Areas are measured in square units, and lengths such as the circumference in plain units.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from Ultimate Python Programming: Learn Python with 650+ Programs, 900+ Practice Questions, and 5 Projects by Deepali Srivastava, published by BPB Publications. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · math-module · input · float · arithmetic