Anonymous Functions
Learn how to create a function with no name in Python.
We'll cover the following
Using lambda
keyword
An anonymous function is a single-expression function, having no name. The lambda
keyword is used to declare the anonymous function. It can have any number of arguments but can have only one expression as stated above. Look at the simple example below and see how an anonymous function works.
x = lambda a: a+5 # anonymous function
The above anonymous function takes one argument a
and returns a+5
(single expression).
The above anonymous function is similar to the following regular function defined using the def
keyword:
def x(a):
return a+5
So, the lambda
keyword offers us a compact form to declare functions, as compared to the def
keyword.
Calling an anonymous function
Run the following program.
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy