Object-Oriented Programming with Python - Course Outline
Introduction: Preface
Preface
This book is designed to provide a comprehensive understanding of Object-Oriented Programming (OOP)
using Python.
It begins with foundational programming paradigms and gradually advances to complex OOP concepts such
as polymorphism,
abstraction, and class relationships like aggregation and composition. Each chapter includes theoretical
concepts,
real-world examples, practical activities, and applications to ensure a deep understanding of the subject.
This book is suitable for students, professionals, and anyone who wants to master OOP in Python.
Happy Learning!
Week 1: Introduction to Object-Oriented Programming
Topics:
- Overview of programming paradigms: Procedural, Object-Oriented, Functional.
- Introduction to OOP in Python: Key principles (Encapsulation, Abstraction, Inheritance, Polymorphism).
- Comparison with procedural programming: OOP focuses on objects and data, procedural focuses on
functions.
Lab Activity:
- Write Python programs with basic class and object definitions.
Object-Oriented Programming with Python - Course Outline
Example:
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print(f"Hello, my name is {self.name}")
p = Person("Alice")
p.greet()
Applications:
- Used in software systems requiring modularity and reusability like games, GUI apps, etc.
Week 2: Classes and Objects
Topics:
- Defining classes and creating objects in Python.
- Constructors (__init__) and destructors (__del__).
- Access modifiers (public, protected, private) in Python.
Assignment:
- Implement a class to model a real-world entity like a BankAccount.
Example:
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
Object-Oriented Programming with Python - Course Outline
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
def get_balance(self):
return self.__balance
acc = BankAccount("Bob", 1000)
acc.deposit(500)
print(acc.get_balance())
Applications:
- Real-world modeling in banking, inventory systems, etc.
Week 3: Encapsulation and Data Hiding
Topics:
- Encapsulation: Bundling data and methods that operate on that data.
- Benefits: Security, modularity, and code maintenance.
- Getters and setters using property decorators.
- Data hiding with private attributes (__attribute).
Lab Activity:
- Create a class with private attributes and public getter/setter methods.
Example:
class Employee:
Object-Oriented Programming with Python - Course Outline
def __init__(self, name, salary):
self.__name = name
self.__salary = salary
@property
def salary(self):
return self.__salary
@salary.setter
def salary(self, amount):
if amount > 0:
self.__salary = amount
emp = Employee("John", 5000)
emp.salary = 6000
print(emp.salary)
Applications:
- Ensures sensitive data is protected and controlled.
Week 4: Inheritance
Topics:
- Concept of inheritance: deriving a new class from an existing class.
- Types: Single, multilevel, hierarchical.
- Method overriding and use of super().
Object-Oriented Programming with Python - Course Outline
Assignment:
- Develop a program demonstrating single and multilevel inheritance.
Example:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
super().speak()
print("Dog barks")
d = Dog()
d.speak()
Applications:
- Used in hierarchical class designs, reducing redundancy.
Week 5: Polymorphism (Part 1)
Topics:
- Compile-time polymorphism (method overloading) using default arguments.
- Constructor overloading using default parameters.
Lab Activity:
- Implement method and constructor overloading.
Example:
Object-Oriented Programming with Python - Course Outline
class Math:
def add(self, a, b=0, c=0):
return a + b + c
m = Math()
print(m.add(2))
print(m.add(2, 3))
print(m.add(2, 3, 4))
Applications:
- Simplifies complex logic by allowing method variations.
Week 6: Polymorphism (Part 2)
Topics:
- Runtime polymorphism (method overriding).
- Dynamic method dispatch.
Quiz:
- Differences between compile-time and runtime polymorphism.
Example:
class Shape:
def area(self):
print("Calculating area")
class Circle(Shape):
def area(self):
Object-Oriented Programming with Python - Course Outline
print("Area of Circle")
s = Shape()
c = Circle()
s.area()
c.area()
Applications:
- Flexible and extensible software design.
Week 7: Abstraction and Interfaces
Topics:
- Abstract classes and methods using abc module.
- Interfaces using abstract base classes.
- Differences between abstract classes and interfaces.
Assignment:
- Create and implement an abstract class.
Example:
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
Object-Oriented Programming with Python - Course Outline
class Car(Vehicle):
def start(self):
print("Car started")
v = Car()
v.start()
Applications:
- Enforces contracts in class design, supports multiple developers.
Week 8: Association, Aggregation, and Composition
Topics:
- Class relationships: association, aggregation, composition.
- Association: A general relationship.
- Aggregation: Has-a relationship, both can exist independently.
- Composition: Stronger relationship, parts cannot exist without whole.
Lab Activity:
- Implement aggregation and composition.
Example:
class Engine:
def __init__(self, power):
self.power = power
class Car:
def __init__(self, engine):
Object-Oriented Programming with Python - Course Outline
self.engine = engine
e = Engine("200 HP")
c = Car(e)
print(c.engine.power)
Applications:
- Used in component-based system design.