Python is an object-oriented programming (OOP) language, which means it allows you to organize code using classes and objects. These concepts help you build programs that are more structured, reusable, and easier to manage.
In this tutorial, you will learn what classes and objects are, how to create them, and how to use them in real-world examples.
What is a Class?
A class is a blueprint or template for creating objects.
It defines:
- Attributes (variables)
- Methods (functions)
Example:
class Car:
passHere, Car is a class, but it does nothing yet.
What is an Object?
An object is an instance of a class. It represents a real-world entity.
Example:
class Car:
pass
car1 = Car()
print(car1)Class vs Object
| Class | Object |
|---|---|
| Blueprint | Real instance |
| Defines structure | Uses structure |
| Example: Car design | Example: Toyota Car |
Creating a Class with Attributes
Attributes are variables inside a class.
Example:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Toyota", "Corolla")
print(car1.brand)
print(car1.model)The init Method (Constructor)
The __init__ method is automatically called when an object is created.
Example:
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
s1 = Student("John", 20)
print(s1.name)
print(s1.age)Adding Methods in a Class
Methods are functions inside a class.
Example:
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello", self.name)
s1 = Student("Alice")
s1.greet()Creating Multiple Objects
Each object has its own data.
Example:
class Student:
def __init__(self, name):
self.name = name
s1 = Student("John")
s2 = Student("Sara")
print(s1.name)
print(s2.name)Modifying Object Values
You can change values after creating objects.
Example:
class Car:
def __init__(self, brand):
self.brand = brand
car1 = Car("Toyota")
car1.brand = "Honda"
print(car1.brand)Deleting Object Properties
You can delete attributes using del.
Example:
class Student:
def __init__(self, name):
self.name = name
s1 = Student("John")
del s1.name
# print(s1.name) # This will cause errorDeleting Objects
You can delete objects completely.
Example:
class Car:
pass
car1 = Car()
del car1The self Keyword
self refers to the current object.
Example:
class Student:
def __init__(self, name):
self.name = name
def show(self):
print(self.name)
s1 = Student("John")
s1.show()Real-World Example: Student System
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def display(self):
print("Name:", self.name)
print("Marks:", self.marks)
s1 = Student("John", 85)
s2 = Student("Alice", 90)
s1.display()
s2.display()Real-World Example: Car System
class Car:
def __init__(self, brand, speed):
self.brand = brand
self.speed = speed
def show(self):
print(self.brand, "runs at", self.speed, "km/h")
car1 = Car("Toyota", 120)
car2 = Car("BMW", 200)
car1.show()
car2.show()Key Features of Classes and Objects
- Code reusability
- Easy maintenance
- Real-world modeling
- Data encapsulation
- Scalability
Common Mistakes
Mistake 1: Forgetting self
❌ Wrong:
def show(name):
print(name)✔ Must use self
Mistake 2: Not creating object
Car.show()✔ Must create object first
Mistake 3: Confusing class and object
✔ Class = blueprint, Object = real instance
Safe Example
class Test:
def show(self):
print("Hello OOP")
t = Test()
t.show()Conclusion
Classes and objects are the foundation of Python OOP programming.
You learned:
- What classes and objects are
- How to create attributes and methods
- How to use constructors
- Real-world examples
- Key OOP concepts
Mastering classes and objects is the first step to becoming an expert in Python object-oriented programming.


0 Comments