Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Area and Circumference of a Circle

Mathematical Programs · Python example

Area and Circumference of a Circle

Import the math module and use pi to calculate circle measurements.

Open the compilerAll examples
beginnermathpifloat

Python's math module provides mathematical constants and functions. Here math.pi supplies an accurate value of π.

example.py
import math

radius = 4.5
area = math.pi * radius ** 2
circumference = 2 * math.pi * radius

print(f"Area: {area:.2f}")
print(f"Circumference: {circumference:.2f}")

How the code works

  • The area uses πr².
  • The circumference uses 2πr.
  • Both results are rounded to two decimal places only for display.

Key ideas

  • import math loads the standard library module.
  • ** is the exponentiation operator, so radius ** 2 squares the radius.
  • Formatting with .2f keeps the output readable.

Try changing it

  • Compute the volume of a sphere with 4/3 × π × r³.
  • Ask the user for the radius and convert it with float().
  • Compare math.pi with the approximation 3.14.

Related examples

beginner

Calculate Simple Interest

Use arithmetic operators and formatted floats to compute interest.

intermediate

Quadratic Equation Roots

Use the quadratic formula and math.sqrt to find real roots.

Previous exampleCalculate Simple InterestNext exampleEven or Odd Checker

Expected output

Area: 63.62
Circumference: 28.27

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects