IT Career Guide
Search

Is Python Functional Programming? Explained with Examples

Lines of Python code on a monitor illustrating functional programming

No, Python is not a functional programming language, but it is a multi-paradigm language that supports a functional style through first-class functions, lambda, map, filter, comprehensions, generators and the functools module. Pure functional languages such as Haskell are built around immutable data and functions without side effects. Python lets you write that way when it helps, but it also supports procedural and object-oriented code, and much everyday Python mixes all three. This guide explains what functional programming means, which functional features Python has, where it falls short and when to use a functional style.

Key takeaways

  • Python is multi-paradigm: it supports procedural, object-oriented and functional styles in the same program.
  • Functions in Python are first-class objects, so you can pass them as arguments, return them and store them in variables.
  • Python offers lambda, map, filter, functools.reduce, comprehensions, generators and closures for functional-style code.
  • Python is not purely functional because most of its data types are mutable and it lacks tail-call optimisation.
  • Use a functional style for data transformations and small pure helpers, and keep code readable rather than forcing every line into one paradigm.

What is functional programming?

Functional programming is a way of writing software by combining functions. Its main ideas are:

  • Pure functions: the same input always gives the same output, with no side effects such as changing a global variable.
  • Immutability: data is not changed after creation; you create new data instead.
  • First-class and higher-order functions: functions can be passed to and returned from other functions.
  • Declarative style: you describe what to compute rather than step-by-step how.
  • Recursion over loops: repetition is often expressed with recursion.

Is Python functional programming? Why the answer is no, but partly

Python’s creator designed it as a practical, readable general-purpose language rather than a functional one. The language borrowed several functional ideas, which is why the question comes up so often. You can write functional-style Python, but the language does not enforce or optimise for it.

Functional features Python does have

First-class functions

def shout(text):
    return text.upper()

def apply(func, value):
    return func(value)

print(apply(shout, 'hello'))   # HELLO

Lambda functions

square = lambda x: x * x
students = [('Asha', 82), ('Rahul', 91), ('Priya', 76)]
print(sorted(students, key=lambda s: s[1], reverse=True))

map, filter and reduce

from functools import reduce

nums = [1, 2, 3, 4, 5, 6]
squares = list(map(lambda n: n * n, nums))
evens = list(filter(lambda n: n % 2 == 0, nums))
total = reduce(lambda a, b: a + b, nums)
print(squares, evens, total)

reduce moved to the functools module in Python 3, a sign that the language prefers explicit loops or built-ins such as sum for many cases.

Comprehensions

squares = [n * n for n in nums]
evens = [n for n in nums if n % 2 == 0]
lookup = {name: score for name, score in students}

Comprehensions came from functional languages and are usually considered more Pythonic than map and filter with lambda.

Generators and lazy evaluation

def read_large_file(path):
    with open(path) as f:
        for line in f:
            yield line.strip()

errors = (line for line in read_large_file('app.log') if 'ERROR' in line)

Closures and decorators

def make_multiplier(n):
    def multiply(x):
        return x * n
    return multiply

double = make_multiplier(2)
print(double(10))   # 20

Decorators build on closures to wrap functions with extra behaviour such as logging or caching.

The functools and itertools modules

functools provides partial, reduce, lru_cache and cache. itertools provides tools such as chain, groupby, accumulate and combinations, many inspired by functional languages.

Why Python is not a pure functional language

Functional ideaPure functional languagePython
ImmutabilityDefault for all dataLists, dicts and sets are mutable; tuples and frozensets are immutable
Side effectsControlled or isolatedAllowed anywhere
RecursionOptimised, often with tail callsNo tail-call optimisation and a default recursion limit
LambdaFull expressions and functionsSingle expression only
Main styleFunctionalMulti-paradigm, often object-oriented

Because of these limits, deeply recursive or strictly immutable designs can feel awkward or slow in Python.

Is Python object-oriented or functional?

Both, and procedural too. In Python, everything is an object, including functions, integers and modules, so the language is object-oriented at its core. At the same time, you can write a whole script with plain functions and no classes. Choosing the right style for each part of a program is a key Python skill. Our guide on why Python is an interpreted language covers another common question about how Python works.

Same task in three styles

Seeing one small problem written three ways makes the difference clear. The task: from a list of orders, find the total value of paid orders above 500 rupees.

Procedural style

orders = [
    {'id': 1, 'amount': 450, 'paid': True},
    {'id': 2, 'amount': 1200, 'paid': True},
    {'id': 3, 'amount': 800, 'paid': False},
    {'id': 4, 'amount': 650, 'paid': True},
]

total = 0
for order in orders:
    if order['paid'] and order['amount'] > 500:
        total += order['amount']
print(total)   # 1850

Functional style

def is_big_paid(order):
    return order['paid'] and order['amount'] > 500

total = sum(o['amount'] for o in orders if is_big_paid(o))
print(total)   # 1850

Object-oriented style

class Order:
    def __init__(self, id, amount, paid):
        self.id, self.amount, self.paid = id, amount, paid

    def is_big_paid(self):
        return self.paid and self.amount > 500

objs = [Order(**o) for o in orders]
print(sum(o.amount for o in objs if o.is_big_paid()))   # 1850

All three are valid Python. The functional version is short and its helper is a pure function that is easy to test, which is why many Python developers reach for this style in data-processing code. The object-oriented version pays off when orders carry more behaviour, such as discounts and status changes.

When to use a functional style in Python

  • Transforming lists or streams of data with comprehensions and generators.
  • Writing small pure helper functions that are easy to test.
  • Passing behaviour as a parameter, such as a key function for sorting.
  • Caching results of pure functions with functools.cache.
  • Data pipelines in pandas or PySpark, which encourage chaining transformations.

When not to force it

  • Long chains of nested map, filter and lambda that are harder to read than a simple loop.
  • Deep recursion that may hit the recursion limit.
  • Programs with lots of shared state, where classes model the problem better.

Frequently asked questions

Is Python a functional or procedural language?

Python is multi-paradigm. It supports procedural, object-oriented and functional programming, and you can mix them in one program.

Does Python support functional programming?

Yes. Python supports a functional style through first-class functions, lambda, map, filter, comprehensions, generators, closures and the functools and itertools modules.

Which languages are purely functional?

Haskell is the best-known purely functional language. Languages such as Scala, F#, Clojure and Elixir are strongly functional while allowing some other styles.

Is this asked in Python interviews?

Yes. Interviewers often ask about lambda, map versus list comprehensions, decorators, generators and whether Python is functional or object-oriented.

Revise more with our Python interview questions or start from the basics with learn Python for beginners. Browse more in our Python category.

Other articles

Radium4 — IT training blog

Practical, plain-English guides for DevOps, cloud, data, Python, testing and security learners.