Home Blogs All Courses
python c OA ( Online Assessment ) Amazon Web Services (AWS) leetcode dsa algorithms java Machine learning
Get Referral
Login Sign Up

python › Programming Paradigms in Python

Programming Paradigms in Python: Procedural vs OOP vs Functional

phoolchandra  ·  Oct 23, 2026  · Published


There is more than one way to solve a problem — and more than one style of writing the solution. These styles are called programming paradigms. Python is special because it supports several of them. Understanding paradigms will help you write cleaner code, read other people's code, and get ready for Object-Oriented Programming, which is coming next in this series.

Watch the video first, then use this blog as your notes. Every section has runnable examples, and at the end you will find a "Predict the Output" quiz, practice challenges, a self-check list, interview questions and a quick summary.

How to use this blog: Open the Python shell (type python in your terminal) or any online Python editor and type every example yourself. Change the values, break the code and see what happens — that is how concepts really stick.

1. What is a Programming Paradigm?

A programming paradigm is a style or approach of organising and writing code. It is not a language feature — it is a way of thinking.

Analogy: you can travel from Lucknow to Delhi by train, bus or car. The destination is the same; the way you get there is different. Similarly, the same program can be written in different paradigms.

Programming Paradigms
                    /                     \
           Imperative                      Declarative
          (HOW to do it)                  (WHAT you want)
          /           \                   /            \
   Procedural   Object-Oriented     Functional      Logic / SQL
ImperativeDeclarative
Describes how to do something, step by stepDescribes what result you want
You manage loops, variables and state changesThe language/library handles the steps
Examples: procedural, object-orientedExamples: functional, SQL, HTML
Python: a for loop with a running totalPython: sum(numbers) or a list comprehension

2. Let's Solve One Problem in Every Paradigm

Problem: Given a list of marks, find the total of the marks of students who passed (marks ≥ 40).

marks = [35, 78, 92, 40, 15, 66]

2.1 Imperative style — step by step

total = 0
for m in marks:
    if m >= 40:
        total = total + m
print(total)          # 276

You tell the computer exactly how to do it: create a variable, loop, check, update.

2.2 Procedural programming — organised into functions

Procedural programming is imperative code organised into procedures (functions). The program is a sequence of function calls that work on data.

def is_pass(mark):
    return mark >= 40

def total_of_passed(marks):
    total = 0
    for m in marks:
        if is_pass(m):
            total += m
    return total

print(total_of_passed(marks))   # 276
ProsCons
Simple and easy to understandData and functions are separate — data can be changed from anywhere
Great for small scripts and automationHarder to manage in very large programs
Functions are reusableLess natural for modelling real-world things

2.3 Object-Oriented Programming (OOP) — data + behaviour together

OOP organises code into objects. Each object bundles data (attributes) and behaviour (methods). Objects are created from classes (blueprints).

class Classroom:
    def __init__(self, marks):
        self.marks = marks            # data

    def passed(self):                 # behaviour
        return [m for m in self.marks if m >= 40]

    def total_of_passed(self):
        return sum(self.passed())

class_10a = Classroom(marks)
print(class_10a.total_of_passed())    # 276
print(class_10a.passed())             # [78, 92, 40, 66]

The four pillars of OOP (you will learn each in this series):

PillarMeaningReal-world analogy
EncapsulationBundle data and methods; hide internal detailsA capsule hides the medicine inside
AbstractionShow only what is necessaryYou drive a car without knowing the engine internals
InheritanceA class reuses features of another classA child inherits features from parents
PolymorphismSame interface, different behaviourA person is a student in class, a customer in a shop

2.4 Functional programming — pure functions and no changing state

Functional programming treats computation as evaluating functions. Its key ideas:

  • Pure functions – the same input always gives the same output, with no side effects.
  • Immutability – don't change data; create new data.
  • Functions are first-class – pass them as arguments, return them from functions.
  • Higher-order functions – map, filter, reduce.
from functools import reduce

passed = filter(lambda m: m >= 40, marks)
total = reduce(lambda a, b: a + b, passed)
print(total)                                   # 276

# The most Pythonic (declarative) way
print(sum(m for m in marks if m >= 40))        # 276

Pure vs impure functions

# Pure: depends only on its input, changes nothing outside
def add(a, b):
    return a + b

# Impure: changes something outside (side effect)
total = 0
def add_to_total(x):
    global total
    total += x

3. Side-by-Side Comparison

FeatureProceduralObject-OrientedFunctional
Main unitFunctions / proceduresObjects and classesPure functions
Data and functionsSeparateBundled togetherData passed through functions
StateChanges freelyKept inside objectsAvoided (immutable)
Best forScripts, automation, simple tasksLarge apps, games, GUIs, web frameworksData transformation, parallel processing
Python toolsdef, loopsclass, objects, inheritancelambda, map, filter, reduce, comprehensions
Example in real lifeA recipe followed step by stepA car factory making many cars from one designA juicer: fruit in, juice out, nothing else changes

4. Python is a Multi-Paradigm Language

Python does not force one style. In real projects you mix them:

  • Django (which powers codingbattle.in) uses OOP for models and views.
  • Data science code with Pandas often looks declarative / functional.
  • Automation scripts are usually procedural.
