PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Programs to Calculate Area and Perimeter of Circle

Python Programs to Calculate Area and Perimeter of Circle

Updated on: April 17, 2025 | Leave a Comment

In this article, we will explore different ways to calculate the Area and Perimeter of a Circle using Python, with detailed explanations and examples.

Table of contents

  • What is a Circle?
  • 1. How to Calculate Area and Circumference of a Circle
  • 2. Calculate Using User Inputs
  • 3. Calculate Using a Function
  • 4. Calculate Area of Circle Using Class (OOP)
  • 5. Calculate Area of Circle Using Lambda
  • Summary

What is a Circle?

A circle is a two-dimensional (2D) shape where all points on the boundary are equidistant from a fixed central point. This fixed point is called the center, and the constant distance from the center to any point on the circle is called the radius (r).

Examples of Circles in Real Life are the wheels of a car, clocks, coins, rings, etc.

circle shape

Understand the Area of a Circle

The area of a circle is the total space enclosed within its boundary. It is measured in square units such as square centimeters (cm²), square meters (m²), or square inches (in²).

Formula to Calculate the Area of a Circle:

Area = πr2

  • A (Area) = Total space inside the circle
  • r (Radius) = Distance from the center to the edge of the circle
  • π(Pi) ≈ 3.14159

For Example, If a circle has a radius of 5 meters:

then Area = π × (52) = 3.14159 × 25 = 78.54 m²

Understand the Perimeter(Circumference) of a Circle

The perimeter of a circle is the total distance around its edge — essentially, it is the circumference of the circle.

There are two formulas you can use, depending on what’s given:

1. Using Radius (r): Perimeter = Circumference = 2πr

2. Using Diameter (d): Perimeter = Circumference = πd

Where:

  • r = Radius (distance from center to edge)
  • d = Diameter (distance across the circle through the center), and d = 2r
  • π (pi) ≈ 3.14159

For Example, If the radius = 7 cm:
Then, the Circumference= 2 × π × 7 ≈ 2 × 3.1416 × 7 = 43.98 cm

If the diameter = 10 m:
Then, the Circumference = π × 10 = 31.42 m

1. How to Calculate Area and Circumference of a Circle

Below are the steps to calculate the Area and Perimeter of Circle in Python:

  1. Import the math Module

    Python has a built-in math module that provides the constant π (pi).
    To use π, import the module:
    import math

  2. Define the Radius

    Assign a value to the radius of the circle.

  3. Apply the Formula to calculate the Area

    Use the formula A = πr².
    In Python: area = math.pi * radius ** 2

    Here, math.pi provides the value of π and radius ** 2 calculates r².
    The Exponentiation operator (**) in Python raises a number to the power of another number. For Example, print(3 ** 2) # Output: 9 (3²)

  4. Apply the Formula to calculate the Perimeter

    The formula to calculate the Perimeter of a circle is:
    perimeter = 2 * math.pi * radius

  5. Mention the Correct Unit

    If the measurements are in meters, the area will be in square meters (m²), and the Perimeter will be in meters (m).

  6. Display the result

    Display Area and Perimeter using the print() function

Code Example

import math  # Step 1: Import the math module

# Step 2: Define radius
radius = 5

# Step 3.1: Calculate area
area = math.pi * radius ** 2

# Step 3.2: Calculate the perimeter
perimeter = 2 * math.pi * radius

# Step 4: Display the result
print(f"Area is {area} square units")
print(f"Perimeter is {perimeter} units")Code language: Python (python)

Output:

Area is 78.53981633974483 square units
Perimeter is 31.41592653589793 units

2. Calculate Using User Inputs

If you want to calculate area using the radius of the circle specified by user then use the input() function.

The input() function allow us to get input from the user while a Python script is running. It pauses the execution of your program and waits for the user to type something into the console (or terminal) and press the Enter key.

The input() always returns the user’s input as a string, regardless of whether the user typed numbers, letters, or symbols. So you must convert it to numbers using the float() function.

Code Example

import math  # Step 1: Import the math module

# Step 2: Get user input for radius
radius = float(input("Enter the radius of the circle: "))

# Step 3.1: Calculate area
area = math.pi * radius ** 2

# Step 3.2: Calculate the perimeter
perimeter = 2 * math.pi * radius

# Step 4: Display the result
print(f"Area is {area} square units and Perimeter is {perimeter} units")Code language: Python (python)

Output:

Enter the radius of the circle: 5
Area is 78.53981633974483 square units and Perimeter is 31.41592653589793 units

3. Calculate Using a Function

