Simple programm using __INIT__
CODE:
class circle:
        def __init__(self,pi):
            self.pi=pi
        def cal_area(self,radius):
            return self.pi*radius**2
c1=circle(3.14)
print(c1.cal_area(2))
OUTPUT:
12.56
CODE: wap to initialise th evalue of attribute s by making use of init method
class circle:
    pi=0
    radius=0
    def __init__(self):
        self.pi=3.14
        self.radius=5
    def cal_area(self):
        print("radius",self.radius)
        return self.pi*self.radius**2
c1=circle()
print("the area of circle is ",c1.cal_area())
OUTPUT:
radius 5 the area of circle is 78.5
CODE:WRITE THE VOLUME OF A BOX
class Box:
    width=0
    height=0
    depth=0
    volume=0
    def __init__(self):
        self.width=5
        self.height=5
        self.depth=5
    def cal_vol(self):
        print("width =",self.width)
        print("height = ",self.height)
        print("depth = ",self.depth)
        return self.width*self.height*self.depth
B1=Box()
print("volume of a cube = ",B1.cal_vol())
OUTPUT:width = 5 height = 5 depth = 5 volume of a cube 125
Comments
Post a Comment