Up to this point, we’ve been working with data; storing it, organizing it, and interacting with it. But as programs grow, handling everything separately can start to feel scattered.
What if we could group related data together in a more natural, real-life way?
That’s where classes and objects come in.
A class is simply a blueprint. It describes what something should look like and what information it should hold.
An object is what you create from that blueprint.
Think about a student.
A student has a name, an age, maybe a course. Instead of storing these as separate variables, we can group them into one structure.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student("Mary", 20)
Here, Student is the class, and student1 is the object.
Instead of dealing with scattered pieces of data, everything about the student is now in one place.
You can access the information like this:
print(student1.name)
print(student1.age)
It feels cleaner. More organized. More real.
Classes help us model real-world things inside our code. Whether it’s a person, a product, or even a system, we can represent it in a structured way.
And the best part?
You can create as many objects as you want from one class each with its own unique data.
In simple terms, classes and objects help your code move from just storing data… to actually representing the world around you.
Challenge 🌱
Create your own class called Car.
- It should have at least two attributes (for example: brand and year)
- Create one object from it
- Print out the details of that car
If you want to push yourself a little more, try creating two different cars from the same class.
Take your time with it the goal is not perfection, but understanding.
In the next article, we’ll look at how to give these objects behavior so they don’t just hold data, but can also do things.
Top comments (0)