-
Hajipur, Bihar, 844101
Classes and objects are the foundation of Object Oriented Programming in Python. If you want to build programs that are organised, reusable, and easy to scale, you need to understand how classes and objects work. This chapter explains both concepts in a clear and practical way, with examples that help you learn how to use them in real Python projects.
A class is a blueprint, and an object is the actual item created from that blueprint. The class defines the structure and behaviour, and the object stores real data and performs actions.
For example:
A class Car defines brand, model, and methods.
Objects like c1 or c2 represent actual cars with their own data.
Classes allow you to create unlimited objects with the same structure but different values.
A class is defined using the class keyword. Inside it, you can place attributes (data) and methods (functions).
Basic structure:
class MyClass:
pass
This class does nothing yet, but it acts as a placeholder. You can now start adding attributes and methods to give it functionality.
Once a class is defined, you can create objects using the class name followed by brackets.
Example:
class Student:
pass
s1 = Student()
s2 = Student()
Here, s1 and s2 are two separate objects. Each can store its own data later.
Objects are also known as instances of the class.
__init__() MethodThe __init__() method runs automatically whenever a new object is created. It helps you set up default values for attributes.
Example:
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
Now you can create objects with actual data:
s1 = Student("Riya", 89)
s2 = Student("Neha", 92)
Each object has its own version of name and marks.
Attributes are variables that store information inside an object. There are two main types:
These belong to each object. Changing one object’s attribute does not affect another.
Example:
s1.name = "Riya"
s2.name = "Neha"
These are shared by all objects of the class.
Example:
class Student:
school = "Green Valley School"
All Student objects can access the same school value.
Methods are functions written inside a class. They perform actions linked to the object.
Example:
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def info(self):
print("Name:", self.name)
print("Marks:", self.marks)
Using the method:
s1 = Student("Riya", 89)
s1.info()
The info() method prints data stored inside the object.
Let’s create a class for an online store product.
class Product:
def __init__(self, name, price, stock):
self.name = name
self.price = price
self.stock = stock
def details(self):
print("Product:", self.name)
print("Price:", self.price)
print("Stock:", self.stock)
Creating objects:
p1 = Product("Notebook", 45, 120)
p2 = Product("Pen Pack", 80, 50)
Now you can use the method:
p1.details()
p2.details()
Each product holds its own data.
It’s important to know the difference.
class Employee:
company = "TechZone" # Class attribute
def __init__(self, name, salary):
self.name = name # Instance attribute
self.salary = salary
company is the same for all employees
name and salary are different for each employee
Objects:
e1 = Employee("Neha", 30000)
e2 = Employee("Anita", 28000)
You can change instance values without affecting the other:
e1.salary = 35000
You can access attributes using dot notation.
Example:
print(e1.name)
print(e1.salary)
You can call methods the same way:
e1.info()
The dot notation helps you use everything stored inside an object.
You can change object attributes anytime.
Example:
p1.price = 50
p1.stock = 100
Objects are flexible, so you can adjust their data based on your program’s needs.
Classes let you create as many objects as you want.
Example:
students = [
Student("Riya", 89),
Student("Neha", 92),
Student("Anita", 85)
]
for s in students:
s.info()
Looping through objects lets you build real-world systems like inventory apps, school management tools, and automated reports.
Creating a Simple Class
class User:
pass
Adding Attributes with __init__()
class User:
def __init__(self, username):
self.username = username
Creating Objects
u1 = User("Anita")
u2 = User("Meera")
Adding Methods
def greet(self):
print("Hello", self.username)
Calling Methods
u1.greet()
Class with Multiple Attributes
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
Method to Display Information
def show(self):
print(self.title, "-", self.author)
Creating Many Objects
books = [Book("Python Basics", "Reema"), Book("Web Guide", "Tina")]
Looping Through Objects
for b in books:
b.show()
Updating Attributes
books[0].author = "R. Meera"
Classes and objects are at the heart of Python’s OOP approach. A class defines the structure, and objects store real data based on that structure. Attributes hold values, and methods perform actions. Using classes makes your code more organised, reusable, and easier to maintain. Once you understand how to create and use them, you can build stronger and more flexible Python applications.
Q1. Write a Python program to create a class named Laptop with properties brand and price.
Q2. Write a Python program to use the __init__() method to initialize the properties of the Laptop class.
Q3. Write a Python program to create two objects of the Laptop class and print their details.
Q4. Write a Python program to add a method discount() that reduces the price by a given amount or percentage.
Q5. Write a Python program to modify the value of the price property after the object is created.
Q6. Write a Python program to add a method that returns a full description of the laptop (e.g., brand and price).
Q7. Write a Python program to create a class Teacher with attributes name and subject.
Q8. Write a Python program to use self to access and print the teacher’s information inside a method.
Q9. Write a Python program to delete the subject property of a Teacher object using the del keyword.
Q10. Write a Python program to delete the entire object of the Teacher class using the del keyword.