We use different ways to organize code so it is easier to understand and reuse. Procedural and object-oriented are two common ways to do this.
Procedural vs object-oriented approach in Python
Start learning this pattern below
Jump into concepts and practice - no test required
Procedural approach: def greet(name): print(f"Hello, {name}!") greet("Alice") Object-oriented approach: class Greeter: def __init__(self, name): self.name = name def greet(self): print(f"Hello, {self.name}!") person = Greeter("Alice") person.greet()
Procedural code is a list of instructions or functions that run one after another.
Object-oriented code groups data and functions inside classes to create objects.
def add(a, b): return a + b result = add(3, 4) print(result)
class Calculator: def add(self, a, b): return a + b calc = Calculator() print(calc.add(3, 4))
def greet(name): print(f"Hi, {name}!") greet("Bob")
class Person: def __init__(self, name): self.name = name def greet(self): print(f"Hi, {self.name}!") p = Person("Bob") p.greet()
This program shows both ways to greet someone. First, a simple function prints a greeting. Then, a class creates an object that prints a greeting.
def procedural_greet(name): print(f"Hello from procedural, {name}!") class Greeter: def __init__(self, name): self.name = name def greet(self): print(f"Hello from object-oriented, {self.name}!") # Using procedural approach procedural_greet("Alice") # Using object-oriented approach person = Greeter("Alice") person.greet()
Procedural code is easier for small tasks but can get messy as programs grow.
Object-oriented code helps organize complex programs by bundling data and actions.
Both approaches can be mixed depending on what fits best.
Procedural programming uses functions and instructions step-by-step.
Object-oriented programming uses classes and objects to group data and behavior.
Choosing the right approach helps keep code clear and easy to maintain.
Practice
Solution
Step 1: Understand procedural programming basics
Procedural programming organizes code as functions and instructions executed in order.Step 2: Understand object-oriented programming basics
Object-oriented programming organizes code using classes and objects that combine data and behavior.Final Answer:
Procedural programming uses functions and step-by-step instructions; object-oriented programming uses classes and objects. -> Option DQuick Check:
Procedural = functions, OOP = classes/objects [OK]
- Thinking procedural can't use variables
- Believing OOP is always slower
- Confusing program size with programming style
Solution
Step 1: Recall Python class syntax
In Python, classes are defined using the keywordclassfollowed by the class name and parentheses.Step 2: Check each option
class MyClass(): pass uses correct Python syntax. def MyClass(): pass usesdefwhich defines a function, not a class. function MyClass() {} uses JavaScript syntax. class MyClass[]: pass uses invalid brackets.Final Answer:
class MyClass(): pass -> Option BQuick Check:
Python classes start with 'class' keyword [OK]
- Using def instead of class
- Using wrong brackets [] instead of ()
- Confusing Python with other languages syntax
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())Solution
Step 1: Understand the procedural function greet
The function greet(name) returns the string "Hello, {name}!" with the given name.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!".Final Answer:
Hello, Anna! -> Option CQuick Check:
Class method calls procedural function correctly [OK]
- Confusing variable name with string 'name'
- Expecting error due to mixing styles
- Forgetting to use self.name
class Calculator:
def add(self, a, b):
return a + b
result = Calculator.add(3, 4)
print(result)Solution
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.Step 2: Correct usage
To fix, create an instance: calc = Calculator() then call calc.add(3, 4). This passes self automatically.Final Answer:
Missing self argument when calling add method -> Option AQuick Check:
Instance methods need self, call via instance [OK]
- Calling instance method directly on class
- Ignoring self parameter
- Assuming methods are static by default
# Procedural code
def area_rectangle(width, height):
return width * height
w = 5
h = 3
print(area_rectangle(w, h))Solution
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.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.Final Answer:
class Rectangle with __init__ storing width and height, and area method using them -> Option AQuick Check:
OOP encapsulates data and behavior in class [OK]
- Not using self for instance variables
- Returning values from __init__
- Defining methods without self parameter
