Solution Review: Implement a Rectangle Class

This lesson covers the Python code to create a simple class with a constructor and attributes.

We'll cover the following

Solution:

The solution to the exercise in the previous lesson was very simple. It just had a constructor with four attributes as x1, y1, x2, and y2 as the coordinates of the rectangle. The constructor also checks if the top-left and bottom-right coordinates of the rectangle are correct, i.e., x1 < x2 and y1 > y2. To check the code, we created an object r with the coordinates (2, 7, 8, 4). The solution is shown below:

Get hands-on with 1200+ tech skills courses.