Functional programming means handling a large chunk of code by defining your own functions. It means comparing a problem to a set of functions. In this article, I’ll walk you through what functional programming in Python is.
What is Functional Programming?
Functional programming means producing outputs by taking only inputs and not affecting any internal state that may affect the output of a particular input.
Also, Read – Python Projects with Source Code: Solved and Explained.
It is a method of managing your large code in small tasks that can be used from multiple places within an application. For example, if something you want to implement in your app requires a dozen lines of code, you don’t want to repeat the same code every time you need to.
Also, whenever you want to change anything or want to fix a mistake in some code, you don’t have to do it every place you wrote the code. If this code is inside a function, you can only change or correct it on location.
Functional Programming in Python
Here are the 4 techniques we use for functional programming in Python:
- Lambda Function
- Map Function
- Reduce Function
- Filter Function
Now let’s go through all these techniques one by one.
Lambda Function:
An anonymous function is defined using lambda. Lambda settings are set to the left of the colon. The body of the function is defined to the right of the colon. Let’s see how to use the lambda function in Python:
s=lambda x:x*x s(2)
4
Map Function:
The Map function takes a function as a collection of items, then creates a new empty collection and runs on the function on each item present in the original collection and inserts each returned value into the new collection and returns the new collection as an output. Let’s see how to use the map function in Python:
def addition(n): return n + n numbers = (1, 2, 3, 4) result = map(addition, numbers) print(list(result))
[2, 4, 6, 8]
Reduce Function:
Just like the map function, the reduce function also takes a function as a collection of items and then returns the value created by combining the items. Let’s see how to use the reduce function in Python:
import functools def mult(x,y): print("x=",x," y=",y) return x*y fact=functools.reduce(mult, range(1, 10)) print(fact)
x= 1 y= 2 x= 2 y= 3 x= 6 y= 4 x= 24 y= 5 x= 120 y= 6 x= 720 y= 7 x= 5040 y= 8 x= 40320 y= 9 362880
Filter Function:
Just like the map and reduce functions, the filter function also takes a function as a collection of elements and then returns the collection of each element where the function returned True. Let’s see how to use the filter function in Python:
seq = [0, 1, 2, 3, 5, 8, 13] result = filter(lambda x: x % 2 != 0, seq) print(list(result))
[1, 3, 5, 13]
Summary
So we use lambda, map, reduce, and filter functions in Python for functional programming. You should try to use these functions in between your projects to master them properly. You can get the complete code use in this article from below.
I hope you liked this article on what is functional programming in Python. Feel free to ask your valuable questions in the comments section below.