Functional Programming in Python

NIIT Digital
6 min readJul 26, 2021

--

Photo by Stefan Cosma on Unsplash

Originally published at https://www.niit.com/india/

Python offers functional programming, which is a programming paradigm, and it tries to bind everything in a simple mathematical functions style. It aims at “what to solve” that goes against an imperative style where the main objective is “how to solve”. Functional programming uses expressions instead of statements. It is a demonstrative type of programming. An expression is calculated to make a value, while a statement is produced to assign variables. To know more about programming in Python do check out the Post Graduate Program in Full Stack Software Engineering at NIIT.

Without further ado, let’s get to it.

Concepts of Functional Programming:

Functional programming language should follow these concepts:

  1. Pure Functions: Functions have two principle properties. To begin with, they generally produce the same output for similar arguments independent of anything else. Also, they have no side effects. For example, they do alter any argument or global variables or output something.
  2. Recursion: No “for” or “while” loops should be present in functional languages. Iteration in functional languages is carried through recursion.
  3. Functions are First-Class and can be Higher-Order: Top functions are treated as first-class variables. The top variables can be passed to functions as a parameter, which can be returned from functions or stored in data structures.
  4. Variables are Immutable: In functional programming, modifying a variable once it is initialized is not possible. New variables can be created instead of modifying the existing variables.

Functional Programming in Python:

Python also supports Functional Programming paradigms without any support of special features or libraries.

Pure Functions:

As explained above, pure functions have two properties.

  • It generally produces the same output for similar arguments. For example, 3+7 will always be 10.
  • Pure functions do not modify the input variable.

The second property is immutability. The result of a Pure Function is the value that it returns. Functional programming programs are easy to debug because pure functions have no side effects or private I/O. Moreover, these functions make it clear to write parallel/concurrent applications. When code is written in this manner, a smart compiler can do a variety of tasks, including parallelizing instructions. Also, it can wait to calculate results when needed and memorize the results since it changes as long as the input is not changed.

Example:

Recursion:

In functional programming, there is no concept like “for loop” or “while loop”; instead, there is a concept of recursion. It is a process in which a function calls itself indirectly or directly. The solution to the base case in the recursion program is produced, and solutions for big problems are expressed in smaller problems. The base case is a condition that lets the compiler or the interpreter exit from the function.

Example:

Given the program, we will find the sum of all the list elements without using anything for the loop.

Output:

First-class objects are taken care of uniformly throughout. They can be saved in data structures, passed as arguments, or can be used in control structures. A programming language supports first-class functions if it deals with the function as first-class objects.

Properties of the first-class function:

  1. It is an instance of the object type.
  2. A function can be stored as a variable.
  3. A function can be passed as a parameter to another function.
  4. Returning from one function to another is accessible.

A function is stored in data structures, i.e., hash tables, lists, etc.

Output:

Built-in Higher-order Function:

Python implemented some higher-order functions to make the processing of iterable objects such as lists and iterators easier. The higher-order function then returns an iterator that is space-efficient. However, few commonly used built-in higher-order functions are as follows:

  • Map(): After applying the below function to every item of a given iterable, i.e. list, tuple, etc., this function returns a list of results.

Syntax: map(fun, iter)

Parameters:

fun: It is a function to which a map passes each element of a given iterable.

iter: It is iterable, which is to be mapped.

Return Type: Returns an iterator of the map class

Example:

Output:

  • Filter(): With the help of a function that testifies whether each element in the sequence is true or not, this method filters the given sequence:

Syntax: filter(function, sequence)

Parameters:

function: a function that determines whether or not each element of a sequence is true or false

sequence: sequence which needs to be filtered, It can be sets, lists, tuples, or containers of any iterators that need to be filtered.

Return Type: returns a previously filtered iterator.

Example:

Output:

Output:

  • Lambda Function: Anonymous function in Python means that the function is without a name. We know that the lambda keyword helps create anonymous functions, whereas the def keyword helps define the normal function.

Syntax:

lambda arguments: expression

  1. The lambda function can have an ’n’ number of arguments but one expression only, which is calculated and returned.
  2. This function is restricted to only one single expression.
  3. It has multiple methods of programming along with other expressions in the function.
  4. The function is free to use where the function objects are required.

Example:

Output:

  • Immutability: This programming paradigm is used for debugging as it delivers an error where the variable is changed, and the value is not changed. String, tuple, numeric, etc., are a few types of immutable data that Python supports
  • Example:

Output:

Wrapping Up

Are you interested in Python programming? Does data science, machine learning, and artificial intelligence pique your curiosity? If yes, then you must check out NIIT’s Knowledge Centre for tutorials such as the one above.

--

--

NIIT Digital

We are a skill development hub for learners worldwide sharing ideas on technology. Explore insightful reads at https://www.niit.com/india/knowledge-center