1 of 19

BinaryTree

Intermediate

Python Course

BinaryTree Digital Education Program

2 of 19

Week 4

Introduction to OOP

(Object-Oriented Programming)

3 of 19

Think About It

What is OOP?

Let's think about the Harry Potter series...

→ What is each book part of?

→ What does each book have (attributes)?

→ What are some actions in each book (methods)?

4 of 19

What is Object-Oriented Programming?

  • OOP organizes code into objects that contain both data (attributes) and actions (methods).
  • Models real-world things in code — a Car class can have properties (color, brand) and actions (drive, stop).
  • Based on key concepts: encapsulation, inheritance, and polymorphism.
  • Python supports OOP but is not fully OOP-based. Java, on the other hand, is.

5 of 19

Open-Ended Question

Give a few reasons why developers use Object-Oriented Programming while building software.

⏱ 5 Minutes

6 of 19

Classes & Objects

Class

The Blueprint

Defines how objects should behave — like a recipe telling you how to make a dish.

Object

An Instance

An actual instance of a class, with its own unique data — like a specific dish (pasta or salad).

Many objects can be created from one class — code reuse with unique data per object.

7 of 19

Example — Classes & Objects

Based on this code, what would you expect as the output?

class Car:� def __init__(self, brand, color):� self.brand = brand� self.color = color�� def describe(self):� print(f"This is a {self.color} {self.brand}.")��my_car = Car("Toyota", "red")�my_car.describe()

8 of 19

Instance Variables and Methods

Instance Variables

Store unique data for each object (e.g., a car's color or brand). Created inside __init__.

Instance Methods

Functions inside a class that allow objects to perform actions — drive, brake, honk.

Together, they let objects store information and interact — making programs more dynamic.

9 of 19

The Concept of "self" in Python

1

self represents the current instance of the class.

2

Each object maintains its own separate data — self ensures the correct object's attributes are used.

3

Without self, Python couldn't differentiate between different objects.

def greet(self): → self.name is this object's name, not some other object's

10 of 19

Constructors and Class Attributes

Constructor (__init__)

Automatically called when an object is created. Initializes instance variables.

def __init__(self, name):

self.name = name

Class Attributes

Belong to the class itself — all instances share the same value unless overridden.

species = "Mammal" # shared by all

Instance Attributes

Unique to each object. Set via self inside __init__.

self.name = name # unique per object

11 of 19

Optional Parameters

Some class parameters have preset (default) values. If not provided, the default is used.

class Car:� def __init__(self, brand="Unknown", color="White"):� self.brand = brand� self.color = color��car1 = Car("Ferrari", "Red") # all params provided�car2 = Car("Bugatti") # color defaults to White�car3 = Car() # both default�

12 of 19

Reference Variables — Scope

Type

Where Defined

Scope

Access

Method Variable

Inside a function/method

Local — exists only within the function

Cannot be accessed outside

Global Variable

Outside all functions

Global — accessible throughout the program

Use carefully — avoid naming conflicts

Instance Variable (self.)

Inside __init__

Object-level — unique per instance

Access via self.variable_name

13 of 19

Open-Ended Question

Differentiate between method variables and global variables.

→ When would you use each?

→ What happens if they share the same name inside a class?

⏱ 5 Minutes

14 of 19

Clean Code vs. Spaghetti Code

Clean Code

Well-organized, structured, follows OOP principles.

• Low coupling between classes

• Easy to read and debug

• Efficient and maintainable

Spaghetti Code

Unstructured, tightly coupled, hard to follow.

• Leads to harder-to-catch bugs

• Performance issues

• Term coined to describe "tangled" logic

Always aim for clean, organized OOP — it makes your code easier for others (and your future self) to understand.

15 of 19

Practice Question

What do class attributes define?

A

Which syntax to use for a certain scenario.

B

Shared properties across all objects in a class.

C

Parameters to use when defining objects.

D

Variables to assign to values in your code.

16 of 19

Practice Question

What is unstructured, inefficient, and unorganized code called?

A

Pasta code

B

Salad code

C

Spaghetti code

D

Cereal code

17 of 19

Practice Question

What would print(dog.species + " " + dog.name) output if species = "Mammal" and name = "Python"?

A

C++ Python

B

C++ Mammal

C

species Python

D

Mammal Python

18 of 19

Practice Question

How are instance variables created?

A

By using the constructor __init__.

B

By running print(instance_variable).

C

By using only self.

D

By using swapping logic with a placeholder variable.

19 of 19

Practice Question

In Python, what is the purpose of "self"?

A

To show the user's coding software (IDE).

B

To represent the current instance of a class.

C

To represent the current instance of an object.

D

To represent the current instance of a method variable.