Python Practice Set (Part 10): Introduction to Object-Oriented Programming (OOP)
- Anubhav Somani
- Sep 2
- 2 min read


Building Blueprints: An Introduction to Python OOP
Welcome to Part 10 of our Python practice series! We're now moving from writing scripts to building structured programs. Object-Oriented Programming (OOP) is a paradigm for organizing code around "objects," which can be thought of as custom data types. We create a "class" as a blueprint, and from that blueprint, we create individual "objects." This approach is essential for building complex, manageable, and reusable applications.
1. Your First Class: The Dog (Classes and Methods)
Create a class named Dog. Inside the class, define a simple method called bark. This method should take no arguments (other than self) and should print the string "Woof!". After defining the class, create an instance (an object) of the Dogclass and call its bark method.
2. The Constructor: The init Method (Attributes)
Modify the Dog class from the previous exercise. This time, add an init method (the constructor). This special method should accept two arguments, self and name. Inside the init method, create an instance attribute called name and assign the passed-in name to it. Then, change the bark method to print f"{self.name} says Woof!".
Create an instance of this new Dog class, giving it a name like "Fido".
3. The Car Blueprint (Multiple Attributes and a Return Method)
Create a class called Car. The init method should take self, make, model, and year as arguments and store them as attributes. Add another method called get_description that returns a single formatted string summarizing the car's details, for example: "2023 Tesla Model S".
Create an instance of the Car class and print the result of its get_description method.
4. The BankAccount (Modifying State)
Create a BankAccount class. The init method should initialize two attributes: owner_name and balance (set the initial balance to 0).
Add a deposit method that takes an amount and adds it to the balance.
Add a withdraw method that takes an amount and subtracts it from the balance.
After each deposit or withdrawal, print the new balance.
Create an instance of BankAccount, deposit some money, and then withdraw some.
5. Pretty Printing with str (String Representation)
Enhance the Car class from Question 3. Add a special method called str. This method should return the exact same descriptive string as your get_description method. The str method allows you to define what happens when you try to print() an object of your class directly.
After adding the method, create an instance of the Car and simply use print(my_car_object).
Comments