How to calculate area of a circle in Python

In this python tutorial, you will learn about how to calculate area of a circle in Python, you can check the Python program to calculate area of a circle and, also we will check:

  • Python program to calculate the area of a circle
  • Python program to find the area of a circle using function
  • Python program to find the area of a circle using the math module
  • Python program to calculate the area and circumference of a circle
  • Python program to calculate the area and circumference of a circle using an inbuilt math module
  • Python program to find the area and perimeter of a circle
  • Python program to calculate the area of a circle using class
  • Python program to calculate the area and perimeter of a circle using class

Python program to calculate area of a circle

Let see python program to calculate the area of a circle.

  • Firstly, we will take input from the user using the input() function for radius and store it in a variable.
  • We can use constant to store the value of ‘pi’.
  • Now, we will calculate the area of a circle by using the formula area = PI * r * r.
  • And at last, print the area of a circle to get the output.

Example:

PI = 3.14
r = float(input("Enter the radius of a circle:"))
area = PI * r * r
print("Area of a circle = %.2f" %area)

You can refer to the below screenshot to see the output for python program to calculate the area of a circle

Python program to calculate the area of a circle
Python program to calculate the area of a circle

The above code, we can use to calculate area of a circle in Python.

Also, read, Python program to find an area of a rectangle and How to find area of a triangle in Python?

Python program to find area of a circle using function

Here, we will see python program to find the area of a circle using function.

  • In this example, I have defined a function as def findArea(r).
  • We can store the value PI = 3.14 which is fixed.
  • The function is returned as return PI * (r * r)
  • The function findArea(6) is called and it will print the output.
READ:  Initialize Python dictionary with keys and values

Example:

def findArea(r):
    PI = 3.14
    return PI * (r*r);
print("Area of circle = %.6f" % findArea(6));

You can refer to the below screenshot to see the output for the python program to find the area of a circle using the function.

Python program to find the area of a circle using function
Python program to find the area of a circle using function

The above python code to find area of a circle using function in Python.

Find area of a circle using Python math module

Now, we will see python program to find area of a circle using the math module.

  • In this example, we will import math module which is an inbuilt module.
  • We will take the radius as an input from the user.
  • Now, we will calculate the area of a circle by using the formula area = math.pi * r * r. The value of “pi” is taken from the “math” module.
  • And at last, print the area of a circle to get the output.

Example:

import math
r = float(input("Enter the radius of a circle:"))
area = math.pi * r * r
print("Area of a circle = %.2f" %area)

You can refer to the below screenshot to see the output for the python program to find the area of a circle using the math module.

Python program to find the area of a circle using the math module
Python program to find the area of a circle using the math module

The above code, we can use to find area of a circle using Python math module.

Also read, How to Create Countdown Timer using Python Tkinter.

Python program to calculate area and circumference of a circle

Let’s see python program to calculate the area and circumference of a circle.

  • Firstly, we will take input from the user using the input() function for radius and store it in a variable.
  • We can use constant to store the value of ‘pi’.
  • Now, we will calculate the area of a circle by using the formula area = PI * r * r.
  • And for calculating the circumference of a circle we will use the formula circumference = 2 * PI * r.
  • And at last, print the area and circumference of a circle to get the output. The value is formatted up to two decimal places using %.2f.

Example:

PI = 3.14
r = float(input(' Please Enter the radius of a circle: '))
area = PI * r * r
circumference = 2 * PI * r
print(" Area Of a Circle = %.2f" %area)
print(" Circumference Of a Circle = %.2f" %circumference)

You can refer to the below screenshot to see the output for the python program to calculate the area and circumference of a circle.

Python program to calculate the area and circumference of a circle
Python program to calculate the area and circumference of a circle

This is the Python program to calculate area and circumference of a circle.

READ:  Command errored out with exit status 1 python

You may like, Python program to find the area of square.

Python program to calculate the area and circumference of a circle using an inbuilt math module

