Table of contents
1.
Introduction
2.
What is functional programming in Python?
3.
Concepts of Functional Programming
3.1.
Pure Functions 
3.1.1.
Example
3.2.
Python
3.3.
Immutability
3.4.
Higher-Order Functions
3.4.1.
Map()
3.4.2.
Example 
3.5.
Python
3.5.1.
filter() 
3.5.2.
Example 
3.6.
Python
3.7.
Recursion
3.7.1.
Example
3.8.
Python
3.9.
Lambda Expressions
3.9.1.
Example
3.10.
Python
4.
Difference between Functional Programming and Object-Oriented Programming
5.
Frequently Ask Questions
5.1.
Why is Python a functional programming language?
5.2.
What is functional programming best used for?
5.3.
Is Python good for functional programming?
5.4.
Is Python more OOP or functional?
5.5.
What are the 4 types of functions in Python?
5.6.
What is an example of functional programming?
6.
Conclusion
Last Updated: Aug 18, 2025
Easy

Functional Programming in Python

Author Muskan Gupta
1 upvote

Introduction

Functional programming in Python is a significant programming paradigm that is strongly tied to the mathematical underpinnings of computer science. We define it as a language that transforms data using functions.

Python isn't functional programming in the Python language, although it does use some of its features in conjunction with other programming paradigms. Python makes it simple to write code in various styles, each of which may offer the optimal answer for the problem at hand.

Functional Programming in Python

What is functional programming in Python?

Functional programming in Python is a programming paradigm where computation is treated as the evaluation of mathematical functions. It emphasizes immutability, first-class functions, and avoiding side effects. In Python, functional programming allows you to use higher-order functions, work with functions as arguments, and leverage built-in functions like map(), filter(), and reduce() to process data in a declarative way.

Concepts of Functional Programming

  • First-Class Functions: Functions are treated as first-class citizens, meaning they can be assigned to variables, passed as arguments, or returned from other functions.
  • Immutability: Data cannot be changed once created. Instead of modifying data, new data structures are created, ensuring side effects are minimized.
  • Pure Functions: Functions that always produce the same output for the same input and do not alter any external state or variables.
  • Higher-Order Functions: Functions that take other functions as arguments or return them, enabling more flexible and abstract code.
  • Lambda Functions: Anonymous, single-expression functions defined using the lambda keyword, often used for short operations.
  • Recursion: Functional programming often uses recursion instead of loops, where a function calls itself to solve a problem.

Pure Functions 

If you'd like functions to be pure, do not change the input value or any data outside the function's scope.

This makes testing the function we build a lot easier. We know we'll get the same outcome every time we execute the function with the same input because it doesn't modify the state of any variables.

Example

  • Python

Python

def multiply_2_pure(numbers):
new_numbers = []
for n in numbers:
new_numbers.append(n * 2)
return new_numbers

original_numbers = [1, 3, 5, 10]
changed_numbers = multiply_2_pure(original_numbers)
print(original_numbers) # [1, 3, 5, 10]
print(changed_numbers) # [2, 6, 10, 20]
You can also try this code with Online Python Compiler
Run Code

This example for a pure function to multiply numbers by 2

The original list of numbers is unchanged, and we don't reference any other variables outside of the function, so it is pure.

Immutability

Have you ever had a bug where you wondered how a variable you set to 25 became none? If the variable had been immutable, the error would have been thrown when modified, not when the changed value had already impacted the software; the bug's primary cause might have been found earlier.

Immutable data types are available in Python, with the Tuple being one of the most prominent. Consider the Tuple in comparison to a changeable List:

mutable_collection = ['Tim', 10, [4, 5]]
immutable_collection = ('Tim', 10, [4, 5])

 

The following are the basic forms of data that can be read:

print(mutable_collection[2]) # [4, 5]
print(immutable_collection[2]) # [4, 5]

 

Let's enhance the second value of mutable_collection from 10 to 15.

mutable_collection[1] = 15

 

With the tuple, this fails.

immutable_collection[1] = 15
TypeError: The issue you'll see is that the 'tuple' object doesn't permit item assignment.

 

A Tuple could appear to be a changeable object in an unusual case. For example, if we wish to alter the immutable collection's list from [4, 5] to [4, 5, 6], we can do it as follows:

immutable_collection[2].append(6)
print(immutable_collection[2]) # [4, 5, 6]

 

Because a list is a changeable object, this works. Let's change the list back to [4, 5].

immutable_collection[2] = [4, 5]
# This throws a standard error:
# TypeError: 'Item assignment is not supported by the 'tuple' object.

 

It fails just as we predicted. While the contents of a changeable object in a tuple can be changed, the reference to the mutable object in memory cannot be changed.

Higher-Order Functions

Python has implemented some commonly used Higher-Order Functions to make processing iterable objects like lists and iterators considerably easier. These functions produce a space-efficient iterator. The following are some of the built-in higher-order functions:

Map()

