Object Oriented Programming in Python
Object Oriented Programming (OOP) is a way of writing programs by grouping related data and the functions that work on that data into a single unit called ...
The Idea Behind OOP
Object Oriented Programming (OOP) is a way of writing programs by grouping related data and the functions that work on that data into a single unit called an object. A class is the blueprint, and an object is a real item built from that blueprint. For example, a Student class can describe what every student has (name, marks) and can do (display result), and then you create many Student objects from it. OOP makes large programs easier to organise, reuse and maintain.
Four Key Concepts
The four pillars are: encapsulation (keeping data and methods together inside a class and controlling access to them), inheritance (a child class reusing and extending a parent class), polymorphism (the same method name behaving differently for different classes), and abstraction (hiding complex details and showing only what is needed). In Python a class is defined with the class keyword, the special method __init__ acts as the constructor that runs when an object is created, and self refers to the current object.
Example and Exam Tip
Example: class Student: def __init__(self, name): self.name = name then s = Student('Asha') creates an object whose s.name is 'Asha'. Exam tip: be able to define a simple class with a constructor and one method, and explain the difference between a class and an object in one line each. Inheritance questions often ask you to create a child class that adds a new method while reusing the parent's data. Always remember self as the first parameter of every method, since forgetting it is a frequent error.