As Python programs become larger, managing everything with simple variables, conditional statements, loops, and functions can become difficult. A small program may be easy to understand when it contains only a few functions, but a large application can contain hundreds or even thousands of variables and functions. As the size and complexity of a program increase, organizing the code becomes increasingly important.
Object-Oriented Programming (OOP) is a programming approach that helps organize programs around objects and the data and behavior associated with those objects.
Python supports multiple programming paradigms, including Procedural Programming, Object-Oriented Programming, Functional Programming, and Declarative Programming. In this lesson, our focus is on understanding the basic idea and importance of Object-Oriented Programming in Python.
Object-Oriented Programming is a programming paradigm in which programs are designed around objects. An object can represent an entity and contain information about that entity along with behavior related to it.
For example, consider a Student.
A student may have different types of information:
A student can also perform different actions:
In OOP, these related properties and behaviors can be represented together using objects and classes.
This gives us a simple way of thinking about OOP:
Object
|
├── Data
|
└── Behavior
The data describes the object, while the behavior describes what the object can do.
One of the main reasons for using OOP is to help manage the complexity of larger programs.
Suppose we are developing a school management system. In a very small program, we might store information using individual variables:
student_name = "Rahul"
student_age = 20
student_course = "Python"
teacher_name = "Neha"
teacher_subject = "Python"
This approach may work when there are only a few records. But imagine a school with hundreds of students, dozens of teachers, multiple courses, classes, subjects, and other types of information.
Managing all of this information through individual variables can quickly become difficult.
OOP provides a structured way to organize related information and behavior into reusable structures.
Instead of creating separate structures manually for every student, we can define a common structure for students and then create multiple student objects from it.
This common structure is called a class, while the individual entities created from that class are called objects. Classes and objects will be explored in detail later in this lesson.
A programming paradigm is a general approach or style used to design and write programs.
Different paradigms provide different ways of thinking about and solving programming problems.
Some commonly discussed programming paradigms include:
Python is flexible because it supports multiple programming paradigms rather than forcing programmers to use only one approach.
In Procedural Programming, programs are generally organized around functions and a sequence of operations.
For example, suppose we want to calculate the total price of products:
def calculate_total(price, quantity):
return price * quantity
total = calculate_total(500, 3)
print(total)
Output:
1500
Here, the primary focus is on the operation being performed. We provide a price and quantity to a function, and the function calculates the total.
Procedural programming can be very effective for small and straightforward programs where the logic can be clearly divided into functions.
In Object-Oriented Programming, we think about the program in terms of objects and the relationships between them.
For example, a product could conceptually contain:
Product
Data:
name
price
quantity
Behavior:
calculate_total()
update_stock()
Here, the product’s information and the operations related to that information are conceptually connected.
This becomes particularly useful when an application contains many different entities and each entity has its own data and behavior.
| Feature | Procedural Programming | Object-Oriented Programming |
|---|---|---|
| Main Focus | Functions and procedures | Objects and classes |
| Organization | Program logic is organized around operations | Program logic is organized around objects and their behavior |
| Data and Behavior | Often handled separately | Related data and behavior can be organized together |
| Reusability | Functions can be reused | Classes and objects can provide reusable structures |
| Large Applications | Can become harder to organize as complexity increases | Provides structures that can help organize complex applications |
No. Object-Oriented Programming is not automatically better than procedural programming.
The appropriate programming approach depends on the problem being solved.
For example, if you need to write a small Python script that performs a few calculations, creating multiple classes may introduce unnecessary complexity.
On the other hand, if you are developing a larger application containing many related entities, OOP can provide a useful structure for organizing and maintaining the code.
The goal is therefore not to use OOP everywhere. The goal is to understand when an object-oriented approach is useful.
One of the easiest ways to understand OOP is to think about real-world entities.
Examples include:
These entities have both information and behavior.
For example, a Bank Account may contain information such as:
BankAccount
Data:
account_number
account_holder
balance
It may also have behaviors such as:
deposit()
withdraw()
check_balance()
Similarly, a Student can be represented conceptually as:
Student
Data:
student_id
name
course
marks
Behavior:
calculate_grade()
display_details()
The important idea is that OOP allows us to think about a program in terms of entities, their data, and the operations associated with them.
A simple mental model is:
Real-World Entity
↓
Object
↓
Data + Behavior
↓
Organized Program Structure
This introductory understanding is important because the remaining OOP concepts build on it.
Once you understand the idea of representing entities as objects, you can begin to understand how Python provides classes as blueprints, how objects are created from those classes, how attributes store information, and how methods define behavior.
These concepts will form the foundation for the practical Python OOP examples that follow.
In the previous part, you learned that Object-Oriented Programming organizes programs around objects and that a class can be used as a blueprint for creating those objects. Now we will look more closely at how classes and objects work in Python.
Classes and objects are the foundation of Python OOP. Once you understand their relationship, concepts such as attributes, methods, constructors, inheritance, and polymorphism become much easier to understand.
A class is created using the class keyword.
class Student:
pass
Here, Student is the name of the class.
At this point, the class does not contain any attributes or methods. The pass statement simply tells Python that the class is intentionally empty for now.
We can later add data and behavior to the class.
An object is created by calling the class.
class Student:
pass
student1 = Student()
Here:
Student is the class.student1 is an object.Student() creates an instance of the class.We can create multiple objects from the same class:
student1 = Student()
student2 = Student()
student3 = Student()
All three objects are instances of the Student class.
Objects can contain data in the form of attributes.
class Student:
pass
student1 = Student()
student1.name = "Rahul"
student1.age = 21
student1.course = "Data Analytics"
Now the object contains three pieces of information:
name → Rahul
age → 21
course → Data Analytics
We can access these attributes using the dot operator:
print(student1.name)
print(student1.age)
print(student1.course)
Output:
Rahul
21
Data Analytics
Objects created from the same class can contain different values.
class Student:
pass
student1 = Student()
student1.name = "Rahul"
student1.course = "Python"
student2 = Student()
student2.name = "Priya"
student2.course = "Data Analytics"
Now:
print(student1.name)
print(student1.course)
print(student2.name)
print(student2.course)
Output:
Rahul
Python
Priya
Data Analytics
The class provides the common structure, while each object can contain its own data.
A class can also contain methods that define behavior.
class Student:
def study(self):
print("Student is studying")
Create an object and call the method:
student1 = Student()
student1.study()
Output:
Student is studying
Methods allow objects to perform actions rather than simply store data.
Methods can access the attributes of the current object using self.
class Student:
def display(self):
print("Name:", self.name)
print("Course:", self.course)
Now:
student1 = Student()
student1.name = "Rahul"
student1.course = "Python"
student1.display()
Output:
Name: Rahul
Course: Python
Here, self.name refers to the name attribute belonging to the current object, and self.course refers to its course.
The importance of self becomes clearer when multiple objects use the same method.
class Student:
def display(self):
print("Name:", self.name)
print("Course:", self.course)
student1 = Student()
student1.name = "Rahul"
student1.course = "Python"
student2 = Student()
student2.name = "Priya"
student2.course = "Data Analytics"
student1.display()
student2.display()
Output:
Name: Rahul
Course: Python
Name: Priya
Course: Data Analytics
When student1.display() is called, self refers to student1.
When student2.display() is called, self refers to student2.
This allows the same method to work with different objects and their individual data.
We can combine attributes and multiple methods to create a more meaningful class.
class Student:
def display(self):
print("Name:", self.name)
print("Course:", self.course)
print("Marks:", self.marks)
def is_pass(self):
if self.marks >= 40:
return True
return False
Now create an object:
student1 = Student()
student1.name = "Rahul"
student1.course = "Data Analytics"
student1.marks = 78
student1.display()
print("Passed:", student1.is_pass())
Output:
Name: Rahul
Course: Data Analytics
Marks: 78
Passed: True
This example demonstrates an important OOP idea: the object contains information and the class provides behavior that works with that information.
| Class | Object |
|---|---|
| Blueprint or template | Instance created from the class |
| Defines possible structure and behavior | Contains actual values and uses the behavior |
Example: Student |
Example: student1 |
| Can create many objects | Represents one particular instance |
A class is not the same thing as an object.
Think of it this way:
Class
↓
Student
Objects
↓
student1
student2
student3
The class defines what a student object can contain and do, while each object represents a particular student with its own data.
The same concept can be applied to products.
class Product:
def display(self):
print("Product:", self.name)
print("Price:", self.price)
product1 = Product()
product1.name = "Laptop"
product1.price = 55000
product1.display()
Output:
Product: Laptop
Price: 55000
We could create another product object without creating another class:
product2 = Product()
product2.name = "Smartphone"
product2.price = 25000
product2.display()
This demonstrates one of the central benefits of classes: a single class can be used to create multiple related objects.