What is a Class in Python?
• • A class is a blueprint or template for creating
objects.
• • It allows us to model real-world entities in
code.
• • Think of it like a recipe – you define it once
and use it to make many dishes (objects).
• • Helps make code reusable, organized, and
clean.
Why Use Classes?
• • Avoids writing repetitive code.
• • Keeps related data and functions together.
• • Makes big projects manageable.
• • Easy to update or modify later.
• • Example: Instead of writing separate code
for each player in a game, create one Player
class.
Real-Life Example: Student Class
• class Student:
• def __init__(self, name, roll):
• self.name = name
• self.roll = roll
• def introduce(self):
• print(f"My name is {self.name} and my
roll number is {self.roll}")
Creating Objects
• student1 = Student("Ali", 101)
• student2 = Student("Sara", 102)
• student1.introduce()
• student2.introduce()
• • Each object has its own data but shares the
same structure (class).
What is a Method?
• • A method is a function defined inside a class.
• • It can work with the object’s data using the
keyword self.
• • Example: introduce() is a method that uses
self.name and self.roll.
Benefits of Using Classes &
Methods
• • Keeps code organized and logical.
• • Makes it easier to add features later.
• • Reusable across multiple parts of your
project.
• • Great for building games, websites, apps,
and more.