We can use the map function to apply a function to each element of an iterable object. If we had a list of names and wanted to add a greeting to the Strings, we could do something like this:

Example 

  • Python

Python

names = ['Muskan', 'Jason', 'Yusef', 'Rajesh']
greeted_names = map(lambda x: 'Hello ' + x, names)
# This prints something similar to: <map object at 0x10ed93cc0>
print(greeted_names)
# Recall, that map returns an iterator 
# In a for loop, we may print all of the names.
for name in greeted_names:
print(name)
You can also try this code with Online Python Compiler
Run Code

Output

<map object at 0x000001C368126F50>
Hello Muskan
Hello Jason
Hello Yusef
Hello Rajesh

filter() 

The filter() method filters a sequence using a function that checks if each element in the sequence is true or not.

Example 

  • Python

Python

def fun(variable):  
letters = ['a', 'e', 'i', 'o', 'u'] 
if (variable in letters): 
return True
else: 
return False
sequence = ['g', 'e', 'e', 'j', 'k', 's', 'p', 'r'] 
filtered = filter(fun, sequence) 
print('The filtered letters are:') 
for s in filtered: 
print(s)
You can also try this code with Online Python Compiler
Run Code

Output

The filtered letters are:
e
e

 

This Python program demonstrates the working of the filter.

Recursion

There is no such thing as a for loop or a while loop in functional programming in Python. Recursion is employed instead. A procedure in which a function calls itself directly or indirectly is known as recursion. The recursive program provides the answer to the base case, and the solution to the larger problem is represented in terms of lesser concerns. A question may arise: what is the base case? The base case can be considered a condition that tells the compiler or interpreter to exit from the function.

Example

Consider a program that calculates the sum of all list elements without utilizing any loops.

  • Python

Python

def Sum(L, i, n, count):
# Base case
if n <= i:
return count
count += L[i]
# Going into the recursion
count = Sum(L, i + 1, n, count)
return count
# Driver's code
L = [1, 2, 3, 4, 5]
count = 0
n = len(L)
print(Sum(L, 0, n, count))
You can also try this code with Online Python Compiler
Run Code

Output

15

 

This Python program demonstrates recursion. It has a recursive function to find some of a list.

Lambda Expressions

An anonymous function is a lambda expression. In Python, we use the def keyword to define a function and give it a name. We can define a function much faster with lambda expressions.

Example

Let's make a Higher Order Function called hof product that returns a function that multiplies a number by a given value:

  • Python

Python

def hof_product(multiplier):
return lambda x: x * multiplier
mult6 = hof_product(6)
print(mult6(6)) # 36
You can also try this code with Online Python Compiler
Run Code

Output

36

The function arguments follow the keyword lambda in a lambda expression. The code provided by the lambda is placed after the colon; this ability to build functions "on the go" is quite useful when working with Higher-Order Functions.

You can practice by yourself with the help of online python compiler for better understanding.

Difference between Functional Programming and Object-Oriented Programming

ParametersFunctional ProgrammingObject-Oriented Programming
ParadigmBased on mathematical functions and immutability.Based on objects, classes, and the interaction between them.
Core FocusFunctions as first-class citizens, pure functions, and immutability.Objects encapsulate data and methods; focuses on state and behavior.
State ManagementAvoids mutable state; emphasizes immutability.State is stored in objects and can be modified.
Data HandlingData is transformed through functions.Data is encapsulated in objects and manipulated through methods.
Side EffectsMinimizes or avoids side effects.Side effects are common (e.g., modifying object state).
Code ReusabilityAchieved through higher-order functions and function composition.Achieved through inheritance and polymorphism.
ConcurrencyEasier to achieve due to immutability and no side effects.More challenging due to mutable state and object interactions.
Example LanguagesPython, Haskell, Scala, ErlangPython, Java, C++, Ruby

Frequently Ask Questions

Why is Python a functional programming language?

Python authorizes us to code in a functional, declarative style. It keeps many standard functional characteristics like lambda expressions and the map and filter functions. Yet, the Python community does not believe the use of functional Programming techniques is the best approach at all times.

What is functional programming best used for?

Functional programming is best for tasks requiring immutability, parallel processing, concise code, and mathematical computations, where side effects need to be minimized.

Is Python good for functional programming?

Yes, Python supports functional programming with features like first-class functions, higher-order functions, and built-in functions like map(), filter(), and reduce().

Is Python more OOP or functional?

Python is primarily an object-oriented language but also supports functional programming features, making it a multi-paradigm language with flexibility in both approaches.

What are the 4 types of functions in Python?

The four types of functions in Python are built-in functions, user-defined functions, lambda (anonymous) functions, and recursive functions.

What is an example of functional programming?

An example of functional programming is using the map() function to transform a list by applying a function to each element without modifying the original list.

Conclusion

In this article, we have extensively discussed Functional Programming in Python, immutability, and lambda expression. The article explains the Recursion and High-Order functions with examples.

Recommended Readings:

Live masterclass