PythonIntroduction to OOP

Introduction to Object-Oriented Programming (OOP) in Python

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects” — data structures that contain data (attributes) and functions (methods) to operate on that data.

Python supports OOP and allows you to design code that is modular, reusable, and easier to maintain.


Key Concepts of OOP

ConceptMeaning
ClassA blueprint for creating objects
ObjectAn instance of a class
AttributeData stored in an object
MethodFunction defined inside a class
EncapsulationHiding internal state and requiring all interaction through methods
InheritanceOne class can inherit attributes and behavior from another class
PolymorphismObjects of different classes can be treated as objects of a common superclass

Why Use OOP?

BenefitDescription
Modular CodeDivide code into logical parts
ReusabilityDefine classes once, reuse multiple times
Easier MaintenanceChange in one place affects all objects of that class
ScalabilityMore natural for complex software design

How OOP Works in Python

  • You define a class using the class keyword.
  • You create objects (instances) from the class.
  • You can add attributes and methods to represent object state and behavior.

Example 1: Basic Class Definition

oop_intro_class.py
class Person:
    def greet(self):
        print("Hello!")
 
p = Person()
p.greet()
output.txt
Hello!

Example 2: Class with Attributes and Methods

oop_intro_attributes.py
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
 
    def introduce(self):
        print(f"My name is {self.name} and I am {self.age} years old.")
 
p1 = Person("Alice", 30)
p1.introduce()
output.txt
My name is Alice and I am 30 years old.

Example 3: Multiple Objects from the Same Class

oop_intro_multiple_objects.py
class Car:
    def __init__(self, model, year):
        self.model = model
        self.year = year
 
    def display_info(self):
        print(f"Model: {self.model}, Year: {self.year}")
 
car1 = Car("Tesla Model S", 2022)
car2 = Car("Ford Mustang", 2020)
 
car1.display_info()
car2.display_info()
output.txt
Model: Tesla Model S, Year: 2022
Model: Ford Mustang, Year: 2020

How Does OOP Improve Code Quality?

AdvantageExample
Logical grouping of data + behaviorClass Car has both model and display_info()
No need for global variablesEach object stores its own state
Reduced code duplicationMethods shared across all objects of a class
Easier to understand large codebasesEach class serves one responsibility

Summary

  • OOP allows you to build software using objects.
  • An object is an instance of a class.
  • Objects contain attributes (data) and methods (behavior).
  • OOP makes Python programs modular, reusable, and scalable.

Next, we will dive deeper into Classes and Objects — how to define them, how they work, and how you can use them to structure your Python programs.