OOP Basics (Object-Oriented Programming)
Object-Oriented Programming (OOP) is a powerful way of organizing Python code. Instead of writing everything in long scripts, OOP allows you to create reusable blocks called classes and objects. OOP makes programs cleaner, easier to maintain, and much more scalable for real-world applications.
What Is OOP?
OOP is a programming style that focuses on creating objects that contain both data and behavior. It is widely used in large applications, automation systems, simulations, AI models, and software frameworks. If you understand OOP well, your Python skills move to a professional level.
Key Concepts of OOP
OOP is built on four major pillars: Classes, Objects, Methods, and Attributes. These elements allow you to model real-world things inside your program.
1. What Is a Class?
A class is a blueprint or template for creating objects. It defines what data an object will store and what actions it can perform. Think of a class like a design plan for building multiple objects of the same type.
class Car:
pass
This is the smallest possible class. It can be expanded with data and functions as needed.
2. What Is an Object?
An object is a real example created from a class. When you make an object, Python allocates memory and gives you a working instance of the class. Each object can have its own values, making your program flexible and dynamic.
my_car = Car()
3. Attributes (Object Data)
Attributes are variables inside a class that store information about the object. For example, a Car object may store its model, color, and speed as attributes.
Example: Adding Attributes
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
c1 = Car("Tesla", "Black")
print(c1.brand, c1.color)
The __init__ method runs automatically when creating an object and sets its initial values.
4. Methods (Object Behaviors)
Methods are functions inside a class. They define what actions an object can perform, such as starting an engine, accelerating, or stopping.
Example: Methods Inside a Class
class Car:
def start(self):
print("Car has started")
c1 = Car()
c1.start()
Methods always take self as the first parameter, which refers to the object calling the method.
5. Combining Attributes and Methods
A class can have multiple attributes and methods working together, just like real objects. This allows Python programs to model real-world behavior accurately and cleanly.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"My name is {self.name} and I am {self.age} years old")
p = Person("Oliver", 30)
p.introduce()
Why Use OOP?
OOP makes programs easier to extend because you can add new classes without breaking old code. It increases code reusability by allowing multiple objects to share the same structure. It also makes your applications cleaner, more organized, and easier to debug.
Real-World Uses of OOP
OOP is used in many areas like game development, web applications, simulations, AI models, and user interfaces. Any system with multiple interacting components benefits from classes and objects. This makes OOP one of the most valuable concepts for professional Python development.
📝 Practice Exercises
Exercise 1
Create a class called Book with attributes title and author, then create an object and print its data.
Exercise 2
Create a class Dog with a method bark() that prints “Woof!”.
Exercise 3
Create a class Student with attributes name and grade, and a method to display them.
Exercise 4
Create two objects from any class and give them different attribute values.
✅ Practice Answers
Answer 1
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
b = Book("Sky Adventures", "Laura Stone")
print(b.title, b.author)
Answer 2
class Dog:
def bark(self):
print("Woof!")
d = Dog()
d.bark()
Answer 3
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def display(self):
print(self.name, self.grade)
s = Student("Emily", "A")
s.display()
Answer 4
class Laptop:
def __init__(self, brand, ram):
self.brand = brand
self.ram = ram
l1 = Laptop("Dell", 16)
l2 = Laptop("HP", 8)
print(l1.brand, l1.ram)
print(l2.brand, l2.ram)