Lecture 9 – Object-Oriented Programming (OOP) in Python

Learn Object-Oriented Programming in Python with Lecture 9. Understand classes, objects, constructors, methods, inheritance, and real-world examples. Beginner-friendly and Data Science focused.

What You Will Learn

After completing this lecture, you will be able to:

  • Understand Object-Oriented Programming (OOP) concepts
  • Create classes and objects
  • Use constructors (__init__)
  • Define methods
  • Understand attributes
  • Apply inheritance
  • Relate OOP to real-world & Data Science problems
  • Solve MCQs and practice questions

1) What is Object-Oriented Programming (OOP)?

OOP is a programming style based on real-world objects.

Real-world examples:

  • Student
  • Car
  • Bank Account
  • Laptop

Each object has:

  • Properties (data)
  • Actions (functions)

Python supports OOP to make code:
Organized
Reusable
Easy to manage
Scalable for large projects

2) Class – Blueprint of an Object

A class is like a blueprint.

Example:
class Student:
    pass

This defines a class named Student.

3) Object – Instance of a Class

An object is created from a class.

Example:
class Student:
    pass

s1 = Student()
print(s1)

4) Class with Attributes

class Student:
    name = "Tahir"
    age = 21

s1 = Student()
print(s1.name)
print(s1.age)

Lecture 8 – Modules and Packages in Python

5) Constructor (__init__ method)

The constructor runs automatically when an object is created.

Example:
class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

s1 = Student("Tahir", 21)
s2 = Student("Sara", 20)

print(s1.name, s1.age)
print(s2.name, s2.age)

6) Methods (Functions inside a Class)

class Student:
    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def display(self):
        print("Name:", self.name)
        print("Marks:", self.marks)

s = Student("Tahir", 95)
s.display()

7) Real-Life Example – Bank Account

class BankAccount:
    def __init__(self, owner, balance):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        self.balance -= amount

    def show_balance(self):
        print("Balance:", self.balance)

acc = BankAccount("Tahir", 5000)
acc.deposit(2000)
acc.withdraw(1000)
acc.show_balance()

The Python Tutorial- Documentation Classes

8) Inheritance (VERY IMPORTANT)

Inheritance allows a class to reuse another class.

Parent Class:

class Person:
    def __init__(self, name):
        self.name = name

    def greet(self):
        print("Hello", self.name)

Child Class:

class Student(Person):
    def __init__(self, name, roll_no):
        super().__init__(name)
        self.roll_no = roll_no

s = Student("Tahir", 101)
s.greet()
print(s.roll_no)

9) Data Science Example Using OOP

Dataset Object

class Dataset:
    def __init__(self, values):
        self.values = values

    def mean(self):
        return sum(self.values) / len(self.values)

    def maximum(self):
        return max(self.values)

data = Dataset([10, 20, 30, 40])
print("Mean:", data.mean())
print("Max:", data.maximum())

10) Another Example – Employee System

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def annual_salary(self):
        return self.salary * 12

e = Employee("Sara", 50000)
print(e.annual_salary())

Quiz – Test Your Understanding
Instruction: First read the question and choose your answer. Then scroll down to see the correct answer

What is a class?

a) Object
b) Blueprint
c) Function
d) Variable

Answer: b) Blueprint

Which method runs automatically?

a) main
b) start
c) init
d) __init__

Answer: d) __init__

Inheritance is used to:

a) Delete code
b) Repeat code
c) Reuse code
d) Hide data

Answer: c) Reuse code

What does self represent?

a) Class
b) Method
c) Current object
d) Variable

Answer: c) Current object

Practice Questions
Instruction for students: Read the question, open your Jupyter Notebook, and try to solve it yourself. After that, come back and check the answer.

Create a class Car with attributes brand and year.
class Car:
    def __init__(self, brand, year):
        self.brand = brand
        self.year = year
Create a method to display car details.
def display(self):
    print(self.brand, self.year)
Create a child class ElectricCar.
class ElectricCar(Car):
    pass
Create a class for ML Model Evaluation.
class Model:
    def accuracy(self, correct, total):
        return correct / total

Leave a Reply

Your email address will not be published. Required fields are marked *