Welcome back, future programmer! In Module One, you learned what Object Oriented Programming is. You learned that an object is a thing with data and actions. You learned that a class is a blueprint for creating objects. You also learned how to think in objects.
Now it is time to write real code. In this module, you will learn how to create a class in code. You will learn how to create objects from that class. You will learn how to give objects their own data. You will learn how to make objects do things using methods.
We will use a simple programming language called Python. Python is easy to read and write. It is used by millions of people around the world. Do not worry if you have never written code before. We will go step by step.
By the end of this module, you will have written your own class and created your own objects. You will see your code come to life. Let’s start coding!
After you finish this module, you will be able to:
__init__ method to set up attributes.self keyword.Ada lived in Lagos. Her uncle worked in a robot factory. One day, Ada visited the factory. She saw a big machine that made robots. The machine had a computer. On the computer was a program written in Python.
Ada asked her uncle, “How does the computer know how to make a robot?” Her uncle showed her the code. It looked like this:
class Robot:
def __init__(self, name, colour):
self.name = name
self.colour = colour
def say_hello(self):
print("Hello, my name is " + self.name)
“This is a class,” said her uncle. “It is a blueprint for making robots. The __init__ method sets up the robot’s name and colour. The say_hello method makes the robot speak.”
Ada was curious. “Can you make a robot for me?” she asked. Her uncle typed:
my_robot = Robot("AdaBot", "blue")
my_robot.say_hello()
The computer printed: Hello, my name is AdaBot. Ada was amazed. “So my_robot is an object,” she said. “And it was made from the class Robot.”
“Exactly!” said her uncle. “You can make as many robots as you want. Each one can have a different name and colour.”
Ada decided that day that she would learn to write classes and create objects. And that is exactly what you will do in this module.
Moral of the story: A class is a blueprint. An object is a thing made from the blueprint. You can make many objects from one class.
Definition: Python is a programming language. It is a way to tell a computer what to do.
Why it is important: Python is easy to read and write. It is great for beginners.
Simple explanation: Python looks almost like English. You write instructions, and the computer follows them.
Real-life example: Many apps and websites are built with Python.
School example: Python is used in schools to teach programming.
Home example: You can use Python to make a simple calculator.
Nigerian example: Nigerian tech companies use Python to build software.
Illustration:
+---------------------+
| PYTHON |
| print("Hello!") |
| Easy to read! |
+---------------------+
Mini summary: Python is a programming language. It is easy to read and write. It is great for beginners.
Definition: A class is a blueprint for creating objects. In Python, you write class Name:.
Why it is important: The class defines what data and actions the objects will have.
Simple explanation: You write the word class, then the name of the class, then a colon. Everything inside the class is indented.
Real-life example: A class called Car defines what a car looks like.
School example: A class called Student defines what a student looks like.
Home example: A class called Pet defines what a pet looks like.
Nigerian example: A class called Trader defines what a trader looks like.
Example code:
class Car:
pass
Illustration:
class Car:
pass
|
V
A blueprint for cars
Mini summary: A class is written with the word class. It is a blueprint for objects.
__init__ Method – Setting Up Your ObjectDefinition: The __init__ method is a special method that runs when you create an object. It sets up the object’s data.
Why it is important: Every object needs to start with some data. __init__ gives it that data.
Simple explanation: When you create a car, you want to give it a colour and a speed. __init__ does that.
Real-life example: When a baby is born, it gets a name. __init__ is like giving the object its name.
School example: When a student joins a school, they get a name and a class. __init__ does that.
Home example: When you get a new phone, you set its name and wallpaper. __init__ does that.
Nigerian example: When you open a bank account, you get an account number. __init__ does that.
Example code:
class Car:
def __init__(self, colour, speed):
self.colour = colour
self.speed = speed
Illustration:
__init__ runs
|
V
Object gets data
|
V
colour = "red"
speed = 60
Mini summary: __init__ is a special method that sets up an object’s data when it is created.
self?Definition: self is a word that means “this object”. It is used inside a class to refer to the object being created or used.
Why it is important: Without self, Python does not know which object you are talking about.
Simple explanation: Imagine you have two cars. If you say “colour”, which car do you mean? self tells Python: “this car”.
Real-life example: When you say “my bag”, you mean your own bag. self means “my own”.
School example: When you say “my desk”, you mean your own desk. self means “my own”.
Home example: When you say “my room”, you mean your own room. self means “my own”.
Nigerian example: When you say “my shop”, you mean your own shop. self means “my own”.
Example code:
class Car:
def __init__(self, colour):
self.colour = colour
Illustration:
Object: Car 1
|
V
self.colour = "red"
|
V
This car's colour is red
Mini summary: self means “this object”. It is used inside a class to refer to the object.
Definition: Creating an object means making a specific thing from a class.
Why it is important: Objects are what you use in your program.
Simple explanation: You write the class name, then parentheses with the data. For example: my_car = Car("red", 60).
Real-life example: From the class Car, you create my_car.
School example: From the class Student, you create ada.
Home example: From the class Pet, you create bingo.
Nigerian example: From the class Trader, you create mama_nkechi.
Example code:
my_car = Car("red", 60)
Illustration:
Class: Car
|
V
my_car = Car("red", 60)
|
V
Object created!
Mini summary: Creating an object means making a specific thing from a class. You give it data.
Definition: Each object can have its own data. This is called instance data.
Why it is important: It makes objects unique. Your car can be red. Your friend’s car can be blue.
Simple explanation: When you create an object, you pass in the data. Each object stores its own copy.
Real-life example: Two phones can have different colours.
School example: Two students can have different names.
Home example: Two pets can have different breeds.
Nigerian example: Two traders can sell different goods.
Example code:
car1 = Car("red", 60)
car2 = Car("blue", 80)
print(car1.colour) # red
print(car2.colour) # blue
Illustration:
car1: colour = "red" car2: colour = "blue" Each object has its own data!
Mini summary: Each object can have its own data. This makes objects unique.
Definition: A method is a function inside a class. It defines what an object can do.
Why it is important: Methods let objects do things. They make objects useful.
Simple explanation: You write def method_name(self): inside the class. Then you write what the method does.
Real-life example: A car can drive. drive() is a method.
School example: A student can study. study() is a method.
Home example: A dog can bark. bark() is a method.
Nigerian example: A trader can sell. sell() is a method.
Example code:
class Car:
def __init__(self, colour, speed):
self.colour = colour
self.speed = speed
def drive(self):
print("The car is driving at " + str(self.speed) + " km/h")
Illustration:
class Car:
def drive(self):
print("Driving!")
|
V
Car can drive
Mini summary: A method is a function inside a class. It defines what an object can do.
Definition: Calling a method means telling the object to do the action.
Why it is important: Methods only work when you call them.
Simple explanation: You write the object name, then a dot, then the method name, then parentheses.
Real-life example: You press a button on your phone to make a call. That is calling a method.
School example: You raise your hand to answer a question. That is calling a method.
Home example: You press the remote to change the TV channel. That is calling a method.
Nigerian example: You tell the trader “I want to buy”. That is calling a method.
Example code:
my_car = Car("red", 60)
my_car.drive()
Output:
The car is driving at 60 km/h
Illustration:
my_car.drive()
|
V
Method runs
|
V
"The car is driving at 60 km/h"
Mini summary: Calling a method means telling the object to do the action. Use a dot and parentheses.
Definition: You can create many objects from one class.
Why it is important: This is the power of OOP. One class, many objects.
Simple explanation: You just call the class again with different data.
Real-life example: A factory makes many cars from one design.
School example: A school has many students from one admission form.
Home example: A bakery makes many cakes from one recipe.
Nigerian example: A tailor makes many dresses from one pattern.
Example code:
car1 = Car("red", 60)
car2 = Car("blue", 80)
car3 = Car("green", 100)
car1.drive() # The car is driving at 60 km/h
car2.drive() # The car is driving at 80 km/h
car3.drive() # The car is driving at 100 km/h
Illustration:
Class: Car
|
V
+--------+ +--------+ +--------+
| car1 | | car2 | | car3 |
| red | | blue | | green |
| 60 | | 80 | | 100 |
+--------+ +--------+ +--------+
Mini summary: You can create many objects from one class. Each object can have different data.
Definition: You will now write a complete class with __init__ and methods.
Why it is important: A complete class is the foundation of any OOP program.
Simple explanation: Start with class. Add __init__. Add methods. Create objects. Call methods.
Real-life example: A class called BankAccount with deposit and withdraw methods.
School example: A class called Student with study and play methods.
Home example: A class called Pet with eat and sleep methods.
Nigerian example: A class called Trader with buy and sell methods.
Example code:
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
def study(self):
print(self.name + " is studying.")
def play(self):
print(self.name + " is playing.")
ada = Student("Ada", 14, "JSS3")
ada.study()
ada.play()
Output:
Ada is studying. Ada is playing.
Illustration:
class Student:
__init__ -> name, age, grade
study() -> prints "studying"
play() -> prints "playing"
|
V
ada = Student("Ada", 14, "JSS3")
|
V
ada.study() -> "Ada is studying."
Mini summary: A complete class has __init__ and methods. You create objects and call methods.
Definition: Dot notation is using a dot to access an object’s data or methods.
Why it is important: You need dot notation to use objects.
Simple explanation: You write the object name, then a dot, then the attribute or method.
Real-life example: Like saying “my phone’s colour”.
School example: Like saying “Ada’s bag”.
Home example: Like saying “Bingo’s food”.
Nigerian example: Like saying “Mama Nkechi’s shop”.
Example code:
my_car = Car("red", 60)
print(my_car.colour) # red
my_car.drive() # The car is driving at 60 km/h
Illustration:
my_car.colour
| |
| V
| attribute
V
object
Mini summary: Dot notation lets you access an object’s data and methods. Use a dot.
Definition: You can add as many methods as you want to a class.
Why it is important: More methods make your objects more useful.
Simple explanation: Just write another def inside the class.
Real-life example: A phone can call, text, and take photos.
School example: A student can study, play, and write.
Home example: A dog can bark, run, and eat.
Nigerian example: A trader can buy, sell, and count money.
Example code:
class Car:
def __init__(self, colour, speed):
self.colour = colour
self.speed = speed
def drive(self):
print("Driving at " + str(self.speed) + " km/h")
def stop(self):
print("Car has stopped.")
def honk(self):
print("Beep beep!")
Illustration:
class Car:
drive()
stop()
honk()
Mini summary: You can add many methods to a class. Each method defines an action.
Definition: Using objects means creating them and calling their methods in your program.
Why it is important: This is how OOP programs work.
Simple explanation: You create objects, then call methods on them.
Real-life example: A game creates characters and makes them move.
School example: A school program creates students and makes them study.
Home example: A smart home program creates lights and turns them on.
Nigerian example: A market program creates traders and makes them sell.
Example code:
car1 = Car("red", 60)
car2 = Car("blue", 80)
car1.drive()
car2.drive()
car1.stop()
car2.honk()
Output:
Driving at 60 km/h Driving at 80 km/h Car has stopped. Beep beep!
Illustration:
Create car1 -> drive() -> stop() Create car2 -> drive() -> honk()
Mini summary: Using objects means creating them and calling their methods. This is how OOP programs work.
Definition: These are mistakes beginners often make.
Why it is important: Knowing them helps you avoid them.
Simple explanation: Here are some mistakes to watch out for.
List of mistakes:
self in the method.__init__ with the right number of arguments.Illustration:
Bad Code Good Code +----------------+ +----------------+ | def drive(): | | def drive(self):| | print() | | print() | +----------------+ +----------------+
Mini summary: Avoid common mistakes. Always use self. Always indent. Always create objects first.
Definition: You have written a complete class and created objects.
Why it is important: Reviewing helps you remember.
Simple explanation: You learned how to write a class, use __init__, use self, create objects, and call methods.
Real-life example: Like finishing a small project.
School example: Like finishing your first essay.
Home example: Like cooking your first meal.
Nigerian example: Like making your first pot of jollof rice.
Illustration:
Write class
|
V
Add __init__
|
V
Add methods
|
V
Create objects
|
V
Call methods
|
V
Celebrate 🎉
Mini summary: You wrote your first complete program. You are now an OOP programmer!
| Word | Simple Definition |
|---|---|
| Python | A programming language that is easy to read. |
| Class | A blueprint for creating objects. |
| Object | A thing made from a class. |
| Instance | Another word for an object. |
| __init__ | A special method that sets up an object’s data. |
| self | A word that means “this object”. |
| Attribute | Data that an object holds. |
| Method | An action that an object can do. |
| Dot notation | Using a dot to access data or methods. |
| Argument | A value you pass to a method. |
| Indent | Moving code to the right. |
| Call | To run a method. |
class Name:.Name(data).def method(self):.class Name:.def __init__(self, ...):.self.attribute = value.def method(self):.my_car = Car("red", 60).my_car.drive().self is not a special word in Python?__init__ sets up data.self means “this object”.self in methods.self.
Write class
|
V
Add __init__
|
V
Add methods
|
V
Create object
|
V
Call methods
|
V
See output! 🎉
Lesson 1: What is Python? Lesson 2: Write a class Lesson 3: __init__ Lesson 4: self Lesson 5: Create object Lesson 6: Own data Lesson 7: Methods Lesson 8: Call methods Lesson 9: Multiple objects Lesson 10: Complete class Lesson 11: Dot notation Lesson 12: More methods Lesson 13: Use objects Lesson 14: Avoid mistakes Lesson 15: Complete program
| Feature | Class | Object |
|---|---|---|
| What is it? | A blueprint | A thing made from the blueprint |
| How many? | One class | Many objects |
| Example | Car design | Your red car |
| Exists in memory? | No | Yes |
| Feature | Attributes | Methods |
|---|---|---|
| What are they? | Data | Actions |
| Example (Student) | name, age | study, play |
| Example (Car) | colour, speed | drive, stop |
Lesson 1: Python is a programming language. It is easy to read and write.
Lesson 2: A class is written with the word class.
Lesson 3: __init__ is a special method that sets up an object’s data.
Lesson 4: self means “this object”.
Lesson 5: Creating an object means making a specific thing from a class.
Lesson 6: Each object can have its own data.
Lesson 7: A method is a function inside a class.
Lesson 8: Calling a method means telling the object to do the action.
Lesson 9: You can create many objects from one class.
Lesson 10: A complete class has __init__ and methods.
Lesson 11: Dot notation lets you access an object’s data and methods.
Lesson 12: You can add many methods to a class.
Lesson 13: Using objects means creating them and calling their methods.
Lesson 14: Avoid common mistakes. Always use self. Always indent.
Lesson 15: You wrote your first complete program. You are an OOP programmer!
In this module, you learned how to create classes and objects in Python. You learned what Python is and why it is good for beginners. You wrote your first class. You learned about __init__ and self. You created objects and gave them their own data. You wrote methods and called them. You created multiple objects from one class. You learned about dot notation. You also learned to avoid common mistakes. You wrote your first complete OOP program. You are now ready for Module Three, where you will learn about inheritance and polymorphism.
class Name: and indent the code.__init__? A special method that sets up an object’s data.self? A word that means “this object”.self, forgetting to indent, wrong arguments.Match the word to its meaning.
| Word | Meaning |
|---|---|
| 1. Class | A. A special method that sets up data |
| 2. Object | B. A blueprint for creating objects |
| 3. __init__ | C. A thing made from a class |
| 4. self | D. An action that an object can do |
| 5. Method | E. A word that means “this object” |
Answers: 1-B, 2-C, 3-A, 4-E, 5-D
class Car:)__init__)my_car. How do you make it drive? (my_car.drive())my_car.colour)Title: Write a Class Together
Instructions: In groups of 3, choose a thing (e.g., a phone, a student, a pet). Write a class in Python. Add __init__ and at least two methods. Create two objects from the class. Run the code together. Present to the class.
Title: My First Class
Instructions: Write a class called MyPet. Add __init__ with name and type. Add two methods: eat() and sleep(). Create two objects. Call the methods. Write down what happens.
Title: A Class for Your School
Instructions: Write a class called Student. Add __init__ with name, age, and grade. Add methods: study(), play(), and write(). Create three students. Call the methods. Print the results.
Task: Write a short paragraph (5 sentences) explaining what you learned about classes and objects. Then, list three things you found easy and one thing you found hard. Finally, describe the class you created and what it does.
__init__ sets up an object’s data.self means “this object”.self mean?In Module Three, we will learn about inheritance and polymorphism. You will learn how one class can inherit from another. You will learn how to reuse code. To get ready, make sure you:
See you in Module Three! 🚀
End of Module Two · Object Oriented Programming