🐍 Python Lesson: Understanding Classes and Objects with Real Examples Part - 1 :

🐍 Python Lesson: Understanding Classes and Objects with Real Examples
If you're new to Python, you might have seen code that does… well, nothing at first. You copy-paste the code, run it, and the terminal shows… blank.
Let me show you why that happens — and how to turn that code into something powerful.
🔹 The Mysterious Code That Shows Nothing
First, let’s look at this code:
class Dog:
def init(self, name, age):
self.name = name
self.age = age
Now run this code in VS Code or any Python IDE.
❓ What Do You See in the Terminal?
Nothing. That’s right.
This is because the code only defines a class, a kind of blueprint. It doesn’t actually do anything unless you create an object and use its data or functions.
Let’s fix that.
🔧 Add These Lines:
my_dog = Dog("Willi", 6)
print(f"My dog's name is {my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
✅ Terminal Output:
My dog's name is Willi.
My dog is 6 years old.
Nice! Now the program actually shows something.
🚀 Code Examples to Understand Classes
Let’s go deeper by comparing three examples.
📦 Code 1: Dog
class Dog:
def init(self, name, age):
self.name = name
self.age = age
def sit(self):
print(f"{self.name} is now sitting.")
def roll_over(self):
print(f"{self.name} rolled over!")
🤖 Code 2: Robot
class Robot:
def init(self, model):
self.model = model
def move_arm(self):
print(f"{self.model} is moving its arm.")
def speak(self):
print(f"{self.model} says: Hello, human.")
🚗 Code 3: Car
class Car:
def init(self, brand):
self.brand = brand
def drive(self):
print(f"{self.brand} car is now driving.")
def brake(self):
print(f"{self.brand} car is braking.")
💡 Observations:
Code | About | Functions |
Code 1 | Dog | sit(), roll_over() |
Code 2 | Robot | move_arm(), speak() |
Code 3 | Car | drive(), brake() |
🛠 Modifying Code 3 (Car → Truck)
Let’s change Car to Truck, and update its features.
🚚 Truck Version:
class Truck:
def init(self, model):
self.model = model
def move_forward(self, speed):
print(f"{self.model} truck is moving forward at {speed} km/h.")
def move_backward(self, speed):
print(f"{self.model} truck is reversing at {speed} km/h.")
def turn_left(self, angle):
print(f"{self.model} truck is turning left by {angle} degrees.")
✅ Try This:
my_truck = Truck("Ford")
my_truck.move_forward(40)
my_truck.move_backward(20)
my_truck.turn_left(45)
Terminal Output:
Ford truck is moving forward at 40 km/h.
Ford truck is reversing at 20 km/h.
Ford truck is turning left by 45 degrees.
🧠 What’s Happening Here?
You’ve now learned:
- How to define a class (blueprint).
- How to create an object from it.
- How to use methods (functions inside the class).
- How to customize data using the init method.
🔄 Using Multiple Objects from One Class
You can reuse the same class to make multiple objects:
my_car = Car("Toyota")
your_car = Car("Honda")
my_car.drive()
your_car.brake()
Each object keeps its own data but shares the structure.
🆕 Let’s Build a New Program
Idea: Track the entry time of students coming to class.
We want to build a structure that stores each student’s name and their entry time.
👨🏫 Student Entry Tracker:
class StudentEntry:
def init(self, name, time):
self.name = name
self.time = time
def show_entry(self):
print(f"{self.name} entered the class at {self.time}.")
✅ Try This:
student1 = StudentEntry("Ravi", "9:00 AM")
student2 = StudentEntry("Sita", "9:05 AM")
student1.show_entry()
student2.show_entry()
Terminal Output:
Ravi entered the class at 9:00 AM.
Sita entered the class at 9:05 AM.
🎯 Final Thoughts
- Classes help you organize data and behavior.
- Objects created from classes are like custom tools you build yourself.
- You can start with one example (dog, robot, car), then build your own programs using the same concept.
💬 What to Try Next?
- Add more features to your truck or car (like honk(), load_cargo()).
- Build a class for LibraryBook with methods like borrow() and return_book().
- Make a class for BankAccount to practice deposits and withdrawals.
If you enjoyed this lesson, follow my blog for more beginner-friendly Python tutorials!
Subscribe to my newsletter
Read articles from Vikash S Naruka directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
