πŸ§‘β€πŸ« What is Class and Object in Python? | Simple Explanation for Beginners



πŸ”Ή What is a Class?

  • A class is a template or design to make things (objects).
  • It tells Python what the object has (variables) and what it can do (functions).

βœ… Example:

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

This class is like a student form. It has:

  • name
  • age

πŸ”Ή What is an Object?

  • An object is something real made from the class.
  • We can make many objects from one class.

βœ… Example:

s1 = Student("Arun", 18)
print(s1.name)  # Output: Arun

➑️ s1 is an object of the class Student.


πŸ”Ή __init__() Method

  • This is a special function in the class.
  • It runs automatically when you make an object.
  • It gives values to the object.

βœ… Example:

class Car:
    def __init__(self, brand):
        self.brand = brand

c1 = Car("Toyota")
print(c1.brand)  # Output: Toyota


πŸ”Ή What is self?

  • self means this object.
  • It is used to store data inside the object.

βœ… Example:

class Example:
    def show(self):
        print("Hello from", self)

obj = Example()
obj.show()


🧠 Real-Life Example

Let’s say we want to make details of books.

βœ… Example:

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

b1 = Book("Python Basics", "Ravi")
print(b1.title)   # Python Basics
print(b1.author)  # Ravi


βœ… Benefits of Using Class and Object

Why we use OOP?Simple Explanation
Reuse CodeUse the same class to make many objects
Clean CodeEasy to read and update
Better StructureKeep data and actions together

πŸ“ Practice Questions

Q1. Create a class Employee with name and salary. Show details.

βœ… Answer:

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def show(self):
        print("Name:", self.name)
        print("Salary:", self.salary)

e1 = Employee("Meena", 25000)
e1.show()


🧾 Summary Table

WordMeaning
ClassBlueprint for making objects
ObjectReal item created from class
__init__()Gives values when object is created
selfRefers to the current object

πŸ”— Next Topic:

➑️ __init__() in Python – Constructor Made Simple (Coming up next)



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *