-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Circle class which can be constructed by either radius or diameter and has a method to compute the perimeter and area
- Loading branch information
1 parent
3d154de
commit 1b64922
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}") |