0% found this document useful (0 votes)
5 views3 pages

PP Assignment No 1

The document contains Python code defining classes for various geometric shapes: Triangle, Rectangle, Circle, and Square. Each class includes methods to calculate area and perimeter or circumference. The code also creates instances of these shapes and prints their area and perimeter/circumference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

PP Assignment No 1

The document contains Python code defining classes for various geometric shapes: Triangle, Rectangle, Circle, and Square. Each class includes methods to calculate area and perimeter or circumference. The code also creates instances of these shapes and prints their area and perimeter/circumference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT NO:1

INPUT:-
import math

# Define the Shape classes

class Triangle:

def __init__(self, base, height, side1, side2, side3):

self.base = base

self.height = height

self.side1 = side1

self.side2 = side2

self.side3 = side3

def area(self):

return 0.5 * self.base * self.height

def perimeter(self):

return self.side1 + self.side2 + self.side3


class Rectangle:

def __init__(self, length,

width): self.length = length

self.width = width

def area(self):

return self.length * self.width

def perimeter(self):

return 2 * (self.length + self.width)

class Circle:

def __init__(self, radius):

self.radius = radius
def area(self):

return math.pi * (self.radius ** 2)

def circumference(self):

return 2 * math.pi * self.radius

class Square:

def __init__(self, side):

self.side = side

def area(self):

return self.side * self.side

def perimeter(self):

return 4 * self.side
# Create instances of each shape

triangle = Triangle(base=5, height=10, side1=5, side2=7, side3=8)

rectangle = Rectangle(length=4, width=6)

circle = Circle(radius=7)

square = Square(side=4)

# Call the functions to calculate area and perimeter/circumference

print("Triangle:")

print(f"Area: {triangle.area()} units²")

print(f"Perimeter: {triangle.perimeter()} units")

print("\nRectangle:")

print(f"Area: {rectangle.area()} units²")

print(f"Perimeter: {rectangle.perimeter()} units")

print("\nCircle:")
print(f"Area: {circle.area():.2f} units²")

print(f"Circumference: {circle.circumference():.2f} units")

print("\nSquare:")

print(f"Area: {square.area()} units²")

print(f"Perimeter: {square.perimeter()} units")

OUPUT:-

You might also like