Question

In: Computer Science

Circle Object Python program Compute the area and perimeter of a circle using Python classes. take...

Circle Object Python program

Compute the area and perimeter of a circle using Python classes. take the radius from the user and find the area and perimeter of a circle

please use comments to explain so i better understand

Solutions

Expert Solution

I have written the comments along with the code please go through them.

Code Snippet:

import math # for using pi

class Circle():
    def __init__(self,radius): # init method or constructor    
        """ Initializing Circle class with radius as argument 
        """
        self.radius = radius # assigning radius to data member of circle class

    def area(self):
        """ Function calculates the circle area
            Formula = pi * radius**2
        """
        circle_area = math.pi * (self.radius**2) # calculating area of circle

        return circle_area
    
    def circumference(self):
        """ Function calculate the circumference of the circle
            Formula = 2 * pi* radius
        """    
        circle_circumference = 2 * math.pi * self.radius # calculating circumference of circle

        return circle_circumference


def main():
    circle = Circle(radius=float(input("Enter circle radius to calculate Area and Circumference: ")))
    print("Area of Circle with radius {} is {}".format(circle.radius, circle.area()))
    print("Circumference of Circle with radius {} is {}".format(circle.radius, circle.circumference()))


if __name__ == "__main__":
    main()

Images of Code and Outputs:

Console Output:

Leave a comment for any further doubts.


Related Solutions

Calculate the Area and Perimeter for shapes using functions in Python. Your goal is to match...
Calculate the Area and Perimeter for shapes using functions in Python. Your goal is to match the sample output below: Welcome to my area and perimeter calculator ====================================================== Circle : area = 39.82, perimeter = 22.37 Square : area = 85.19, perimeter = 36.92 Rectangle: area = 41.24, perimeter = 34.24 Triangle : area = 21.16 Here is the code to use for your program (Note you may have to type this code into Trinket directly) import math # Add...
write a java program for area and perimeter of right triangle
write a java program for area and perimeter of right triangle
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user...
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user to input the length and width. Calculate the area as length * width. Calculate the perimeter as 2* length + 2* width. Display the area and perimeter. Please use a proper heading in a comment block (name, date, description of the program). Please use comments in your code. Run your program twice using different input. Copy the output and place it at the bottom...
Create a program in java that calculates area and perimeter of a square - use a...
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
In python using tkinter, I want to create a program. The program can have various classes...
In python using tkinter, I want to create a program. The program can have various classes and methods but I want it to have circles, triangles, and squares. Each shape movies in a certain way, like circles can move linearly, triangles can be affected by gravity, and squares can only go up and down. If the shapes get too close to one another then they disappear and a new shape appears somewhere else. I want this program to run continuously.
/* Writing a program that displays the following: calculating area of a circle, calculating area of...
/* Writing a program that displays the following: calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.text.DecimalFormat; import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { char selection; Scanner get = new Scanner(System.in); //having user input DecimalFormat twoDigits = new DecimalFormat("0.00"); //formatting area to two decimal places //display choice of selection System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a...
Compute the wet area, wet perimeter, hydraulic radius and hydraulic depth for the trapezoidal channel section...
Compute the wet area, wet perimeter, hydraulic radius and hydraulic depth for the trapezoidal channel section with the following data: water depth = 5.5 ft, bottom width = 25 ft and side slopes 1:2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT