|
| 1 | +### 1. **Using `self`** |
| 2 | + |
| 3 | +**Assignment:** |
| 4 | +Create a class `Student` with attributes `name` and `marks`. Use the `self` keyword to initialize these values via a constructor. Add a method `display()` that prints student details. |
| 5 | + |
| 6 | +---------- |
| 7 | + |
| 8 | +### 2. **Using `cls`** |
| 9 | + |
| 10 | +**Assignment:** |
| 11 | +Create a class `Counter` that keeps track of how many objects have been created. Use a class variable and a class method with `cls` to manage and display the count. |
| 12 | + |
| 13 | +---------- |
| 14 | + |
| 15 | +### 3. **Public Variables and Methods** |
| 16 | + |
| 17 | +**Assignment:** |
| 18 | +Create a class `Car` with a public variable `brand` and a public method `start()`. Instantiate the class and access both from outside the class. |
| 19 | + |
| 20 | +---------- |
| 21 | + |
| 22 | +### 4. **Class Variables and Class Methods** |
| 23 | + |
| 24 | +**Assignment:** |
| 25 | +Create a class `Bank` with a class variable `bank_name`. Add a class method `change_bank_name(cls, name)` that allows changing the bank name. Show that it affects all instances. |
| 26 | + |
| 27 | +---------- |
| 28 | + |
| 29 | +### 5. **Static Variables and Static Methods** |
| 30 | + |
| 31 | +**Assignment:** |
| 32 | +Create a class `MathUtils` with a static method `add(a, b)` that returns the sum. No class or instance variables should be used. |
| 33 | + |
| 34 | +---------- |
| 35 | + |
| 36 | +### 6. **Constructors and Destructors** |
| 37 | + |
| 38 | +**Assignment:** |
| 39 | +Create a class `Logger` that prints a message when an object is created (constructor) and another message when it is destroyed (destructor). |
| 40 | + |
| 41 | +---------- |
| 42 | + |
| 43 | +### 7. **Access Modifiers: Public, Private, and Protected** |
| 44 | + |
| 45 | +**Assignment:** |
| 46 | +Create a class `Employee` with: |
| 47 | + |
| 48 | +- a public variable `name`, |
| 49 | + |
| 50 | +- a protected variable `_salary`, and |
| 51 | + |
| 52 | +- a private variable `__ssn`. |
| 53 | + |
| 54 | + |
| 55 | +Try accessing all three variables from an object of the class and document what happens. |
| 56 | + |
| 57 | +---------- |
| 58 | + |
| 59 | +### 8. **The `super()` Function** |
| 60 | + |
| 61 | +**Assignment:** |
| 62 | +Create a class `Person` with a constructor that sets the name. Inherit a class `Teacher` from it, add a subject field, and use `super()` to call the base class constructor. |
| 63 | + |
| 64 | +---------- |
| 65 | + |
| 66 | +### 9. **Abstract Classes and Methods** |
| 67 | + |
| 68 | +**Assignment:** |
| 69 | +Use the `abc` module to create an abstract class `Shape` with an abstract method `area()`. Inherit a class `Rectangle` that implements `area()`. |
| 70 | + |
| 71 | +---------- |
| 72 | + |
| 73 | +### 10. **Instance Methods** |
| 74 | + |
| 75 | +**Assignment:** |
| 76 | +Create a class `Dog` with instance variables `name` and `breed`. Add an instance method `bark()` that prints a message including the dog's name. |
| 77 | + |
| 78 | +---------- |
| 79 | + |
| 80 | +### 11. **Class Methods** |
| 81 | + |
| 82 | +**Assignment:** |
| 83 | +Create a class `Book` with a class variable `total_books`. Add a class method `increment_book_count()` to increase the count when a new book is added. |
| 84 | + |
| 85 | +---------- |
| 86 | + |
| 87 | +### 12. **Static Methods** |
| 88 | + |
| 89 | +**Assignment:** |
| 90 | +Create a class `TemperatureConverter` with a static method `celsius_to_fahrenheit(c)` that returns the Fahrenheit value. |
| 91 | + |
| 92 | +---------- |
| 93 | + |
| 94 | + |
| 95 | +### 13. **Composition** |
| 96 | + |
| 97 | +**Assignment:** |
| 98 | +Create a class `Engine` and a class `Car`. Use composition by passing an `Engine` object to the `Car` class during initialization. Access a method of the `Engine` class via the `Car` class. |
| 99 | + |
| 100 | +---------- |
| 101 | + |
| 102 | +### 14. **Aggregation** |
| 103 | + |
| 104 | +**Assignment:** |
| 105 | +Create a class `Department` and a class `Employee`. Use aggregation by having a `Department` object store a reference to an `Employee` object that exists independently of it. |
| 106 | + |
| 107 | +---------- |
| 108 | + |
| 109 | +### 15. **Method Resolution Order (MRO) and Diamond Inheritance** |
| 110 | + |
| 111 | +**Assignment:** |
| 112 | +Create four classes: |
| 113 | + |
| 114 | +- `A` with a method `show()`, |
| 115 | + |
| 116 | +- `B` and `C` that inherit from `A` and override `show()`, |
| 117 | + |
| 118 | +- `D` that inherits from both `B` and `C`. |
| 119 | + |
| 120 | + |
| 121 | +Create an object of `D` and call `show()` to observe MRO. |
| 122 | + |
| 123 | +---------- |
| 124 | + |
| 125 | +### 16. **Function Decorators** |
| 126 | + |
| 127 | +**Assignment:** |
| 128 | +Write a decorator function `log_function_call` that prints "Function is being called" before a function executes. Apply it to a function `say_hello()`. |
| 129 | + |
| 130 | +---------- |
| 131 | + |
| 132 | +### 17. **Class Decorators** |
| 133 | + |
| 134 | +**Assignment:** |
| 135 | +Create a class decorator `add_greeting` that modifies a class to add a `greet()` method returning "Hello from Decorator!". Apply it to a class `Person`. |
| 136 | + |
| 137 | +---------- |
| 138 | + |
| 139 | +### 18. **Property Decorators: `@property`, `@setter`, and `@deleter`** |
| 140 | + |
| 141 | +**Assignment:** |
| 142 | +Create a class `Product` with a private attribute `_price`. Use `@property` to get the price, `@price.setter` to update it, and `@price.deleter` to delete it. |
| 143 | + |
| 144 | +---------- |
| 145 | + |
| 146 | +### 19. **`callable()` and `__call__()`** |
| 147 | + |
| 148 | +**Assignment:** |
| 149 | +Create a class `Multiplier` with an `__init__()` to set a factor. Define a `__call__()` method that multiplies an input by the factor. Test it with `callable()` and by calling the object like a function. |
| 150 | + |
| 151 | +---------- |
| 152 | + |
| 153 | +### 20. **Creating a Custom Exception** |
| 154 | + |
| 155 | +**Assignment:** |
| 156 | +Create a custom exception `InvalidAgeError`. Write a function `check_age(age)` that raises this exception if `age < 18`. Handle it with `try...except`. |
| 157 | + |
| 158 | +---------- |
| 159 | + |
| 160 | +### 21. **Make a Custom Class Iterable** |
| 161 | + |
| 162 | +**Assignment:** |
| 163 | +Create a class `Countdown` that takes a start number. Implement `__iter__()` and `__next__()` to make the object iterable in a for-loop, counting down to 0. |
0 commit comments