Coding a rectangle class in Python
Level: Beginner
Your task for today is to create a Python class that models a rectangle. This is a great exercise to practice defining a class, initializing attributes, and writing simple methods — foundational skills for working with object-oriented code.
📝 Project Task
The program should:
Define a class named
RectangleThe class should have:
Two instance attributes:
widthandheight(set via the constructor)A method
area()that returns the area of the rectangleA method
perimeter()that returns the perimeter of the rectangleA method
is_square()that returnsTrueif width and height are equal
Bonus (optional):
Add
__str__()to print something like:Rectangle(4 x 5)Add input validation to ensure width and height are positive
This project introduces you to writing your own classes — a core concept in building reusable, modular, and real-world Python code.
📌 Expected Output
r1 = Rectangle(4, 5)
print(r1.area()) # 20
print(r1.perimeter()) # 18
print(r1.is_square()) # False
r2 = Rectangle(6, 6)
print(r2.is_square()) # True
💻 Launch This Project in Colab
Open the interactive Google Colab notebook for today’s project — with full instructions, hints, and solutions.
Click the button below to start coding — no setup needed:


