In Python, everything is built around objects. Classes and objects let you combine data and behavior in one structure, making your programs easier to organize and reuse.
📝 If You Want to Practice the Code
If you plan to practice the Python code mentioned within this blog post, you’ll need a working Python environment. You can execute the commands in one of the following ways (choose only one option):
- From a Python prompt in your terminal or command line – see Getting Started with Python and Virtual Environment in Python and How to Set One Up
- Using Miniconda and Jupyter Notebook – see How to Install and Set Up Miniconda and Install Jupytor Notebook Windows, How to Install and Set Up Miniconda on MacOS, How to Install and Set Up Miniconda on Linux (Ubuntu), and Getting Started with Jupyter Notebooks.
- On Google Colab (browser-based, no installation required) – see Getting Started with Google Colab.
- Through VS Code – see Setting Up VS Code for Python Development.
👉 Choose whichever environment you’re most comfortable with. If you haven’t set up Python yet, start with one of the installation posts before trying the examples here.
Understanding Classes and Objects
- A class is like a blueprint.
- An object is a specific item created from that blueprint.
🔹 Creating a Class
A class is defined using the class keyword.
class Student:
pass
Here, Student is an empty class.
🔹 Making an Object
Objects are created by calling the class:
s1 = Student()
print(type(s1))
👉 Output: <class '__main__.Student'>
🔹 Adding Attributes
Classes can store data using attributes. Attributes are usually set in a special method called __init__.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
s1 = Student("Alice", 20)
s2 = Student("Bob", 22)
print(s1.name, s1.age) # Alice 20
print(s2.name, s2.age) # Bob 22
__init__is the constructor: it runs when the object is created.selfrefers to the current object.
🔹 Adding Methods
A class can also contain methods — functions that belong to the class.
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def greet(self):
print("Hello, my name is", self.name)
def show_grade(self):
print(self.name, "scored grade", self.grade)
s1 = Student("Alice", "A")
s1.greet() # Hello, my name is Alice
s1.show_grade() # Alice scored grade A
🔹 Multiple Objects
Each object keeps its own copy of attributes:
s2 = Student("Bob", "B")
s2.greet() # Hello, my name is Bob
s2.show_grade() # Bob scored grade B
📝 Mini Task
- Create a class
Carwith attributesbrandandyear. - Add a method
drive()that prints:"Brand is driving". - Create two car objects and call the method.
class Car:
def __init__(self, brand, year):
self.brand = brand
self.year = year
def drive(self):
print(self.brand, "is driving")
c1 = Car("Toyota", 2020)
c2 = Car("Tesla", 2022)
c1.drive() # Toyota is driving
c2.drive() # Tesla is driving
✅ Summary
- Class → A blueprint for creating objects.
- Object → An instance of a class.
__init__→ Constructor that sets up attributes.- self → Refers to the object itself.
- Methods → Functions inside a class that use or modify its data.
With classes and objects, you can build more structured and reusable programs.