Functional Programming in Python
By: Pasindu Ukwatta
Tags: Python3, Programming, Functional Programming, Software Development, Python

Photo by Vlada Karpovich from Pexels

“A language that doesn’t affect how you think about programming is not worth learning ” — Alan Perlis

Python is one of the most top Ranked programming languages in the Modern World. It is a High-Level Language. Syntax In the Python is similar to the English Language. Python has used in Big range. Python has a large community to help resolve any problems. Biggest Companies like Google, Facebook, Instagram, Spotify, Netflix use the python for the Development Process in their Companies. Mathematics, Web Development, Software Development are some areas use Python. Python has its capabilities to consider it as a Functional Programming Language.

Meaning of the Functional Programming is that we bind everything in pure Mathematical Style. Python has its inbuilt functions. Reducing the time of the coding is an advantage of the functions. Using predefined styles and rules are some problems that arise when using the functions. When we need Concurrency or Parallelism and doing mathematical Computation we can use Functional Programming.

Pure Functions

Let’s create a pure function to multiply numbers by 2:

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] src- https://stackabuse.com/functional-programming-in-python/

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

Functional Programming also called as Pure Functional Programming. There are some rules to identify pure functions. Always the function should produce the same result at any given time. This is the First Rule. Meaning of this rule is that the result of the function cannot be changed with the time. Then the next rule is that function should not be affected by the outside world. Meaning of that is, the function should only work with the range of the function. Advantages of the pure functions that easy to debugging and testing and restricting the side effects from the outside world help to execute the function without any exception.

Filter Function

# list of letters

letters = ['a', 'b', 'd', 'e', 'i', 'j', 'o']



# function that filters vowels

def filterVowels(letter):

vowels = ['a', 'e', 'i', 'o', 'u']



if(letter in vowels):

return True

else:

return False



filteredVowels = filter(filterVowels, letters)



print('The filtered vowels are:')

for vowel in filteredVowels:

print(vowel) src- https://www.programiz.com/python-programming/methods/built-in/filter

Here, we have a list of letters and need to filter out only the vowels in it. In this function, we need to provide the function name and iterable as the arguments. when we need to filter to the iterable object using a function and get another output without affecting the original input, Then we can use the filter function in Python.

Zip Function

number_list = [1, 2, 3]

str_list = ['one', 'two', 'three']



# No iterables are passed

result = zip()



# Converting itertor to list

result_list = list(result)

print(result_list)



# Two iterables are passed

result = zip(number_list, str_list)



# Converting itertor to set

result_set = set(result)

print(result_set) src- https://www.programiz.com/python-programming/methods/built-in/zip

In this function, we need to provide the iterable to the function. Zip function can use to get the output as a whole from the given inputs. When we provide lists as an input to the function and output will come as a whole. In the output will arrange according to the index of the lists. These means in the output list, at the index (0) all the values from the given list are put into a single tuple and put into the index (0) of the output list. This will continue through the whole list.

Reduce Function

from functools import reduce



numbers = [ 1 , 2 , 3 , 4 , 5 ]



def my_sum(a,b):

return a+b



result = reduce(my_sum,numbers)



print(result)

In the python, we need to import reduce from the functools . This use to apply a particular function passed in its arguments to all of the list elements mentioned in the functions passed away.

Lambda Function

# Program to filter out only the even items from a list

my_list = [1, 5, 4, 6, 8, 11, 3, 12]



new_list = list(filter(lambda x: (x%2 == 0) , my_list))



print(new_list) src- https://www.programiz.com/python-programming/anonymous-function

The function without a name is called the lambda function. It is an anonymous function. We use the lambda keyword when we implement lambda functions. For the use of the short period, we can use lambda functions

Conclusion

Python has functions programming capabilities as some programming language in use. Having a strong understanding of Functional Programming is key to writing good programs with better performance. This article has explored some ways to use Functional Programming in Python that I hope will assist you in completing your work more accurately.

I’d like to thank you for reading my article, I hope to write more articles on Python in the future to keep an eye on my account if you liked what you read today!

References

https://docs.python.org

https://www.learnpython.org/en/Map,_Filter,_Reduce