Here, we will see python program to calculate the area and circumference of a circle using an inbuilt math module.

  • Firstly, we will import math module which is an inbuilt module.
  • We will take input from the user using the input() function for radius and store it in a variable.
  • Now, we will calculate the area of a circle by using the formula area = math.pi * r * r.
  • And for calculating the circumference of a circle we will use the formula circumference = 2 * math.pi * r.
  • And at last, print the area and circumference of a circle to get the output. The value is formatted up to two decimal places using %.2f.

Example:

import math
r = float(input(' Please Enter the radius of a circle: '))
area = math.pi * r * r
circumference = 2 * math.pi * r
print(" Area Of a Circle = %.2f" %area)
print(" Circumference Of a Circle = %.2f" %circumference)

You can refer to the below screenshot to see the output for the python program to calculate the area and circumference of a circle using an inbuilt math module.

Python program to calculate the area and circumference of a circle using an inbuilt math module
Python program to calculate the area and circumference of a circle using an inbuilt math module

Python program to find area and perimeter of a circle

Now, we will see python program to find the area and perimeter of a circle

  • Firstly, we will take input from the user using the input() function for radius and store it in a variable.
  • We have used 3.14 the value of pi in the formula.
  • Now, we will calculate the area of a circle by using the formula area = 3.14* r * r.
  • And for calculating the perimeter of a circle we will use the formula perimeter = 2*3.14*r
  • And at last, print the area and perimeter of a circle to get the output.

Example:

r=float(input("Enter the Radius of circle: "))
area = 3.14*r*r
perimeter = 2*3.14*r
print("Area of Circle: ",area)
print("Perimeter of Circle: ",perimeter)

You can refer to the below screenshot to see the output for the python program to find the area and perimeter of a circle.

Python program to find the area and perimeter of a circle
Python program to find the area and perimeter of a circle

The above code, we can use to find area and perimeter of a circle in Python.

Python program to calculate area of a circle using class

Let see python program to calculate the area of a circle using class.

  • In this example, we have created a class named “Circle” and it has an attribute radius as r.
  • The constructor of the class initiates the attribute using the __init__function.
  • We have used 3.14 the value of pi in the formula.
  • The method “area” is created to calculate the area of a given circle.
  • An instance of the class “Circle” is created as “obj” and the method is invoked to display the output.
READ:  How to Iterate through a Dictionary in Python with Index? [4 Methods]

Example:

class Circle:
    def __init__(self, r):
        self.radius = r
    def area(self):
        return 3.14 * (self.radius ** 2)
obj = Circle(8)
print("Area of circle:",obj.area())

You can refer to the below screenshot to see the output for the python program to calculate the area of a circle using class.

Python program to calculate the area of a circle using class
Python program to calculate the area of a circle using class

The above Python code is to calculate area of a circle using class.

Python program to calculate area and perimeter of a circle using class

Here, we will see python program to calculate the area and perimeter of a circle using class

  • In this example, we have created a class named “Circle” and it has an attribute radius as r.
  • The constructor of the class initiates the attribute using the __init__function.
  • We have used 3.14 the value of pi in the formula.
  • The two methods “area” and “perimeter” are created to calculate the area of a given circle.
  • An instance of the class “Circle” is created as “obj” and the method is invoked to display the output.

Example:

class Circle:
    def __init__(self, r):
        self.radius = r
    def area(self):
        return 3.14 * (self.radius ** 2)
    def perimeter(self):
        return 2*3.14*self.radius
obj = Circle(3)
print("Area of circle:",obj.area())
print("Perimeter of circle:",obj.perimeter())

You can refer to the below screenshot to see the output for the python program to calculate the area and perimeter of a circle using class.

Python program to calculate the area and perimeter of a circle using class
Python program to calculate the area and perimeter of a circle using class

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python programs to calculate the area of a circle. Also, we covered these below topics:

  • How to calculate the area of a circle in Python
  • How to find the area of a circle using a function in Python
  • How to find the area of a circle using the math module in Python.
  • How to calculate the area and circumference of a circle in Python.
  • Python program to calculate the area and circumference of a circle using an inbuilt math module
  • How to find the area and perimeter of a circle in Python
  • How to calculate the area of a circle using class in Python
  • How to calculate the area and perimeter of a circle using class in Python.