Skip to main content
Functional programming is a programming paradigm that solves problems by applying and composing functions. It combines the power of higher-order functions with a declarative programming style to write clean, reusable, and expressive code. Python supports multiple programming paradigms, including:
  • Procedural Programming
  • Object-Oriented Programming (OOP)
  • Functional Programming
Although Python is not a purely functional programming language, it provides several features that make writing functional-style programs simple and effective.

Learning Objectives

After completing this lesson, you will be able to:
  • Explain the functional programming paradigm.
  • Understand the role of higher-order functions.
  • Create anonymous functions using lambda.
  • Use built-in higher-order functions such as map(), filter(), and reduce().
  • Write programs using a functional programming style.

What is Functional Programming?

Functional programming is a style of programming where functions are the primary building blocks of a program. It is based on two key ideas:
  • Higher-order functions, which allow functions to be passed and returned like any other object.
  • Declarative programming, where we describe what transformation should happen rather than how to perform it step by step.
Note: A declarative programming style alone does not make a language a functional programming language. For example, SQL is declarative because we specify what data we want rather than how to retrieve it. Functional programming combines a declarative style with higher-order functions and function composition.

Procedural Approach

The following program explicitly performs each step.
Output

Functional Approach

The same problem can be expressed by composing functions.
Output
The procedural approach describes how to perform each step, whereas the functional approach describes what transformations should be applied to the data.

Characteristics of Functional Programming

  • Functions are the primary building blocks.
  • Uses higher-order functions extensively.
  • Encourages function composition.
  • Follows a declarative programming style.
  • Focuses on transforming data rather than modifying it.
  • Produces modular and reusable code.

Practice

Exercise 1

Which two concepts form the foundation of functional programming?
Functional programming combines:
  • Higher-order functions
  • Declarative programming

Exercise 2

What is the main difference between procedural programming and functional programming?
  • Procedural programming focuses on how to perform a task step by step.
  • Functional programming focuses on what transformations should be applied by composing functions.

Exercise 3

Is SQL a functional programming language? Why?
No. SQL follows a declarative programming style, but it is not a functional programming language because it does not use higher-order functions and function composition as its primary programming model.
Python supports functional programming through features such as lambda expressions and built-in higher-order functions like map(), filter(), and reduce(). We’ll begin by exploring lambda functions in the next section.

Lambda Functions

A lambda function is a small anonymous function created using the lambda keyword. It is commonly used when a function is required for a short period of time and does not need a name. Lambda functions are frequently used with higher-order functions such as map(), filter(), and sorted().

Syntax

A lambda function:
  • Can have one or more parameters.
  • Contains only a single expression.
  • Automatically returns the result of the expression.
  • Does not require the return keyword.

Example

A normal function:
The same function using lambda:
Output

Lambda with Multiple Parameters

Output

Lambda with sorted()

Lambda functions are commonly used to specify a custom sorting rule.
Output
The key function returns the second element (marks) of each tuple, so the list is sorted by marks.

When to Use Lambda Functions

Use lambda functions when:
  • The function is simple.
  • It is used only once.
  • A higher-order function expects another function as an argument.
Avoid lambda functions when:
  • The logic is complex.
  • Multiple statements are required.
  • The function will be reused in multiple places.

Practice

Exercise 1

Convert the following function into a lambda function.

Exercise 2

Predict the output.
The lambda function multiplies the two arguments and returns the result.

Exercise 3

Predict the output.
The key function returns the length of each string, so the list is sorted in ascending order of string length.

Exercise 4

When should you prefer a normal function over a lambda function?
Use a normal function when:
  • The logic is complex.
  • Multiple statements are required.
  • The function will be reused in multiple places.
Use a lambda function when the function is short, simple, and used only once.
Lambda functions become especially useful when working with built-in higher-order functions such as map(), filter(), and reduce(), which we’ll explore next.

Built-in Higher-Order Functions

