Skip to content

Introduction to Object-Oriented Python

Programming Python Difficulty 1/5

Beginning of the lesson

Introduction to Object-Oriented Python

Model real things with classes and objects

Big Idea: A class is a blueprint for a kind of thing, and an object is one real thing built from that blueprint. If Dog is the blueprint, then Rex and Bella are two dog objects made from it. Each object carries its own information and can do its own actions.
By the end you will be able to:
  • explain what a class and an object are,
  • write a class with __init__, attributes, and methods,
  • create objects and use their attributes and methods.

1. Classes and objects: the blueprint idea

Think about a cookie cutter. The cutter is not a cookie — it is the shape that decides how each cookie will look. You can use one cutter to make many cookies. Each cookie is real and separate, but they all follow the same shape.

In Python, a class is like the cookie cutter (the blueprint), and an object is like one real cookie made from it. We say an object is an instance of the class.

Blueprint class Dog: Dog("Rex", 3) a dog object Dog("Bella", 5) another dog object
One class (the Dog blueprint) can make many separate objects, each with its own information.
One blueprint, many objects. The class describes what every dog has and does; each object fills in its own details.

2. Writing your first class

We create a class with the word class, a name that starts with a capital letter, and a colon. Everything inside is indented. Here is a simple Dog class:

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

A few new words to notice:

  • class Dog: starts the blueprint. Class names usually start with a Capital letter.
  • def __init__(self, ...) is a special setup method. It runs automatically every time we make a new object.
  • self means this particular object. It lets each object keep its own data.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers