codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

30 / 68 topics
30Python OOP Concepts31Python Classes & Objects32Python Class Methods & Properties33Python Inheritance & Multiple Inheritance34Python Polymorphism & Operator Overloading35Python Encapsulation
Tutorials/Python Programming/Python OOP Concepts
🐍Python Programming

Python OOP Concepts

Updated 2026-05-15
30 min read

Python OOP Concepts

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. It's based on the concept of "classes," which are blueprints for creating objects, and these objects have attributes and methods that define their behavior. Understanding OOP is crucial for writing modular, reusable, and maintainable code. In this tutorial, we'll explore the core concepts of OOP in Python, including classes, objects, attributes, methods, and the four pillars of OOP: encapsulation, abstraction, inheritance, and polymorphism.

Introduction

Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data, or objects. Objects are instances of classes, which define their properties (attributes) and behaviors (methods). OOP promotes code reusability, modularity, and scalability by allowing developers to create complex systems from simple building blocks.

Core Content

Classes and Objects

A class is a blueprint for creating objects. It defines the attributes and methods that the created objects will have. An object, on the other hand, is an instance of a class.

Creating a Class

Let's start by creating a simple class called Car.

car.py
1class Car:
2 def __init__(self, make, model):
3 self.make = make
4 self.model = model
5
6 def display_info(self):
7 print(f"This car is a {self.make} {self.model}.")

Creating an Object

Now, let's create an object of the Car class.

create_car.py
1from car import Car
2
3my_car = Car("Toyota", "Corolla")
4my_car.display_info()
Output
This car is a Toyota Corolla.

Attributes and Methods

Attributes are variables that hold data about an object, while methods are functions that define the behaviors of an object.

Attributes

In the Car class example, make and model are attributes.

car_attributes.py
1class Car:
2 def __init__(self, make, model):
3 self.make = make
4 self.model = model
5
6 def display_info(self):
7 print(f"This car is a {self.make} {self.model}.")

Methods

In the Car class example, display_info is a method.

car_methods.py
1class Car:
2 def __init__(self, make, model):
3 self.make = make
4 self.model = model
5
6 def display_info(self):
7 print(f"This car is a {self.make} {self.model}.")

The Four Pillars of OOP

OOP has four main pillars: encapsulation, abstraction, inheritance, and polymorphism.

Encapsulation

Encapsulation is the process of bundling data (attributes) and methods that operate on the data into a single unit or class. It also restricts direct access to some of an object's components, which can prevent the accidental modification of data.

encapsulation.py
1class BankAccount:
2 def __init__(self, owner, balance=0):
3 self.owner = owner
4 self.__balance = balance # Private attribute
5
6 def deposit(self, amount):
7 if amount > 0:
8 self.__balance += amount
9 print(f"Added {amount} to the balance")
10 else:
11 print("Deposit amount must be positive")
12
13 def withdraw(self, amount):
14 if 0 < amount <= self.__balance:
15 self.__balance -= amount
16 print(f"Withdrew {amount} from the balance")
17 else:
18 print("Invalid withdrawal amount")
19
20 def get_balance(self):
21 return self.__balance
22
23# Creating a bank account object
24account = BankAccount("Alice", 100)
25account.deposit(50)
26print(account.get_balance())
27account.withdraw(20)
28print(account.get_balance())
Output
Added 50 to the balance
150
Withdrew 20 from the balance
130

Abstraction

Abstraction is the process of hiding complex reality while exposing only the necessary parts. It allows us to simplify complex systems by creating a high-level interface.

abstraction.py
1from abc import ABC, abstractmethod
2
3class Animal(ABC):
4 @abstractmethod
5 def make_sound(self):
6 pass
7
8class Dog(Animal):
9 def make_sound(self):
10 print("Woof!")
11
12class Cat(Animal):
13 def make_sound(self):
14 print("Meow!")
15
16# Creating animal objects
17dog = Dog()
18cat = Cat()
19
20dog.make_sound() # Output: Woof!
21cat.make_sound() # Output: Meow!
Output
Woof!
Meow!

Inheritance

Inheritance is the process by which one class inherits attributes and methods from another class. The class that inherits is called a subclass, and the class being inherited from is called a superclass.

inheritance.py
1class Vehicle:
2 def __init__(self, make):
3 self.make = make
4
5 def start_engine(self):
6 print("Engine started")
7
8class Car(Vehicle):
9 def drive(self):
10 print("Car is driving")
11
12# Creating a car object
13my_car = Car("Toyota")
14my_car.start_engine() # Output: Engine started
15my_car.drive() # Output: Car is driving
Output
Engine started
Car is driving

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables us to perform a single action in different ways.

polymorphism.py
1class Bird:
2 def fly(self):
3 print("This bird can fly")
4
5class Penguin(Bird):
6 def fly(self):
7 print("Penguins cannot fly, but they can swim")
8
9# Creating bird objects
10bird = Bird()
11penguin = Penguin()
12
13bird.fly() # Output: This bird can fly
14penguin.fly() # Output: Penguins cannot fly, but they can swim
Output
This bird can fly
Penguins cannot fly, but they can swim

Practical Example

Let's create a practical example that combines all the concepts we've learned. We'll build a simple library system with classes for Book and Library.

library.py
1class Book:
2 def __init__(self, title, author):
3 self.title = title
4 self.author = author
5
6 def display_info(self):
7 print(f"'{self.title}' by {self.author}")
8
9class Library:
10 def __init__(self):
11 self.books = []
12
13 def add_book(self, book):
14 self.books.append(book)
15
16 def list_books(self):
17 if not self.books:
18 print("No books in the library.")
19 else:
20 for book in self.books:
21 book.display_info()
22
23# Creating a library object
24my_library = Library()
25
26# Adding books to the library
27book1 = Book("1984", "George Orwell")
28book2 = Book("To Kill a Mockingbird", "Harper Lee")
29
30my_library.add_book(book1)
31my_library.add_book(book2)
32
33# Listing all books in the library
34my_library.list_books()
Output
'1984' by George Orwell
'To Kill a Mockingbird' by Harper Lee

Summary

ConceptDescription
ClassA blueprint for creating objects.
ObjectAn instance of a class.
AttributeA variable that holds data about an object.
MethodA function that defines the behaviors of an object.
EncapsulationBundling data and methods into a single unit and restricting access to data.
AbstractionHiding complex reality while exposing only necessary parts.
InheritanceAllowing one class to inherit attributes and methods from another class.
PolymorphismEnabling objects of different classes to be treated as objects of a common superclass.

What's Next?

In the next tutorial, we'll dive deeper into Python classes and objects, exploring more advanced topics such as class variables, static methods, and class methods. This will provide you with a comprehensive understanding of how to design and implement robust object-oriented programs in Python.

Stay tuned for the next installment!


PreviousPython RecursionNext Python Classes & Objects

Recommended Gear

Python RecursionPython Classes & Objects