Python OOPs
Python OOPs
Source: www.youtube.com/@RishabhMishraOfficial
Chapter - 20
OOPs in Python
• What is OOPs
• Why OOP is required
• Class and Object
• Attributes and Methods
• __init__ Method (Constructor)
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
OOPs in Python
Two ways of programming in Python:
1) Procedural Programming,
2) OOPs
P y t h o n N o t e s b y R i s h a b h M i s h ra
OOPs Example in Python
Example: Constructing a building
Class: Blueprint for a floor.
Object: Actual house built from the blueprint. Each house (object) can have different
features, like paint color or size, but follows the same blueprint.
Why OOPs?
• Models Real-World Problems:
Mimics real-world entities for easier understanding.
• Code Reusability:
Encourages reusable, modular, and organized code.
• Easier Maintenance:
OOP organizes code into small, manageable parts (classes and objects). Changes in
one part don’t impact others, making it easier to maintain.
• Encapsulation:
Encapsulation protects data integrity and privacy by bundling data and methods
within objects.
• Flexibility & Scalability:
OOP makes it easier to add new features without affecting existing code.
P y t h o n N o t e s b y R i s h a b h M i s h ra
OOPs – Question
Write a Python program to:
1. Define a Student class with attributes name, grade, percentage, and team.
• Include an __init__ method to initialize these attributes.
• Add a method student_details that prints the student’s details in the format:
"<name> is in <grade> grade with <percentage>%, from team <team>".
2. Create two teams (team1 and team2) as string variables.
3. Create at least two student objects, each belonging to one of the teams.
4. Call the student_details method for each student to display their details.
P y t h o n N o t e s b y R i s h a b h M i s h ra
# Object creation
student1 = Student("Madhav", 10)
print(student1.get_grade()) # Output: Madhav is in grade 10.
P y t h o n N o t e s b y R i s h a b h M i s h ra
Encapsulation in Python: Restricting direct access to attributes & methods
Encapsulation restricts access to certain attributes or methods to protect the data and
enforce controlled access.
Example
class Student:
def __init__(self, name, grade, percentage):
self.name = name
self.grade = grade
self.__percentage = percentage # Private attribute
(hidden)
def get_percentage(self): # Public method to access the
private attribute
return self.__percentage
P y t h o n N o t e s b y R i s h a b h M i s h ra
class GraduateStudent(Student): # GraduateStudent inherits
from Student
def student_details(self):
super().student_details()
print(f"Stream: {self.stream}")
Example
class GraduateStudent(Student):
def student_details(self): # Same method as in parent
class
print(f"{self.name} is a graduate student from final
year.")
# Polymorphism in action
student1 = Student("Madhav", 10, 98)
grad_student = GraduateStudent("Sudevi", 12, 99, "PCM")
student1.student_details()
# Output: Madhav is in 10 grade with 98%
P y t h o n N o t e s b y R i s h a b h M i s h ra
grad_student.student_details()
# Output: Sudevi is a graduate student from final year.
P y t h o n N o t e s b y R i s h a b h M i s h ra