Python Classes and Objects Tutorial | Create Classes, Objects & Methods
Classes and objects are the foundation of Object-Oriented Programming (OOP). While the previous lesson introduced the concept of OOP, this lesson focuses on creating and working with classes and objects in detail. Understanding how to design classes and instantiate objects is essential for building scalable, maintainable, and reusable applications.
From student management systems and banking software to AI-powered applications and web development frameworks like Django, classes and objects are used extensively to model real-world entities and organize code efficiently.
In this lesson, you’ll learn how to create classes, instantiate objects, define instance and class variables, work with methods, constructors, and understand the lifecycle of objects. Through practical examples, you’ll gain hands-on experience with one of the most important concepts in Python programming.
By the end of this lesson, you’ll be able to design your own classes and use objects effectively in real-world software development.
Learning Objectives
After completing this lesson, you will be able to:
- Understand classes and objects in depth.
- Create multiple objects from a class.
- Use instance variables and class variables.
- Work with constructors.
- Define instance methods, class methods, and static methods.
- Access and modify object data.
- Understand object lifecycle.
- Build real-world applications using classes and objects.
Topics Covered
- Classes and Objects
- Instance Variables
- Class Variables
- Constructors
- Instance Methods
- Class Methods
- Static Methods
- Object Lifecycle
- Real-World Applications
- Best Practices
Detailed Lesson Content
Introduction to Classes and Objects
Imagine you’re building an Online Learning Platform like Mango Engineers.
Every student has:
- Name
- Mobile Number
- Course
- Enrollment Date
Instead of creating separate variables for every student, we create a Student Class.
Every new student becomes an Object of that class.
This makes the application organized and scalable.
Revisiting Classes
A class is a blueprint that defines the properties and behaviors of an object.
Example:
class Student:
pass
This class doesn’t perform any operation yet, but it serves as the template for creating student objects.
Creating Objects
Objects are created using the class name.
class Student:
pass
student1 = Student()
student2 = Student()
student3 = Student()
Each object has its own identity and memory location.
Instance Variables
Instance variables store data that belongs to a specific object.
Example:
class Student:
def __init__(self,name,course):
self.name = name
self.course = course
student1 = Student("Rahul","Python")
student2 = Student("Priya","AI")
print(student1.name)
print(student2.course)
Output
Rahul
AI
Each object stores different values.
Class Variables
Class variables are shared among all objects.
Example:
class Student:
institute = "Mango Engineers"
def __init__(self,name):
self.name = name
student1 = Student("Rahul")
student2 = Student("Priya")
print(student1.institute)
print(student2.institute)
Output
Mango Engineers
Mango Engineers
If the class variable changes, it affects all objects.
Constructors
The constructor initializes object data automatically.
class Employee:
def __init__(self,name,salary):
self.name = name
self.salary = salary
employee = Employee("Amit",50000)
The constructor ensures every object starts with the required data.
Instance Methods
Instance methods work with object-specific data.
Example:
class Student:
def __init__(self,name):
self.name = name
def display(self):
print("Student:",self.name)
student = Student("Rahul")
student.display()
Output
Student: Rahul
Class Methods
Class methods operate on class-level data.
Use the @classmethod decorator.
class Student:
institute = "Mango Engineers"
@classmethod
def display_institute(cls):
print(cls.institute)
Student.display_institute()
Output
Mango Engineers
Class methods are useful for working with class variables.
Static Methods
Static methods do not depend on object or class data.
Use the @staticmethod decorator.
class Calculator:
@staticmethod
def add(a,b):
return a+b
print(Calculator.add(10,20))
Output
30
Static methods are ideal for utility functions.
Object Lifecycle
An object’s lifecycle consists of:
- Creation
- Initialization
- Usage
- Destruction
Example:
class Demo:
def __init__(self):
print("Object Created")
demo = Demo()
When an object is no longer referenced, Python’s Garbage Collector automatically frees its memory.
Real-World Example 1: Employee Management System
class Employee:
company = "Mango Engineers"
def __init__(self,name,department,salary):
self.name = name
self.department = department
self.salary = salary
def display(self):
print(self.name)
print(self.department)
print(self.salary)
employee1 = Employee("Rahul","Development",50000)
employee1.display()
Real-World Example 2: E-Commerce Product
class Product:
def __init__(self,name,price):
self.name = name
self.price = price
def discount(self):
self.price = self.price * 0.90
product = Product("Laptop",70000)
product.discount()
print(product.price)
Output
63000
Real-World Example 3: Bank Account
class BankAccount:
def __init__(self,name,balance):
self.name = name
self.balance = balance
def deposit(self,amount):
self.balance += amount
def withdraw(self,amount):
if amount <= self.balance:
self.balance -= amount
def display(self):
print(self.balance)
account = BankAccount("Rahul",20000)
account.deposit(5000)
account.withdraw(3000)
account.display()
Output
22000
Real-World Example 4: Student Attendance
class Student:
def __init__(self,name):
self.name = name
self.attendance = 0
def mark_attendance(self):
self.attendance += 1
student = Student("Rahul")
student.mark_attendance()
print(student.attendance)
Output
1
Common Mistakes
Forgetting self
Incorrect
def display():
Correct
def display(self):
Accessing Instance Variables Incorrectly
Incorrect
print(name)
Correct
print(self.name)
Confusing Class Variables with Instance Variables
Remember:
- Instance Variable → Different for every object.
- Class Variable → Shared among all objects.
Best Practices
- Use PascalCase for class names.
- Keep one responsibility per class.
- Initialize object data in constructors.
- Use meaningful attribute names.
- Prefer instance variables for object-specific data.
- Use class variables only for shared information.
- Keep methods short and focused.
Key Takeaways
- A class is a blueprint.
- Objects are instances of a class.
- Instance variables belong to individual objects.
- Class variables are shared across all objects.
- Constructors initialize object data.
- Instance methods work with object data.
- Class methods work with class data.
- Static methods provide utility functions.
- OOP improves scalability and code organization.



