Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Fundamentals of Object Oriented Programming Level One · Lesson 3 of 6

Module Two

📖 Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
3
Module Two · Object Oriented Programming

📘 Module Two: Creating Classes and Objects in Code

🌟 Module Introduction

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!

🎯 Learning Objectives

After you finish this module, you will be able to:

  • Explain what Python is and why it is good for beginners.
  • Write a simple class in Python.
  • Use the __init__ method to set up attributes.
  • Create objects from a class.
  • Give each object its own data.
  • Write methods inside a class.
  • Call methods on an object.
  • Understand the self keyword.
  • Create multiple objects from one class.
  • Use objects in a simple program.
  • Avoid common mistakes when writing classes.
  • Feel confident to write your own classes.

📖 Warm-up Story: Ada and the Robot Factory

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.

📚 Main Lessons

Lesson 1: What is Python?

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.

Lesson 2: Writing Your First Class

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.

Lesson 3: The __init__ Method – Setting Up Your Object

Definition: 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.

Lesson 4: What is 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.

Lesson 5: Creating an 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.

Lesson 6: Giving Each Object Its Own 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.

Lesson 7: Writing Methods

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.

Lesson 8: Calling Methods on an Object

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.

Lesson 9: Creating Multiple Objects

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.

Lesson 10: Putting It All Together – A Complete Class

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.

Lesson 11: Understanding the Dot Notation

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.

Lesson 12: Adding More Methods

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.

Lesson 13: Using Objects in a Program

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.

Lesson 14: Common Mistakes When Writing Classes

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:

  • Forgetting to put self in the method.
  • Forgetting to indent the code inside the class.
  • Forgetting to call __init__ with the right number of arguments.
  • Using the wrong variable name.
  • Forgetting to create an object before calling a method.

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.

Lesson 15: Your First Complete Program – Review and Celebrate

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!

📖 Key Vocabulary

WordSimple Definition
PythonA programming language that is easy to read.
ClassA blueprint for creating objects.
ObjectA thing made from a class.
InstanceAnother word for an object.
__init__A special method that sets up an object’s data.
selfA word that means “this object”.
AttributeData that an object holds.
MethodAn action that an object can do.
Dot notationUsing a dot to access data or methods.
ArgumentA value you pass to a method.
IndentMoving code to the right.
CallTo run a method.

🔑 Important Concepts

  • Python is easy: It reads like English.
  • Class is a blueprint: You write class Name:.
  • __init__ sets up data: It runs when you create an object.
  • self means “this object”: It is used inside the class.
  • Object is a thing: You create it with Name(data).
  • Methods are actions: You write def method(self):.
  • Dot notation accesses data: Use a dot.
  • One class, many objects: Create as many as you want.

🧭 Step-by-Step Explanations

How to write a class

  1. Write class Name:.
  2. Indent the next lines.
  3. Write def __init__(self, ...):.
  4. Set up attributes with self.attribute = value.
  5. Write other methods with def method(self):.

How to create an object

  1. Write the class name.
  2. Add parentheses with the data.
  3. Example: my_car = Car("red", 60).

How to call a method

  1. Write the object name.
  2. Add a dot.
  3. Write the method name and parentheses.
  4. Example: my_car.drive().

🌍 Real-life Examples

  • A car class with colour and speed.
  • A student class with name and age.
  • A bank account class with balance and deposit.
  • A pet class with name and breed.
  • A phone class with brand and call.

🇳🇬 Nigerian Examples

  • A trader class with name and goods.
  • A danfo bus class with route and fare.
  • A student class with name and class.
  • A bank account class with balance.
  • A market stall class with owner and goods.

🎈 Fun Examples Children Can Relate To

  • A video game character class with health and attack.
  • A football class with size and bounce.
  • A toy class with colour and sound.
  • A pet class with name and run.
  • A bicycle class with wheels and move.

🏠 Everyday Examples

  • Your phone is an object with data and actions.
  • Your school bag is an object with data and actions.
  • Your fridge is an object with data and actions.
  • Your television is an object with data and actions.
  • Your watch is an object with data and actions.

👨‍👩‍👧 Parent Tips

  • Encourage your child to write simple classes.
  • Ask them to explain what a class and an object are.
  • Help them install Python on a computer.
  • Celebrate when they create their first object.
  • Remind them that mistakes are part of learning.

