In programming, we often need to manage lots of information about different things. For example, in a video game, you might have many characters—each with a name, health, and level. If we try to keep track of each character using separate variables, the code quickly becomes messy and hard to manage.
With OOP, we use classes as blueprints. A class describes what kind of data and actions an object will have. When we create a new object from a class, it’s called an instance. Each object can have its own values and can do its own actions.
You’ll learn how to:
Create a class with attributes and methods.
Make objects using the class.
Store objects in a list.
Access and update values stored inside those objects.
We will practise each of these steps with simple examples, then build up to mini-projects that show how everything fits together. The goal of this lesson is not to make perfect programs, but to show that you understand how OOP works and how to write the syntax correctly.
Class A blueprint or plan for an object. It describes what data and actions the object will have.
Object A real thing created from a class. It holds actual values.
Attribute A variable that belongs to an object (e.g. self.name). Stores data inside the object.
Method A function inside a class. It performs actions using the object’s data.
self Refers to the current object. It lets us access the object’s own data and methods.
__init__ A special method that runs when you make a new object. It sets up the data (attributes).
Instantiation The process of creating an object from a class. Example: pet1 = Pet("Max", "dog").
List of Objects A group of objects stored in a list. You can use loops to go through them and access their data.
Calling a Method Using a method with an object. Example: player1.level_up() runs the level_up method for that player.
Object-Oriented Programming (OOP) is a way to organise your code more clearly. It helps us group related data (like a player’s name, score, and level) and actions (like level up or take damage) into one object. This makes the code easier to read, reuse, and update.
Context: You want to keep information about each student in one place.
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
student1 = Student("Alice", 14, "A")
print(student1.name) # Alice
🔧 Task 1: Book Class Create a Book class and make a single object.
Step 1: Define a class called Book.
Step 2: In the __init__ method, add self.title and self.author.
Step 3: Create a book object (e.g. Book("1984", "George Orwell")).
Step 4: Print the book’s title using print(book.title).
Context: You want each student to introduce themselves with a message.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hi, I'm {self.name} and I'm {self.age}."
student2 = Student("Ben", 15)
print(student2.greet())
🔧 Task 2: Add a Method Let your object tell you if the student is a teenager.
Step 1: Add a method called is_teen(self) inside the class.
Step 2: Inside the method, return True if age is 13–19 using if/else.
Step 3: Create 2 student objects with different ages.
Step 4: Use print(student.is_teen()) to test both.
Context: You have more than one student and want to keep them together in a list.
students = []
students.append(Student("Camila", 13, "B"))
students.append(Student("Diego", 15, "A"))
for s in students:
print(s.name, s.grade)
🔧 Task 3: List of Cars Keep a list of 3 cars and print each brand.
Step 1: Create a class Car with brand and year.
Step 2: Make 3 car objects using the class.
Step 3: Add each car to a list (e.g., cars.append(car1)).
Step 4: Use a for loop to print the brand of each car.
Context: In your game, you want players to level up and show new stats.
class Player:
def __init__(self, username, level):
self.username = username
self.level = level
def level_up(self):
self.level += 1
players = [
Player("Nova", 5),
Player("Zed", 7)
]
for p in players:
p.level_up()
print(p.username, "is now level", p.level)
🔧 Task 4: Describe Pets Store pets in a list and describe each one.
Step 1: Create a class Pet with name and animal_type.
Step 2: Add a method called describe() that returns a sentence like "This is Bella, a cat."
Step 3: Make 2 pet objects and add them to a list.
Step 4: Use a for loop to call describe() on each pet and print it.
🔧 Challenge 1: Movie Info
Step 1: Make a class Movie with title, director, and year.
Step 2: Add a method info() that returns a string like "Inception by Nolan (2010)".
Step 3: Make 3 movie objects and store them in a list.
Step 4: Print each movie’s info using a loop.
🔧 Challenge 2: Gamer Score
Step 1: Create a class Gamer with username and score.
Step 2: Add a method add_score(self, points) to increase the score.
Step 3: Make 3 gamer objects and store them in a list.
Step 4: Use the method to increase scores and print each new score.
Write the example from this lesson, ensuring you include:
✅ A class definition using class
✅ A constructor __init__() initializing attributes
✅ Creating an object and accessing its attributes
Read the example code before writing!