Assignment


1️⃣ Classes, Objects, and Constructors (6 Assignments)

Assignment 1: Creating a Simple Class & Object

🔹 Task: Define a Car class with attributes brand and model, then create an object. 🔹 Example Snippet:

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

car1 = Car("Toyota", "Corolla")
print(car1.brand, car1.model)

Assignment 2: Adding a Method to a Class

🔹 Task: Modify the Car class to include a method display_info(). 🔹 Example Snippet:

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def display_info(self):
        print(f"Car: {self.brand} {self.model}")

car1 = Car("Honda", "Civic")
car1.display_info()

Assignment 3: Using Default Constructor Values

🔹 Task: Set default values for brand and model if no values are provided. 🔹 Example Snippet:


Assignment 4: Modifying Object Attributes

🔹 Task: Change an object’s attribute value after creation. 🔹 Example Snippet:


Assignment 5: Using a Class Variable

🔹 Task: Add a class variable wheels and access it via objects. 🔹 Example Snippet:


Assignment 6: Deleting an Object Attribute

🔹 Task: Remove an attribute from an object and handle errors. 🔹 Example Snippet:


2️⃣ Inheritance, Polymorphism, Encapsulation (6 Assignments)

Assignment 7: Implementing Single Inheritance

🔹 Task: Create a Vehicle parent class and a Car child class. 🔹 Example Snippet:


Assignment 8: Using Method Overriding in Inheritance

🔹 Task: Override a method in a child class. 🔹 Example Snippet:


Assignment 9: Implementing Multiple Inheritance

🔹 Task: Create a class that inherits from two parent classes. 🔹 Example Snippet:


Assignment 10: Implementing Encapsulation (Private Variables)

🔹 Task: Use a private variable inside a class. 🔹 Example Snippet:


Assignment 11: Using Getters and Setters

🔹 Task: Modify a private variable using getter and setter methods. 🔹 Example Snippet:


Assignment 12: Demonstrating Polymorphism with Methods

🔹 Task: Define two classes with the same method name but different implementations. 🔹 Example Snippet:


3️⃣ Magic Methods (str, repr, call) (4 Assignments)

Assignment 13: Using __str__ Method

🔹 Task: Define a class with __str__ for readable output. 🔹 Example Snippet:


Assignment 14: Using __repr__ Method

🔹 Task: Define a class with __repr__ for debugging output. 🔹 Example Snippet:


Assignment 15: Using __call__ Method

🔹 Task: Make a class instance callable like a function. 🔹 Example Snippet:


4️⃣ Monkey Patching (4 Assignments)

Assignment 16: Modifying a Class Method Dynamically

🔹 Task: Change a class method at runtime. 🔹 Example Snippet:


Assignment 17: Monkey Patching an Imported Module Function

🔹 Task: Change time.sleep behavior. 🔹 Example Snippet:


Last updated