Learning outcomes
- Understand classes and objects
Core concepts
Lessons
Module goal
Learner understands what a class is, how objects are created from it, and can build a simple class that models something real — closing out Level 2 and setting up the Beginner Capstone.
Lesson 1 of 6 · lecture
Why object-oriented programming exists
Step 1 of 2
Up to this point, data and the functions that act on that data have lived separately. A dictionary held a contact's details; a separate function processed it. This works, but it gets messy as programs grow — nothing ties the data and its related behaviour together.
Object-Oriented Programming (OOP) solves this by letting you bundle data (attributes) and the behaviour that acts on that data (methods) into a single unit, called a class. A class is a blueprint for creating as many individual instances of that thing as needed — each one called an object.
Teaching point
A class called Device is like an architect's blueprint for “a repaired device” — it defines what every device has (brand, model, repair status) and what every device can do (get marked repaired, display details). The blueprint isn't a device; every actual phone through the shop is a separate object built from that plan.
Guided labs
Lab — build a Device class
- 1.Create oop_basics.py
- 2.Define a Device class with __init__ storing brand, model and issue, plus a repaired attribute starting as False
- 3.Add a method mark_as_repaired() that sets repaired to True
- 4.Add a method display_info() returning a formatted string with all details, including repair status
- 5.Create two Device objects, print their info, mark one as repaired, and print both again to confirm only that one changed
class Device:
def __init__(self, brand, model, issue):
self.brand = brand
self.model = model
self.issue = issue
self.repaired = False
def mark_as_repaired(self):
self.repaired = True
def display_info(self):
status = "Repaired" if self.repaired else "Pending"
return f"{self.brand} {self.model} — Issue: {self.issue} — Status: {status}"
phone1 = Device("Apple", "iPhone 12", "Cracked screen")
phone2 = Device("Google", "Pixel 6", "Battery drains fast")
print(phone1.display_info())
print(phone2.display_info())
phone1.mark_as_repaired()
print("\n--- After repair ---")
print(phone1.display_info())
print(phone2.display_info())Beginner Capstone — Contact Manager app
- 1.Use a Contact class to represent each contact (name, phone, email)
- 2.Store all contacts in a list
- 3.Save contacts to a file so they persist between runs, and load them back when the program starts
- 4.Present a menu (using a while loop from Module 3) to add, view or delete a contact, until the user exits
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
def to_line(self):
return f"{self.name},{self.phone},{self.email}\n"
def display(self):
return f"{self.name} | {self.phone} | {self.email}"
def load_contacts():
contacts = []
try:
with open("contacts.txt", "r") as file:
for line in file:
name, phone, email = line.strip().split(",")
contacts.append(Contact(name, phone, email))
except FileNotFoundError:
pass
return contacts
def save_contacts(contacts):
with open("contacts.txt", "w") as file:
for contact in contacts:
file.write(contact.to_line())
contacts = load_contacts()
while True:
print("\n1. Add contact 2. View contacts 3. Delete contact 4. Exit")
choice = input("Choose an option: ")
if choice == "1":
name = input("Name: ")
phone = input("Phone: ")
email = input("Email: ")
contacts.append(Contact(name, phone, email))
save_contacts(contacts)
elif choice == "2":
for contact in contacts:
print(contact.display())
elif choice == "3":
name_to_delete = input("Enter name to delete: ")
contacts = [c for c in contacts if c.name != name_to_delete]
save_contacts(contacts)
elif choice == "4":
break
else:
print("Invalid option, try again.")Teaching point
This is the first project tying together every Level 1 and Level 2 concept at once — variables, control flow, loops, functions, data structures, file handling and OOP. The try/except in load_contacts() previews Module 8 (Error Handling); it's introduced here because it's needed to handle a missing file gracefully on first run.
Success criteria
After calling mark_as_repaired() on phone1 only, its status prints as “Repaired” while phone2 still shows “Pending”. For the capstone: the learner can add a contact, exit, reopen the program, and still see their contacts — proving persistence works alongside the menu-driven flow.
Knowledge Check
Question 1 of 6
What is the difference between a class and an object?
What you read
- What a class represents (a blueprint)
- How objects are instances of a class
- How methods define behavior
What you understand
Key takeaways
- — OOP models real-world entities in code
- — It organizes related data and behavior together
Hands-on lab
Lab tasks
- 1.Build a simple Device class (representing a phone/laptop) with attributes and a method
Knowledge check
- Identify class vs. object vs. attribute vs. method
Write your answers in your own notes before moving on — explaining a concept in your own words is the fastest way to find the gaps.
Beginner capstone
Contact Manager App
Build a Contact Manager App: stores contacts using a class, saves/loads them from a file, and lets the user add, view and delete contacts via a menu-driven loop.