Python Object-Oriented Programming

Let’s look at object-oriented programming

  1. Notice

    A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
class Counter:
    def __init__(self, value):
        self.value = value
    def increment(self):
        self.value += 1
counter = Counter(3)
counter.increment()
print(counter.value)
Live state

Top-level scope

Counter→Object 2
counter→Object 3
Object 2 · typeclass CounterObject 3 · CounterCounter({'value': 4})
This step

A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.

Picture object-oriented programming

A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.

Image brief

Alt text
A diagram for Python Object-Oriented Programming. Instructions: class Counter: def __init__(self, value): self.value = value def increment(self): self.value += 1 counter = Counter(3) counter.increment() print(counter.value). Displayed result: 4. The final example bindings are Counter --> object 2 (type); counter --> object 3 (Counter). object 2: class Counter; object 3: Counter({'value': 4})
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Object-Oriented Programming". Teaching focus: A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS class Counter: def __init__(self, value): self.value = value def increment(self): self.value += 1 counter = Counter(3) counter.increment() print(counter.value) | v DISPLAYED RESULT 4 FINAL BINDINGS Counter --> object 2 (type) counter --> object 3 (Counter) object 2: class Counter object 3: Counter({'value': 4}) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Object-Oriented Programming. Instructions: class Counter: def __init__(self, value): self.value = value def increment(self): self.value += 1 counter = Counter(3) counter.increment() print(counter.value). Displayed result: 4. The final example bindings are Counter --> object 2 (type); counter --> object 3 (Counter). object 2: class Counter; object 3: Counter({'value': 4})

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
class Counter:
    def __init__(self, value):
        self.value = value
    def increment(self):
        self.value += 1
counter = Counter(3)
counter.increment()
print(counter.value)
Live state

Top-level scope

Counter→Object 2
counter→Object 3
Object 2 · typeclass CounterObject 3 · CounterCounter({'value': 4})
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
4

What do you expect this program to print?

What does this example display?

class Counter:
    def __init__(self, value):
        self.value = value
    def increment(self):
        self.value += 1
counter = Counter(3)
counter.increment()
print(counter.value)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

4

Follow the printed values in order. The output is: 4

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.

A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.

More examples and reference

When writing programs, we often deal with real-world things like students, cars, or bank accounts. These things have data (like a student's name or a car's color) and actions (like driving a car or updating a bank balance).

Object-Oriented Programming (OOP) helps us model these things in code using objects and classes.

Let’s See a Simple Example

Imagine you want to create a Dog. Every dog has a name and can bark. name is the attribute of a dog, while barking is its behaviour(method).

# Defining a class
class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):
        print(self.name + " says Woof!")

# Creating objects
dog1 = Dog("Tommy")
dog2 = Dog("Buddy")

# Calling methods
dog1.bark()
dog2.bark()

Output

Tommy says Woof!
Buddy says Woof!

What’s Happening Here?

  • class Dog: creates a class named Dog.
  • __init__() is a special method that runs when we create an object. It sets the dog’s name.
  • self.name stores the name of the dog.
  • bark() is a method that prints a message using the dog’s name.
  • dog1 = Dog("Tommy") creates a dog named Tommy.
  • dog1.bark() tells Tommy to bark!

Key Concepts Here

  • Class: A blueprint or template for creating objects.
  • Object: An actual thing created from the class.
  • Attributes: Variables that belong to an object (like name, age).
  • Methods: Functions that belong to an object (like bark(), run()).

Let’s Add Another Method

We’ll make our Dog learn a new trick: sit.

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

    def bark(self):
        print(self.name + " says Woof!")

    def sit(self):
        print(self.name + " sits down.")

dog = Dog("Charlie")
dog.bark()
dog.sit()

Output

Charlie says Woof!
Charlie sits down.

Understanding self

self is used to refer to the current object. It lets each object keep its own data separate from other objects.

Why Do We Need OOP?

  • Organized code: OOP keeps related code (data + actions) together.
  • Reusability: You can reuse classes to create many similar objects.
  • Easy to manage: Large projects are easier to understand and maintain.

What to remember

Connect what you noticed in the example with the idea below.

Remember

A class describes objects with data and behavior. Each instance can have its own attributes, accessed through self inside methods.

Watch out for this idea

Every instance must share the same value for every attribute.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Inheritance →