Bird
Raised Fist0
Pythonprogramming~10 mins

Procedural vs object-oriented approach in Python - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Procedural vs object-oriented approach
Start
Choose Approach
Procedural
Write functions
Object-Oriented
Define class
This flow shows two ways to organize code: procedural uses functions step-by-step, object-oriented uses classes and objects with methods.
Execution Sample
Python
def add_proc(x, y):
    return x + y

class Adder:
    def add(self, x, y):
        return x + y

result_proc = add_proc(2, 3)
adder = Adder()
result_obj = adder.add(2, 3)
print(result_proc)
print(result_obj)
This code shows adding two numbers using a procedural function and an object-oriented class method.
Execution Table
StepActionEvaluationResult
1Define function add_procStore function in memoryFunction ready to use
2Define class Adder with method addStore class and methodClass ready to create objects
3Call add_proc(2, 3)Add 2 + 35
4Create object adder = Adder()Allocate objectObject adder created
5Call adder.add(2, 3)Call method add with 2,35
6Print resultsOutput values5 and 5 printed
💡 All steps executed, program ends after printing results
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
add_procundefinedfunction storedfunction storedfunction storedfunction stored
Adderundefinedclass storedclass storedclass storedclass stored
adderundefinedundefinedobject createdobject createdobject created
result_procundefined5555
result_objundefinedundefinedundefined55
Key Moments - 3 Insights
Why do we need to create an object to use the add method in the object-oriented approach?
Because the add method is inside the class and works on objects, so we must create an object (see execution_table step 4 and 5) to call the method.
Can we call the procedural function without creating anything?
Yes, the procedural function is standalone and can be called directly (see execution_table step 3).
Are the results from both approaches the same?
Yes, both return 5 when adding 2 and 3 (see execution_table steps 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of calling add_proc(2, 3)?
A2
B3
C5
DError
💡 Hint
Check execution_table row 3 under Result column
At which step is the object 'adder' created?
AStep 4
BStep 3
CStep 5
DStep 2
💡 Hint
Look at execution_table row 4 Action column
If we remove the class and only keep the procedural function, what changes in variable_tracker?
A'result_obj' will be 5
B'adder' variable will be undefined throughout
C'add_proc' will be undefined
D'Adder' will be an object
💡 Hint
See variable_tracker row for 'adder' which is created only after Step 4
Concept Snapshot
Procedural approach:
- Write functions
- Call functions directly
- Simple and linear

Object-oriented approach:
- Define classes
- Create objects
- Call methods on objects

Both can produce same results but organize code differently.
Full Transcript
This lesson compares procedural and object-oriented programming. Procedural uses functions you call directly. Object-oriented uses classes to create objects, then calls methods on those objects. The example adds two numbers both ways. The execution table shows defining functions and classes, calling them, and printing results. Variables track function, class, and object states. Key moments clarify why objects are needed for methods and that procedural functions are standalone. The quiz checks understanding of steps and variable states. The snapshot summarizes the main differences simply.

Practice

(1/5)
1. Which statement best describes the main difference between procedural and object-oriented programming in Python?
easy
A. Procedural programming is faster than object-oriented programming in all cases.
B. Procedural programming is only for small programs; object-oriented programming is for large programs.
C. Procedural programming cannot use variables; object-oriented programming can.
D. Procedural programming uses functions and step-by-step instructions; object-oriented programming uses classes and objects.

Solution

  1. Step 1: Understand procedural programming basics

    Procedural programming organizes code as functions and instructions executed in order.
  2. Step 2: Understand object-oriented programming basics

    Object-oriented programming organizes code using classes and objects that combine data and behavior.
  3. Final Answer:

    Procedural programming uses functions and step-by-step instructions; object-oriented programming uses classes and objects. -> Option D
  4. Quick Check:

    Procedural = functions, OOP = classes/objects [OK]
Hint: Procedural = steps; OOP = objects/classes [OK]
Common Mistakes:
  • Thinking procedural can't use variables
  • Believing OOP is always slower
  • Confusing program size with programming style
2. Which of the following is the correct way to define a class in Python?
easy
A. def MyClass(): pass
B. class MyClass(): pass
C. function MyClass() {}
D. class MyClass[]: pass

Solution

  1. Step 1: Recall Python class syntax

    In Python, classes are defined using the keyword class followed by the class name and parentheses.
  2. Step 2: Check each option

    class MyClass(): pass uses correct Python syntax. def MyClass(): pass uses def which defines a function, not a class. function MyClass() {} uses JavaScript syntax. class MyClass[]: pass uses invalid brackets.
  3. Final Answer:

    class MyClass(): pass -> Option B
  4. Quick Check:

    Python classes start with 'class' keyword [OK]
Hint: Classes start with 'class' keyword in Python [OK]
Common Mistakes:
  • Using def instead of class
  • Using wrong brackets [] instead of ()
  • Confusing Python with other languages syntax
3. What will be the output of this Python code?
def greet(name):
    return f"Hello, {name}!"

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

p = Person("Anna")
print(p.greet())
medium
A. TypeError
B. Hello, name!
C. Hello, Anna!
D. AttributeError

Solution

  1. Step 1: Understand the procedural function greet

    The function greet(name) returns the string "Hello, {name}!" with the given name.
  2. Step 2: Understand the Person class and method call

    The Person class stores the name and its greet method calls the procedural greet function with self.name. Creating p with name "Anna" and calling p.greet() returns "Hello, Anna!".
  3. Final Answer:

    Hello, Anna! -> Option C
  4. Quick Check:

    Class method calls procedural function correctly [OK]
Hint: Class method calls function with self.name [OK]
Common Mistakes:
  • Confusing variable name with string 'name'
  • Expecting error due to mixing styles
  • Forgetting to use self.name
4. Identify the error in this code that mixes procedural and object-oriented styles:
class Calculator:
    def add(self, a, b):
        return a + b

result = Calculator.add(3, 4)
print(result)
medium
A. Missing self argument when calling add method
B. Class Calculator is not defined
C. add method should not return a value
D. print statement syntax error

Solution

  1. Step 1: Understand method call on class vs instance

    The add method is an instance method requiring a self parameter. Calling Calculator.add(3, 4) misses the self argument.
  2. Step 2: Correct usage

    To fix, create an instance: calc = Calculator() then call calc.add(3, 4). This passes self automatically.
  3. Final Answer:

    Missing self argument when calling add method -> Option A
  4. Quick Check:

    Instance methods need self, call via instance [OK]
Hint: Call instance methods on object, not class [OK]
Common Mistakes:
  • Calling instance method directly on class
  • Ignoring self parameter
  • Assuming methods are static by default
5. You want to convert this procedural code into an object-oriented style. Which class design correctly encapsulates the data and behavior?
# Procedural code
def area_rectangle(width, height):
    return width * height

w = 5
h = 3
print(area_rectangle(w, h))
hard
A. class Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height
B. class Rectangle: def area(width, height): return width * height
C. class Rectangle: def __init__(self): pass def area(self): return width * height
D. class Rectangle: def __init__(self, width, height): return width * height

Solution

  1. Step 1: Identify data and behavior to encapsulate

    The procedural code uses width and height as data and area_rectangle as behavior. In OOP, these should be inside a class.
  2. Step 2: Check class options for correct encapsulation

    class Rectangle: def __init__(self, width, height): self.width = width self.height = height def area(self): return self.width * self.height stores width and height as instance variables and defines area() method using them. Other options either miss self, lack data storage, or misuse return in constructor.
  3. Final Answer:

    class Rectangle with __init__ storing width and height, and area method using them -> Option A
  4. Quick Check:

    OOP encapsulates data and behavior in class [OK]
Hint: Store data in __init__, use methods for behavior [OK]
Common Mistakes:
  • Not using self for instance variables
  • Returning values from __init__
  • Defining methods without self parameter