A function allows us to write the calculation once and use it multiple times without repeating code.There are multiple advantages of using a function to calculate the area of a circle.

  • If a program requires multiple area calculations, a function makes it scalable.
  • Avoid errors: A function helps prevent mistakes by keeping the formula in one place.
  • Easier Debugging & Updating: If the formula needs to be updated or changed, a function makes it easy to modify in one place.

In the example below, we have different functions to calculate area and perimeter, which can be reused for multiple circles without repeating the code.

Code Example

import math     # import math module

# Function to calculate area of a circle
def circle_area(radius):
    return math.pi * radius ** 2  # Formula: A = πr²

# Function to calculate perimeter of a circle
def circle_perimeter(radius):
    return 2 * math.pi * radius

# Function call
radius = 5
area = circle_area(radius)
perimeter = circle_perimeter(radius)

# Display the result
print(f"Area is {area} square units and Perimeter is {perimeter} units")

# Output:
# Area is 78.53981633974483 square units and Perimeter is 31.41592653589793 unitsCode language: Python (python)

4. Calculate Area of Circle Using Class (OOP)

Object-Oriented Programming (OOP) is a structured way to write code by creating classes and objects. A class is a blueprint for creating objects, while an object is an instance of a class.

This approach provides better structure, reusability, and maintainability in your code. This approach is highly useful, especially for larger applications where multiple circles need to be handled efficiently.

The following are the advantages of using OOP to calculate the area and perimeter of a circle:

  • Reusability: If you need to calculate the area of multiple circles, you can create multiple objects.
  • Extensibility: With OOP, we can easily extend the class to calculate the circumference without affecting the existing structure.

Code Example:

import math     # import math module

# Define a class named Circle
class Circle:
    # Constructor to initialize radius
    def __init__(self, radius):
        self.radius = radius  # Store radius

    # Method to calculate area
    def calculate_area(self):
        return math.pi * self.radius ** 2

    # Method to calculate perimeter
    def calculate_perimeter(self):
        return 2 * math.pi * self.radius


# Create an object of Circle class
radius = 5
circle = Circle(radius)

# Call the methods
area = circle.calculate_area()
perimeter = circle.calculate_perimeter()

# Display the result
print(f"Area is {area} square units and Perimeter is {perimeter} units")

# Output:
# Area is 78.53981633974483 square units and Perimeter is 31.41592653589793 unitsCode language: Python (python)

Explanation:

  • Define a Class
    • Define a class named Circle.
    • The class will store the radius of the circle.
  • Create a Constructor (__init__ method)
    • The constructor (__init__) initializes the side when an object is created.
    • The self keyword refers to the current instance of the class.
  • Create a Method to Calculate Area
    • Define a method calculate_area() inside the class.
    • This method returns the area using the formula: Area = πr²
  • Create a Method to Calculate Perimeter
    • Define a method calculate_perimeter() inside the class.
    • This method returns the perimeter using the formula: 2 * math.pi * radius
  • Create an Object of the Class
    • Use circle = Circle(5) to create a circle with: radius = 5
  • Call the Method to get the Area
    • Use circle.calculate_area() to get the area of the object.
    • Use circle.calculate_perimeter() to get the perimeter of the object.
  • Store the result in a variable and display it using print().

5. Calculate Area of Circle Using Lambda

A lambda function in Python is a small, anonymous function that can have multiple arguments but only one expression, which is evaluated and returned. For a more compact implementation, you can use a lambda function.

A lambda function syntax: lambda arguments: expression

  • lambda → Keyword to define the function.
  • arguments → Input parameters (like a normal function).
  • expression → A single operation that returns a result.

Code Example

import math     # import math module

radius = 5
circle_area = lambda radius: math.pi * radius ** 2
circle_perimeter = lambda r: 2 * math.pi * radius

area = circle_area(radius)
perimeter = circle_perimeter(radius)

# Display the result
print(f"Area is {area} square units and Perimeter is {perimeter} units")

# Output:
# Area is 78.53981633974483 square units and Perimeter is 31.41592653589793 unitsCode language: Python (python)

Explanation

  • radius is the input parameter.
  • To calculate the Area:
    • math.pi * radius ** 2 is the calculation expression.
    • circle_area is the name to call the anonymous lambda function.
  • To calculate the Perimeter:
    • 2 * math.pi * radius is the calculation expression.
    • circle_perimeter is the name to call the anonymous lambda function.

Summary

Calculating the area and circumference of a circle in Python is both simple and versatile. Whether you’re using basic arithmetic, taking user input, defining functions, applying object-oriented programming, or using lambda expressions, each method provides a clear way to implement the logic efficiently.

These approaches not only reinforce your understanding of Python fundamentals but also build a strong foundation for solving real-world mathematical problems. Depending on your use case—quick calculations, reusable code, or scalable design—you can choose the method that fits best.

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com