Overview of Lambda Functions in Python Programming
Lambda functions in python programming its syntax and how to use them. Lambda functions are also known as anonymous functions.
In this overview of the Lambda functions in Python Programming, you will learn the following things.
- What is Lambda Calculus?
- The History of Lambda Functions
- What are Lambda Functions in Python Programming?
- Syntax of Lambda Functions
- Practical Example of Lambda Functions in python
- Why Use Lambda Functions?
- Using Lambda Functions with filter(), map() and reduce() functions
- Difference between Python Lambda and Regular Functions
What is Lambda Calculus?
Programming language like C++ which is derived from the C language similarly in Python and other major programming languages lambda expression have their deep roots back to the lambda calculus.
Lambda calculus is a computational model invented by Alonzo Church an American logician and a mathematician.
The History of Lambda Functions
In the year of 1930s, a mathematician and logician named Alonzo Church invented a model of computation name as calculus which is a pure abstraction language.
Lambda or Anonymous functions are also referred to as lambda abstractions which are a direct model of Alonzo Church’s original creation.
It is like a Turing test but contrary to the concept of the Turing machine it does not keep any fixed state.
There are two types of programming languages that exist which are as follows.
- Functional Programming Languages
- Imperative Programming Languages
Functional Programming Languages
Mathematical logic and lambda calculus is the building block in functional programming languages and they base on them.
Imperative Programming Languages
The imperative programming languages meet the state-based model of computation invented by Alan Turing.
These two above mentioned programming models can be combined into each other and this process of combination is called the Church-Turing hypothesis.
In this case, Python Programming Language is not primarily a functional language, but it adopted some functional concepts in his early days.
Some new features like map(), filter(), reduce() and the lambda operator was added in this language in the year 1994.
What are Lambda Functions in Python Programming?
In Python programming, a function without any name is called a Lambda function or simply anonymous function.
Normally keyword def
is used to define a normal function but to define lambda functions in Python programming keyword lambda
is used.
Syntax of Lambda Functions
lambda arguments: expression
Lambda or Anonymous functions can have many arguments but with only one expression. The use of expression is evaluated and returned.
Practical Example of Lambda Functions in Python
Here is a practical example of using lambda functions in Python programming that triples the given value.
# This Program to show the use of lambda functions double = lambda x: x * 3 print(double(6))
Output:
18
In the above-mentioned example of lambda functions in Python programming, we set the value as x: x *3 where x is the argument and x * 3 is the expression that gets calculated and returned.
Remember: A thing needs to remember is that this function has not any name defined. It just returns a function object assigned to the data type double. This can be called a statement.
Why Use Lambda Functions?
You can enjoy the better power and capability of lambda functions when it is used inside another function. Moreover, when we are in need of a function without any name with a short span of time we use lambda functions.
Remember: These functions can also be used along with other functions like filter()
, map()
and reduce()
.
Using Python Lambda Functions with filter(), map() and reduce() Functions
Using Python Lambda Functions with filter() Function
In Python programming filter() function takes an argument in the form of a list. When this function called all the items in the existing list and with a new list is returned which contains items for which the function evaluated to True.
Practical Example of filter() function:
# Python code to illustrate lambda() #function with filter() function list_items = [6, 8, 23, 101, 53, 60, 65, 23, 87, 123] final_list_items = list(filter(lambda x: (x%2 != 0) , list_items)) print(final_list_items)
Output:
[23, 101, 53, 65, 23, 87, 123]
In the above example, the function returns all the numbers that are not even means if we divide them with two the remainder is not equal to zero.
Using Python Lambda Functions with map() Function
In Python programming map() function takes a list as an argument and a function. When this function is calls out with lambda() function with a list that we have provided. Then it returns a new list with all the modified lambda items.
A Practical Example of a map() function:
# This Python code to shows # map() function with lambda() function # to get double of a list. list_items = [2, 6, 20, 90, 52, 65, 70, 20, 80, 100] final_list_items = list(map(lambda x: x*2 , list_items)) print(final_list_items)
Output:
[4, 12, 40, 180, 104, 130, 140, 40, 160, 200]
In the above example, a map() function with lambda() returns all the list items by multiplying them with 2.
Using Python Lambda Functions with reduce() Function
In Python programming, a reduce() function accepts a function and a list of values as an argument. When this function is called it returns a new reduced list. Basically reduce() function returns a repetitive task over the pairs of the list items.
This is a part of a module named as functool.
A Practical Example of reduce() function:
# This Python code to shows how # reduce() function with lambda() function # works to get sum of a list items from functools import reduce list_items = [6, 9, 11, 22, 55, 102] sum = reduce((lambda x, y: x + y), list_items) print (sum)
Output:
205
In the above example, a reduce() function adds the first two elements and then adds the other elements and this process goes on till all the values get added just like (((((6+9)+11)+22)+55)+102)
Difference Between Python Lambda and Regular Functions
There is a quote from the Python Design and History FAQ that shows the overall situation and expectation for the usage of lambda function in Python programming. (Check the source)
In Python, this is not a serious problem. Unlike lambda forms in other languages, where they add functionality, Python lambdas are only a shorthand notation if you’re too lazy to define a function.
Despite this quote don’t discourage yourself from using Python’s lambda function in your code if needed. Maybe, at first sight, you think that a this function is just like any other function that takes arguments.
The following section highlights the common things and differences between the Python normal functions and Python lambda()
functions.
Usage of Regular Functions and Lambda Functions in Python Programming
You may be surprised at this point that fundamentally what functionality distinguishes a lambda function from a Python regular expression with a single return line. Under the hood, there is no difference.
Now lets how Python handles a function that is built with a return statement and a lambda() function.
import dis addition = lambda x, y: x + y type(addition) dis.dis(addition) addition
Output:
0 LOAD_FAST 0 (x) 2 LOAD_FAST 1 (y) 4 BINARY_ADD 6 RETURN_VALUE <function <lambda> at 0x7f30c6ce9ea0>
As you can see dis()
function shows a readable version of the Python programming bytecode giving permission to inspect the low-level instructions for Python interpreter to use while it’s executing the program.
Now let’s have a look at a regular function:
import dis def addition(x, y): return x + y type(addition) dis.dis(addition) addition
Output:
0 LOAD_FAST 0 (x) 3 LOAD_FAST 1 (y) 6 BINARY_ADD 7 RETURN_VALUE <function add at 0x7f30c6ce9f28>
In these two functions, the Python interpreted the bytecode in the same way but the difference is in their naming.
As you can see one function name is an addition for a function defined with def
whereas the function with lambda is seen as lambda.
perfectly explained the Lambda Function in Python