Python provides several built-in higher-order functions that simplify common data processing tasks. These functions are widely used in functional programming to transform, filter, and combine data. The most commonly used built-in higher-order functions are:
  • map() – Applies a function to every element.
  • filter() – Selects elements that satisfy a condition.
  • reduce() – Combines all elements into a single value.
  • sorted() – Sorts elements using a custom key function.
  • any() – Returns True if at least one element satisfies a condition.
  • all() – Returns True only if all elements satisfy a condition.
We’ll explore each of these functions in the following sections.

The map() Function

The map() function applies a function to every element of an iterable and returns a map object, which is an iterator.
Since map() returns an iterator, it is commonly converted into a list using the list() function.

Using a Normal Function

Output

Using a Lambda Function

Output

Mapping Multiple Iterables

Output

Practice

Exercise 1

Predict the output.
The lambda function adds 1 to each element, and map() applies it to every element in the list.

Exercise 2

When should you use map()?
Use map() when you want to apply the same transformation to every element of an iterable.Some common use cases include:
  • Squaring numbers
  • Converting strings to uppercase
  • Calculating percentages
  • Formatting data
While map() transforms every element, sometimes we need to select only the elements that satisfy a condition. For this purpose, Python provides the filter() function.

The filter() Function

The filter() function selects only those elements that satisfy a given condition and returns a filter object, which is an iterator.
The function passed to filter() should return either True or False. Since filter() returns an iterator, it is commonly converted into a list using the list() function.

Using a Normal Function

Output

Using a Lambda Function

Output

Another Example

Filter students who scored at least 75 marks.
Output

Procedural vs Functional

Procedural Approach
Functional Approach
The procedural approach manually checks every element, whereas the functional approach simply specifies the filtering condition.

Practice

Exercise 1

Predict the output.
The lambda function returns True only for values greater than 15, so filter() selects only those elements.

Exercise 2

Predict the output.
The lambda function keeps only the strings whose length is greater than 2.

Exercise 3

When should you use filter()?
Use filter() when you want to select only those elements that satisfy a condition.Some common use cases include:
  • Selecting even or odd numbers
  • Filtering students who passed an exam
  • Removing empty strings
  • Selecting records based on a condition

The reduce() Function

The reduce() function repeatedly applies a function to the elements of an iterable and combines them into a single value. Unlike map() and filter(), reduce() is available in the functools module.

Using a Normal Function

Output

Using a Lambda Function

Output

Another Example

Find the product of all numbers.
Output

How reduce() Works

The final result is returned.

Practice

Exercise 1

Predict the output.
reduce() repeatedly applies the lambda function to combine all elements into a single value.

Exercise 2

Predict the output.
The multiplication is performed as:

Exercise 3

When should you use reduce()?
Use reduce() when you need to combine all elements of an iterable into a single value, such as calculating the:
  • Sum
  • Product
  • Maximum
  • Minimum
We have now seen how to transform data using map(), filter data using filter(), and combine data using reduce(). Next, we’ll explore other useful built-in higher-order functions such as sorted(), any(), and all().

Other Built-in Higher-Order Functions

Besides map(), filter(), and reduce(), Python provides several other higher-order functions that are frequently used in functional programming.

The sorted() Function

The sorted() function returns a new sorted list. Using the key parameter, we can specify a function that determines how the elements should be sorted.

Sorting Numbers

Output

Sorting by Length

Output

Sorting Student Records

Output

Practice

Exercise 1

Predict the output.
The reverse=True argument sorts the elements in descending order.

Exercise 2

When should you use the key parameter with the sorted() function?
Use the key parameter when the sorting order should be based on a custom property of each element rather than the element itself.For example:
  • Sort strings by their length.
  • Sort students by their marks.
  • Sort dictionaries by a specific key.

The any() Function

The any() function returns True if at least one element in an iterable evaluates to True.

Example

Output

Using any() with a Generator Expression

Output
The expression checks whether any number is odd.

Practice

Exercise 1

Predict the output.
All elements evaluate to False, so any() returns False.

