Learn the foundational concepts of OOP with practical Python examples.
Object-Oriented Programming is a programming paradigm based on the concept of objects. It allows structuring code in a way that is modular, reusable, and easier to maintain.
__init__() used for initialization.class Student:
def __init__(self, name, course):
self.name = name
self.course = course
def introduce(self):
print(f"Hi, I'm {self.name} and I'm studying {self.course}.")
# Creating objects
s1 = Student("Ravi", "Python")
s2 = Student("Anjali", "Data Science")
s1.introduce()
s2.introduce()
class Teacher:
def __init__(self, name):
self.name = name
def teach(self):
print(f"{self.name} is teaching")
class MathTeacher(Teacher):
def teach(self):
print(f"{self.name} is teaching Mathematics")
# Inherited behavior
m1 = MathTeacher("Suman")
m1.teach()