💡 Interesting Facts

  • Python was created in 1991.
  • It is named after a comedy group called Monty Python.
  • Python is used by Google, NASA, and Netflix.
  • You can write Python on any computer.
  • Python is one of the most popular languages in the world.

🤔 Did You Know?

  • Did you know that a class can have no objects?
  • Did you know that one class can make thousands of objects?
  • Did you know that objects can have other objects inside them?
  • Did you know that self is not a special word in Python?
  • Did you know that you can change an object’s data after creating it?

📌 Remember This

  • Python is easy to read.
  • Class is a blueprint.
  • Object is a thing made from a class.
  • __init__ sets up data.
  • self means “this object”.
  • Methods are actions.
  • Dot notation accesses data and methods.
  • One class can make many objects.

⚠️ Common Mistakes

  • Forgetting self in methods.
  • Forgetting to indent.
  • Wrong number of arguments.
  • Wrong variable name.
  • Forgetting to create an object before calling a method.

✅ Best Practices

  • Always use self.
  • Always indent properly.
  • Give classes clear names.
  • Give methods clear names.
  • Test your code often.
  • Keep your code organised.

📊 Illustrations, Diagrams, and Tables

Flowchart: Writing a Class and Creating Objects

  Write class
        |
        V
  Add __init__
        |
        V
  Add methods
        |
        V
  Create object
        |
        V
  Call methods
        |
        V
  See output! 🎉
  

Timeline: Your Python Journey

  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
  

Table: Class vs Object

FeatureClassObject
What is it?A blueprintA thing made from the blueprint
How many?One classMany objects
ExampleCar designYour red car
Exists in memory?NoYes

Table: Attributes vs Methods

FeatureAttributesMethods
What are they?DataActions
Example (Student)name, agestudy, play
Example (Car)colour, speeddrive, stop

📝 Summary After Every Lesson

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!

📦 End-of-Module Summary

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.

❓ Frequently Asked Questions (10 questions)

  1. What is Python? A programming language that is easy to read.
  2. How do I write a class? Write class Name: and indent the code.
  3. What is __init__? A special method that sets up an object’s data.
  4. What is self? A word that means “this object”.
  5. How do I create an object? Write the class name with parentheses and data.
  6. What is a method? A function inside a class.
  7. How do I call a method? Write the object name, a dot, and the method name.
  8. Can I create many objects? Yes, as many as you want.
  9. What is dot notation? Using a dot to access data or methods.
  10. What are common mistakes? Forgetting self, forgetting to indent, wrong arguments.

🔗 Matching Exercises

Match the word to its meaning.

WordMeaning
1. ClassA. A special method that sets up data
2. ObjectB. A blueprint for creating objects
3. __init__C. A thing made from a class
4. selfD. An action that an object can do
5. MethodE. A word that means “this object”

Answers: 1-B, 2-C, 3-A, 4-E, 5-D

🧩 Scenario-based Exercises

  1. You want to create a class for cars. What do you write first? (class Car:)
  2. You want to give each car a colour. Where do you set it? (In __init__)
  3. You create a car called my_car. How do you make it drive? (my_car.drive())
  4. You want to access the car’s colour. How do you do it? (my_car.colour)
  5. You want to create three cars. How many classes do you need? (One)

👥 Group Activity

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.

🧑 Individual Activity

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.

🛠️ Mini Project

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.

📋 Practical Assignment

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.

🔑 Key Takeaways

  • Python is easy to read and write.
  • A class is a blueprint for objects.
  • An object is a thing made from a class.
  • __init__ sets up an object’s data.
  • self means “this object”.
  • Methods are actions that objects can do.
  • Dot notation accesses data and methods.
  • One class can make many objects.
  • Always test your code.

💬 Classroom Discussion Questions

  1. Why is Python good for beginners?
  2. What is the difference between a class and an object?
  3. What does self mean?
  4. How do you create an object?
  5. What would you like to build with classes and objects?

➡️ Preparation for the Next Module

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:

  • Understand how to write a class.
  • Know how to create objects.
  • Know how to call methods.
  • Have practised writing your own classes.

See you in Module Three! 🚀


End of Module Two · Object Oriented Programming

🏆 Get Certified

🔒

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

🎓 Sign Up to Get Certified
🛠️ Practice Tools
Hands-on simulators & labs - subscription required.
→
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
→