← Back to Python Course | Chapter 7: OOP | Lesson 1 of 11

Python OOP Introduction

Object-oriented programming is organizing code like real-world things, each with its own facts and actions, such as a dog that has a name and can bark. It keeps big programs neat.

What is OOP?

Object-Oriented Programming organizes code around objects that bundle data and behavior together, rather than passing data between standalone functions -- this mirrors how real-world things work (a car has both properties like color and actions like drive()) and keeps related logic in one place as programs grow.

Example: What is OOP?

python
class Car:
    color = "red"  # attribute shared by every instance
    def drive(self):
        print("Driving")

Car().drive()  # create an object and call its method

Objects and Classes

A class is the blueprint -- it defines what attributes and methods every object of that type will have -- while an object is one concrete instance built from that blueprint, holding its own actual values (two Car objects share the class definition but have different colors).

Example: Objects and Classes

python
class Car:
    def __init__(self, color):
        self.color = color  # each instance stores its own color

car1 = Car("red")
car2 = Car("blue")
print(car1.color, car2.color)  # same class, different attribute values

Encapsulation

Encapsulation keeps an object's internal data bundled with the methods that operate on it and limits direct outside access -- in Python this is a convention rather than an enforced rule, signaled by prefixing attribute names with an underscore or double underscore.

Example: Encapsulation

python
class Account:
    def __init__(self, balance):
        self._balance = balance  # underscore signals "internal use"

print(Account(100)._balance)

Abstraction

Abstraction means exposing only what a user of your class needs to know (like a .drive() method) while hiding the messy implementation details behind it (engine RPM calculations, gear ratios) -- it lets you change the internals later without breaking code that uses the class.

Example: Abstraction

python
class Car:
    def drive(self):
        print("Engine details are hidden from the caller")  # implementation is hidden behind the method

Car().drive()

Benefits of OOP

Because classes can be extended and reused across a codebase instead of copy-pasted, OOP tends to produce more modular code -- fixing a bug or adding a feature in one base class automatically benefits every class that inherits from it.

Example: Benefits of OOP

python
class Animal:
    def eat(self):
        print("Eating")

class Dog(Animal):
    pass

Dog().eat()  # inherited automatically
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Thinking a class and an object are the same thing, when the class is the blueprint and each object is one instance created from it.
  2. Writing many loose functions and global variables that share data, instead of grouping the data and behaviour in one class.
  3. Using classes for everything, even a tiny task that one function would handle more simply.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.