Introduction to Object-Oriented Programming

Python tutorial · PySpark.in

Introduction to Object-Oriented 

Object-Oriented Programming (OOP) is one of the most powerful ways to structure and organize Python programs.
It helps you build applications that are modular, reusable, and easy to maintain.

What is OOP?

OOP (Object-Oriented Programming) is a programming paradigm where your code is organized around objects and classes.

In simple words:
OOP lets you group related data and behavior together.


Why Use OOP?

1. Reusability

Use inheritance to reuse code instead of writing it again.

2. Modularity

Break a complex program into smaller, manageable parts.

3. Encapsulation

Protect and hide internal details of a class.

4. Polymorphism

Use one interface for different behaviors.

5. Easy Maintenance

OOP code is easier to update, expand, and debug.

OOP vs Procedural Programming

Procedural ProgrammingObject-Oriented Programming
Focuses on functionsFocuses on classes & objects
Data and functions are separateData + functions bundled together
Less reusableHighly reusable
Example: add(), subtract()Example: Calculator class

OOP Concepts in Python

Python supports all major OOP concepts:

Class

A blueprint for creating objects.

Object

An instance of a class.

Inheritance

One class inherits the properties and methods of another class.

Polymorphism

Methods with the same name behave differently based on the object.

Encapsulation

Restricting direct access to object data.

Abstraction

Showing only essential features while hiding complex details.

Basic Example in Python

PYTHON
1class Car:
2 def __init__(self, brand, model):
3 self.brand = brand
4 self.model = model
5 def start(self):
6 print(f"{self.brand} {self.model} is starting...")
7# Creating object
8car1 = Car("Tesla", "Model S")
9car1.start()
▶ Output will appear here.

What happens here?

Why OOP is Important in Python?

OOP helps you build applications that are:

More Python tutorials

All tutorials · Try the free PySpark compiler · Practice challenges