Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 12

Functional and Object-Oriented Programming

Functions vs classes—and when to mix both styles in Python.

Beginner10 min

What you will learn

By the end of this lesson, you'll understand:

  • How does functional programming work in Python?
  • How does object-oriented programming (OOP) work?
  • When should you choose functions vs classes?
  • How can both styles exist in the same program?

Python is a Multi-Paradigm Language

Python supports multiple programming styles. The two most common are:

Functional Programming Object-Oriented Programming
Organize code using reusable functions Organize code using classes and objects
Best for small tasks Best for large applications

Key idea: You choose the style that best fits the problem.

Functional Programming

Functional programming organizes programs around functions.

A function performs one specific task and can be reused many times.

Example

python
def add(a, b):
    return a + b

def multiply(a, b):
    return a * b

print("Addition =", add(10, 20))
print("Multiplication =", multiply(10, 20))

Output

text
Addition = 30
Multiplication = 200

Visual explanation

Function receives input, processes, returns result

A function receives inputs, processes them, and returns a result.

Why Use Functions?

Functions make code:

  • Reusable
  • Short
  • Easy to test
  • Easy to maintain

Example

Instead of writing addition logic repeatedly:

python
result1 = add(5, 3)
result2 = add(20, 40)
result3 = add(100, 50)

The same function solves multiple problems.

Object-Oriented Programming (OOP)

Object-oriented programming organizes code around classes and objects.

What is a Class?

A class is a blueprint.

What is an Object?

An object is a real instance created from that blueprint.

Real-world analogy

Student blueprint vs Ravi object

The class defines the structure; the object stores actual data.

OOP Example

python
class Student:

    def __init__(self, name, marks):
        self.name = name
        self.marks = marks

    def display(self):
        print("Name =", self.name)
        print("Marks =", self.marks)

s1 = Student("Ravi", 85)

s1.display()

Output

text
Name = Ravi
Marks = 85

Understanding the code

Part Purpose
class Student Creates blueprint
__init__() Initializes object data
self.name Stores student's name
display() Prints student details
Student() Creates an object

Functions vs Classes

Functional Object-Oriented
Uses functions Uses classes
Focus on operations Focus on data + behavior
Small reusable tasks Large structured systems
Example: Calculator Example: Student

Neither is better—the problem determines the choice.

Mixing Both Styles

Python allows both paradigms in the same file.

Example

python
class Calculator:

    def add(self, a, b):
        return a + b

def subtract(a, b):
    return a - b

cal = Calculator()

print("Addition =", cal.add(30, 20))
print("Subtraction =", subtract(30, 20))

Output

text
Addition = 50
Subtraction = 10

Visual structure

Class method vs standalone function

This is completely normal in professional Python projects.

When Should You Use Each?

Use Functional Programming when:

  • Writing utility functions
  • Performing calculations
  • Data transformations
  • Small scripts
  • Automation tasks

Example

python
def calculate_tax(amount):
    return amount * 0.18

Use Object-Oriented Programming when:

  • Building enterprise software
  • Managing users
  • CRM systems
  • Banking applications
  • Property management systems

Example

python
class Customer:
    pass

class Property:
    pass

class Booking:
    pass

Notice how real applications naturally become collections of classes.

Practice Exercise

Employee Salary Calculator

Build both paradigms together.

python
def calculate_bonus(salary):
    return salary * 0.10

class Employee:

    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def display_salary_details(self):
        bonus = calculate_bonus(self.salary)

        print("Name =", self.name)
        print("Salary =", self.salary)
        print("Bonus =", bonus)
        print("Total Salary =", self.salary + bonus)

emp = Employee("Anil", 50000)

emp.display_salary_details()

Expected output

text
Name = Anil
Salary = 50000
Bonus = 5000.0
Total Salary = 55000.0

Key Points

  • Python supports both functional and object-oriented programming.
  • Functions are best for small, reusable operations.
  • Classes organize data and related behavior together.
  • Objects are instances created from classes.
  • Mixing both styles in one Python program is common and recommended when appropriate.

Interview Questions

  1. What is functional programming?
  2. What is object-oriented programming?
  3. What is the difference between a class and an object?
  4. When should you use functions instead of classes?
  5. Can Python support both paradigms in one file?

Summary

Functional Programming Object-Oriented Programming
Built around functions Built around classes
Reusable operations Reusable objects
Ideal for small tasks Ideal for large applications
Example: add() Example: Student

Python is called a multi-paradigm language because it lets you solve problems using whichever style produces the clearest and most maintainable solution. In real-world projects—such as CRM systems, AI tools, and automation—you'll often combine both approaches naturally.

Next

Next lesson: Extensible & Embedded Python — the final lesson of this module explores how Python integrates with other languages and shares functionality across larger software systems.