LanguageMain paradigms
CProcedural
JavaObject-oriented (with some functional features)
HaskellPurely functional
SQLDeclarative
PythonProcedural + Object-oriented + Functional

Did you know?: In Python, everything is an object — even numbers, strings and functions. (5).bit_length() calls a method on the integer 5! So even "procedural" Python is built on top of OOP.

5. Which Paradigm Should You Use?

  • Small script or a quick task? → Procedural.
  • Modelling real-world entities (students, accounts, products) or building a large app? → OOP.
  • Transforming collections of data? → Functional tools and comprehensions.

Good programmers choose the right tool for each part of the problem.

6. Mini Project: Shopping Cart in Three Styles

prices = {"pen": 10, "notebook": 45, "bag": 799}
cart = ["pen", "pen", "notebook", "bag"]

# Procedural
def cart_total(cart, prices):
    total = 0
    for item in cart:
        total += prices[item]
    return total
print(cart_total(cart, prices))                  # 864

# Object-oriented
class ShoppingCart:
    def __init__(self, prices):
        self.prices = prices
        self.items = []

    def add(self, item):
        self.items.append(item)

    def total(self):
        return sum(self.prices[i] for i in self.items)

c = ShoppingCart(prices)
for item in cart:
    c.add(item)
print(c.total())                                 # 864

# Functional
print(sum(map(lambda item: prices[item], cart)))  # 864

Quiz: Predict the Output

Don't run the code yet! Write your answers in a notebook, then check the Answers section at the end of this blog.

Q1. What is the output?

from functools import reduce
print(reduce(lambda a, b: a + b, [1, 2, 3, 4]))

Q2. What is the output?

print(list(filter(lambda x: x > 2, [1, 2, 3, 4])))

Q3. What is the output?

print(list(map(lambda x: x * 10, [1, 2, 3])))

Q4. What is the output?

print(sum(x * x for x in [1, 2, 3, 4] if x % 2 == 0))

Q5. Is this function pure?

count = 0
def increase():
    global count
    count += 1

Q6. Which paradigm bundles data and methods together?

Q7. Imperative or declarative?

total = sum(numbers)

Q8. Name the four pillars of OOP.

Practice Challenges

Solve these from easy to tricky:

  1. Write a procedural program with functions to find the average of a list of numbers.
  2. Rewrite challenge 1 using a class Statistics with a method average().
  3. Using map, convert ["1", "2", "3"] into [1, 2, 3].
  4. Using filter, keep only the words longer than 4 letters from a list of words.
  5. Write a pure function discount(price, percent) that returns the discounted price.
  6. Identify the paradigm(s) used in a Python program you have written before.

Solutions for challenges 2, 3 and 4 are in the Answers section.

Self-Check: Can You Do This?

Tick off each skill once you are confident:

Frequently Asked Interview Questions

Q: What is a programming paradigm?
A style or approach to structuring and writing programs, such as procedural, object-oriented or functional.

Q: Is Python object-oriented or procedural?
Both — and functional too. Python is a multi-paradigm language, and internally everything in Python is an object.

Q: Difference between procedural and object-oriented programming?
Procedural programming separates data from functions and runs a sequence of procedures. OOP bundles data and behaviour into objects and uses encapsulation, inheritance and polymorphism.

Q: What is a higher-order function?
A function that takes another function as an argument or returns a function, e.g. map, filter, sorted(key=...), decorators.

Q: What are the advantages of OOP?
Code reusability (inheritance), data protection (encapsulation), flexibility (polymorphism), and easier maintenance of large codebases.

Summary

  • A paradigm is a style of writing code.
  • Imperative = how (step by step); declarative = what (the result).
  • Procedural: functions operating on separate data.
  • OOP: classes and objects bundling data and behaviour; four pillars — encapsulation, abstraction, inheritance, polymorphism.
  • Functional: pure functions, immutability, map/filter/reduce.
  • Python is multi-paradigm — mix styles wisely.

Quick Revision Cheat Sheet

ConceptSyntax / Key point
Proceduraldef total(marks): ...
OOPclass Student: ...
Functionalmap(f, data), filter(f, data)
Reducefrom functools import reduce
Lambdalambda x: x * 2
Declarative sumsum(x for x in data if x > 0)

Answers

Quiz answers

  1. 10
  2. [3, 4]
  3. [10, 20, 30]
  4. 20 – 2² + 4² = 4 + 16.
  5. No – it changes a global variable (a side effect).
  6. Object-Oriented Programming.
  7. Declarative – you say what you want, not how to loop.
  8. Encapsulation, Abstraction, Inheritance, Polymorphism.

Challenge solutions

# Challenge 2
class Statistics:
    def __init__(self, numbers):
        self.numbers = numbers

    def average(self):
        return sum(self.numbers) / len(self.numbers)

s = Statistics([10, 20, 30, 40])
print(s.average())                                  # 25.0

# Challenge 3
print(list(map(int, ["1", "2", "3"])))              # [1, 2, 3]

# Challenge 4
words = ["code", "python", "battle", "ai", "learn"]
print(list(filter(lambda w: len(w) > 4, words)))    # ['python', 'battle', 'learn']

Conclusion

You now understand the major programming paradigms and how Python supports all of them. Next, dive deep into the heart of OOP: Classes and Objects in Python.

If this blog helped you, watch the full video above, subscribe to the channel and share it with a friend who is learning Python. Happy coding!