Lambda functions in python are single expression functions that are not bound to a name. This is why lambda functions are also called anonymous functions. In this tutorial, we’ll look at lambda functions, how are they different from normal functions, and where are they used along with some examples.
Table of Contents
- Lambda Function Syntax
- How are lambda functions different from normal functions?
- When do we use lambda functions?
Lambda Function Syntax
Lambda functions in python are defined using the lambda
keyword. They are single expression functions. We use the following syntax to define them:
lambda arguments : expressions
Example,
add_10 = lambda x: x+10 print(add_10(15))
The above code gives the following output on execution:
25
In the above example, the statement lambda x: x+10
returns a function object which is stored in add_10
. This function object can be used like any normal function.
Lambda functions can have any number of arguments but only a single expression.
Example:
a = lambda x,y,z: x+y+z print(a(1,2,3))
The above code gives the following output on execution:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
6
In the above example, the lambda function has three arguments, x
, y
, and z
and a single expression x+y+z
.
How are lambda functions different from normal functions?
Lambda functions in python work in a similar way as regular functions do. But, there are some differences between the two:
- Lambda functions are defined using the
lambda
keyword and are not bound to a name. Regular functions, on the other hand, are defined using thedef
keyword and have a name associated with them. - Lambda functions are restricted to a single expression while normal functions can have any number of expressions. Lambda functions have an implicit return statement. During execution, a lambda function evaluates its expression and automatically returns the result.
The example below illustrates the difference between the two:
# Function to add two numbers def add(x,y): # return the sum return x+y add(10,20)
Output:
30
add = lambda x,y: x+y print(add(10,20))
Output:
30
In the above example, the same task of adding two numbers is performed by a regular python function and a lambda function.
When do we use lambda functions?
You may be wondering that if lambda functions and regular functions do the same thing, then why do we use them in the first place?
- If a function is required for a single use simple task, lambda functions can be a good alternative. However, one should avoid writing complicated lambda functions that hamper code readability only to save a couple of lines of code.
- Lambda functions can be used to return a function from another function. See the example below:
# A function returning another function def add_n(n): # returns a lambda function to add n return lambda x: x+n # Function to add 10 to a number add_10 = add_n(10) # calling the function print(add_10(5))
Output:
15
In the above example, the function add_n(n)
takes in an argument n
and returns a lambda function to add that particular argument.
Lambda functions can be used for other use cases as well. For more, check out this answer on stack overflow.
If you’d like to know about regular functions in python check out our article on python functions.
In this tutorial, we looked at lambda functions in python. If you found this article useful do give it a share! For more such articles subscribe to us.
If you’re a beginner looking to start your data science journey and learn python, check out our Python for Data Science Series.