-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcircle_class.py
More file actions
39 lines (29 loc) · 993 Bytes
/
circle_class.py
File metadata and controls
39 lines (29 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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]}")