Introduction to OOP

Lesson 1 of 7 • 10 XP

Keep your place in this quest

Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.

Introduction to Object-Oriented Programming in Python

Object-Oriented Programming (OOP) is a programming paradigm that lets you represent real-world concepts in terms of objects — things that have specific characteristics (data) and behaviors (actions).

Think of an object as a self‑contained thing in your program that bundles data and actions in one place—like a smartphone you own: it has data (brand, model, battery level) and it can perform actions (take a photo, make a call, play music). In code, that data lives in attributes (e.g., battery_level), and the actions are methods (e.g., take_photo()); together they form one coherent unit you can create, configure, and use.

The class is the blueprint (the design of the phone), and an object is a real, individual instance built from that blueprint (your specific phone with its current battery and photos). Each object keeps its own state, so two objects from the same class can carry different values at the same time, and they interact by calling each other’s methods rather than poking at internals—this idea is called encapsulation, and it helps you organize code, reduce bugs, and reason about behavior.

When you create an object, Python typically runs init to set its starting state; afterward, you ask the object to do things by calling its methods, which may read or update its attributes as needed. If you’ve built a game, imagine a Player with health and position that can move() and jump(); in a web app, a User with name and password_hash that can authenticate() and update_profile().

That’s all an object is: data + behavior, living together, created from a class and ready to work for you.

In Python, everything is an object.
That means code is written around objects and their interactions, making OOP a core part of Python and a major feature in popular libraries and frameworks like Django, Flask, and even many game engines.

In this lesson, we’ll explore the basic concepts of OOP in Python and learn how to use them to create our own objects.


Classes and Objects

OOP in Python is built around classes and objects:

  • A class is a blueprint or definition for a type of object. It describes what properties (called attributes) and what behaviors (called methods) that type of object will have.
  • An object is an instance of a class — it’s like a real version of the blueprint, with actual data stored in its attributes.

1.1. Creating a Class

To create a class in Python, use the class keyword, followed by the class name and a colon.

Example:

class Person:
    pass
````

Here we’ve created a class called `Person`.
The `pass` statement is a placeholder — it tells Python “do nothing here” until we add real code later.

TIP: By convention, class names use PascalCase (each word starts with a capital letter).


# Attributes and Methods

A class can have:

* **Attributes** — variables that hold information about the object.
* **Methods** — functions that define actions the object can perform.


## 2.1. Adding Attributes

To set up attributes when creating an object, we define them inside a special method called `__init__` (pronounced “dunder init” — short for *double underscore init*).

`__init__` runs automatically every time you create a new object from the class.
It’s used to initialize the object’s attributes with starting values.

Example:
```python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

What’s happening here:

  • The first parameter, self, is a reference to the specific object being created.
  • The other parameters (name, age) are values passed in when creating the object.
  • self.name and self.age are instance attributes that store the data inside the object.

IMPORTANT!: self must always be the first parameter in instance methods — it lets you access the object’s own attributes and methods.

2.2. Adding Methods

Methods are functions that belong to a class. They usually perform actions that use or change the object’s attributes.

Example:

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

    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

Here:

  • We’ve added a method called greet.
  • It uses the object’s own attributes (self.name and self.age) to display a personalized message.

Creating and Using Objects

Once you’ve defined a class, you can create objects from it just like calling a function — but with the class name.

Example:

person1 = Person("Alice", 30)
person1.greet()

Output:

Hello, my name is Alice and I am 30 years old.

Wrapping Up

In this lesson, you learned:

  • The difference between classes and objects.
  • How to use __init__ to set up attributes when creating objects.
  • How to define methods that let objects perform actions.

This is the foundation of OOP in Python. Next, we’ll expand on this by exploring inheritance — a way to create new classes that build on existing ones.