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.
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| Imperative | Declarative |
|---|---|
| Describes how to do something, step by step | Describes what result you want |
| You manage loops, variables and state changes | The language/library handles the steps |
| Examples: procedural, object-oriented | Examples: functional, SQL, HTML |
| Python: a for loop with a running total | Python: sum(numbers) or a list comprehension |
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]total = 0
for m in marks:
if m >= 40:
total = total + m
print(total) # 276You tell the computer exactly how to do it: create a variable, loop, check, update.
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| Pros | Cons |
|---|---|
| Simple and easy to understand | Data and functions are separate — data can be changed from anywhere |
| Great for small scripts and automation | Harder to manage in very large programs |
| Functions are reusable | Less natural for modelling real-world things |
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):
| Pillar | Meaning | Real-world analogy |
|---|---|---|
| Encapsulation | Bundle data and methods; hide internal details | A capsule hides the medicine inside |
| Abstraction | Show only what is necessary | You drive a car without knowing the engine internals |
| Inheritance | A class reuses features of another class | A child inherits features from parents |
| Polymorphism | Same interface, different behaviour | A person is a student in class, a customer in a shop |
Functional programming treats computation as evaluating functions. Its key ideas:
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: 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| Feature | Procedural | Object-Oriented | Functional |
|---|---|---|---|
| Main unit | Functions / procedures | Objects and classes | Pure functions |
| Data and functions | Separate | Bundled together | Data passed through functions |
| State | Changes freely | Kept inside objects | Avoided (immutable) |
| Best for | Scripts, automation, simple tasks | Large apps, games, GUIs, web frameworks | Data transformation, parallel processing |
| Python tools | def, loops | class, objects, inheritance | lambda, map, filter, reduce, comprehensions |
| Example in real life | A recipe followed step by step | A car factory making many cars from one design | A juicer: fruit in, juice out, nothing else changes |
Python does not force one style. In real projects you mix them:
| Language | Main paradigms |
|---|---|
| C | Procedural |
| Java | Object-oriented (with some functional features) |
| Haskell | Purely functional |
| SQL | Declarative |
| Python | Procedural + 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.
Good programmers choose the right tool for each part of the problem.
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))) # 864Don'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 += 1Q6. Which paradigm bundles data and methods together?
Q7. Imperative or declarative?
total = sum(numbers)Q8. Name the four pillars of OOP.
Solve these from easy to tricky:
Solutions for challenges 2, 3 and 4 are in the Answers section.
Tick off each skill once you are confident:
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.
| Concept | Syntax / Key point |
|---|---|
| Procedural | def total(marks): ... |
| OOP | class Student: ... |
| Functional | map(f, data), filter(f, data) |
| Reduce | from functools import reduce |
| Lambda | lambda x: x * 2 |
| Declarative sum | sum(x for x in data if x > 0) |
# 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']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!