Skip to content

Commit

Permalink
Adding circle_class.py
Browse files Browse the repository at this point in the history
Circle class which can be constructed by either radius or diameter and has a method to compute the perimeter and area
  • Loading branch information
RuchirChawdhry committed Mar 26, 2020
1 parent 3d154de commit 1b64922
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions circle_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# script by Ruchir Chawdhry
# released under MIT License
# github.com/RuchirChawdhry/Python
# ruchirchawdhry.com
# linkedin.com/in/RuchirChawdhry

"""
Define a class 'Circle' which can be constructed by either radius or diameter.
The 'Circle' class has a method which can compute the area and perimeter.
"""

import math


class Circle:
def __init__(self, radius=0, diameter=0):
self.radius = radius
self.diameter = diameter

def _area(self):
if self.diameter:
self.radius = self.diameter / 2
return math.pi * (self.radius * self.radius)

def _perimeter(self):
if self.diameter:
self.radius = self.diameter / 2
return 2 * math.pi * self.radius

def compute(self):
return [self._area(), self._perimeter()]


if __name__ == "__main__":
c = Circle(diameter=10)
print(f"Area of Cricle: {c.compute()[0]} \nPerimeter of Circle: {c.compute()[1]}")

0 comments on commit 1b64922

Please sign in to comment.