The all() Function

The all() function returns True only if every element in an iterable evaluates to True.

Example

Output

Another Example

Output
Since one element does not satisfy the condition, all() returns False.

Practice

Exercise 1

Predict the output.
Since one element is False, the result is False.

Exercise 2

What is the difference between any() and all()?
  • any() returns True if at least one element satisfies the condition.
  • all() returns True only if every element satisfies the condition.

Summary of Built-in Higher-Order Functions

These higher-order functions can be combined to build concise and expressive programs. In the next section, we’ll see how to write programs in a functional programming style by composing these functions.

Writing Programs in Functional Style

Functional programming encourages solving problems by composing small functions. Instead of writing step-by-step instructions, we describe the sequence of transformations that should be applied to the data. A common functional programming workflow is:

Example 1: Sum of Squares of Even Numbers

Procedural Approach
Output
Functional Approach
Output
The data flows through three stages:

Example 2: Student Grades

Calculate the average marks of students who scored at least 75 marks.
Output
The program first filters the required students and then transforms the data before calculating the result.

When to Use Functional Programming

Functional programming works well when:
  • Transforming collections of data.
  • Filtering data based on conditions.
  • Performing calculations on data.
  • Building data processing pipelines.
  • Writing reusable functions.
Avoid using functional programming when:
  • The logic becomes difficult to read.
  • Multiple nested function calls reduce clarity.
  • A simple loop is easier to understand.
Readability is more important than writing everything in a functional style.

Real-World Applications

Functional programming concepts are widely used in Python libraries and frameworks, including:
  • Data processing and analysis
  • Machine learning
  • ETL pipelines
  • Web APIs
  • Asynchronous programming
  • Background task processing
  • Event-driven systems
  • Stream processing
Frameworks such as FastAPI, Pandas, PySpark, Apache Beam, and Dask make extensive use of higher-order functions and functional programming concepts.

Key Takeaways

  • Functional programming combines higher-order functions with a declarative programming style.
  • Python supports functional programming through lambda expressions and built-in higher-order functions.
  • map() transforms data.
  • filter() selects data.
  • reduce() combines data into a single value.
  • sorted() performs custom sorting using a key function.
  • any() and all() simplify condition checking.
  • Functional programming emphasizes composing small functions to build expressive and reusable programs.

Check Your Understanding

Question 1 What are the two main ideas behind functional programming?
Functional programming combines:
  • Higher-order functions
  • Declarative programming
Question 2 What is the purpose of a lambda function?
A lambda function provides a concise way to create a small anonymous function, typically used with higher-order functions.
Question 3 When should you use map()?
Use map() when the same transformation needs to be applied to every element of an iterable.
Question 4 When should you use filter()?
Use filter() when selecting elements that satisfy a given condition.
Question 5 When should you use reduce()?
Use reduce() when combining all elements of an iterable into a single value.
Question 6 What is the difference between any() and all()?
  • any() returns True if at least one element satisfies the condition.
  • all() returns True only if every element satisfies the condition.
Question 7 Why is SQL not considered a functional programming language?
SQL follows a declarative programming style, but it is not a functional programming language because it does not use higher-order functions and function composition as its primary programming model.
Question 8 What is the main advantage of functional programming?
Functional programming encourages writing modular, reusable, and expressive programs by composing small functions.

Practice & Exercises

To reinforce what you’ve learned in this section (Lambdas, Map, Filter, Reduce, Custom Sorting, any(), and all()), practice with these interactive notebooks:

Follow-Along Practice

Practice writing lambda functions, mapping and filtering collections, reducing lists to single values, using custom sorted keys, and checking conditions with any and all.💻 VS Code | 🚀 Colab | 📥 Download

Practice Exercises

Test your skills with exercises on custom lambda sorting of dictionary products, mapping string lengths, filtering vowel-starting words, reducing list elements to products, and validating group scores.💻 VS Code | 🚀 Colab | 📥 Download