Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Functional programming is a programming paradigm that emphasizes the use of functions to solve problems, rather than relying on mutable data and imperative statements. In Python, three built-in functions - map()
, filter()
, and reduce()
- support functional programming techniques.
map()
function: The map()
function applies a function to each element of an iterable and returns a new iterable with the results. Here's an example:numbers = [1, 2, 3, 4, 5] squares = map(lambda x: x**2, numbers) print(list(squares)) # Output: [1, 4, 9, 16, 25]
In this example, we use map()
to apply the lambda function x**2
to each element of the list numbers
. The result is a new iterable squares
that contains the squares of the numbers in the original list.
filter()
function: The filter()
function applies a function to each element of an iterable and returns a new iterable with the elements for which the function returns True
. Here's an example:numbers = [1, 2, 3, 4, 5] even_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(even_numbers)) # Output: [2, 4]
In this example, we use filter()
to apply the lambda function x % 2 == 0
to each element of the list numbers
. The result is a new iterable even_numbers
that contains only the even numbers from the original list.
reduce()
function: The reduce()
function applies a function to the elements of an iterable in a cumulative way, from left to right, to reduce the iterable to a single value. Here's an example:from functools import reduce numbers = [1, 2, 3, 4, 5] product = reduce(lambda x, y: x*y, numbers) print(product) # Output: 120
In this example, we use reduce()
to apply the lambda function x*y
to the elements of the list numbers
in a cumulative way, from left to right. The result is the product of all the numbers in the list.
In summary, map()
, filter()
, and reduce()
are three built-in functions in Python that support functional programming techniques. They allow you to apply functions to iterables in various ways to transform, filter, and reduce data.
How to use map()
function in Python:
map()
.numbers = [1, 2, 3, 4, 5] squared = map(lambda x: x**2, numbers)
Filtering elements with filter()
in Python:
filter()
.numbers = [1, 2, 3, 4, 5] even_numbers = filter(lambda x: x % 2 == 0, numbers)
Aggregating values with reduce()
in Python:
reduce()
.from functools import reduce numbers = [1, 2, 3, 4, 5] sum_result = reduce(lambda x, y: x + y, numbers)
Applying functions to iterables with map()
in Python:
map()
simplifies applying a function to each element of an iterable.words = ["apple", "banana", "cherry"] uppercased = map(str.upper, words)
Conditionally selecting elements with filter()
in Python:
filter()
to selectively include elements based on a specified condition.temperatures = [25, 30, 15, 40, 10] high_temps = filter(lambda x: x > 30, temperatures)
Reducing sequences with reduce()
in Python:
reduce()
to successively apply a function to pairs of elements, reducing the sequence to a single result.product = reduce(lambda x, y: x * y, [1, 2, 3, 4])
Lambda expressions and functional programming in Python:
square = lambda x: x**2 result = square(5)