Which of the following is called an anonymous function in python

What are lambda functions in Python?

In Python, an anonymous function is a function that is defined without a name.

While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword.

Hence, anonymous functions are also called lambda functions.


How to use lambda Functions in Python?

A lambda function in python has the following syntax.

Syntax of Lambda Function in python

lambda arguments: expression

Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.


Example of Lambda Function in python

Here is an example of lambda function that doubles the input value.

# Program to show the use of lambda functions
double = lambda x: x * 2

print(double(5))

Output

10

In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the expression that gets evaluated and returned.

This function has no name. It returns a function object which is assigned to the identifier double. We can now call it as a normal function. The statement

double = lambda x: x * 2

is nearly the same as:

def double(x):
   return x * 2

Use of Lambda Function in python

We use lambda functions when we require a nameless function for a short period of time.

In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions like filter(), map() etc.

Example use with filter()

The filter() function in Python takes in a function and a list as arguments.

The function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to True.

Here is an example use of filter() function to filter out only even numbers from a list.

# 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)

Output

[4, 6, 8, 12]

Example use with map()

The map() function in Python takes in a function and a list.

The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item.

Here is an example use of map() function to double all the items in a list.

# Program to double each item in a list using map()

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

new_list = list(map(lambda x: x * 2 , my_list))

print(new_list)

Output

[2, 10, 8, 12, 16, 22, 6, 24]

The def keyword is used to define a function in Python, as we have seen in the previous chapter. The lambda keyword is used to define anonymous functions in Python. Usually, such a function is meant for one-time use.

lambda [arguments] : expression

The lambda function can have zero or more arguments after the : symbol. When this function is called, the expression after : is executed.

square = lambda x : x * x

Above, the lambda function starts with the lambda keyword followed by parameter x. An expression x * x after : returns the value of x * x to the caller. The whole lambda function lambda x : x * x is assigned to a variable square in order to call it like a named function. The variable name becomes the function name so that We can call it as a regular function, as shown below.

The above lambda function definition is the same as the following function:

def square(x):
    return x * x

The expression does not need to always return a value. The following lambda function does not return anything.

>>> greet = lambda name: print('Hello ', name) 
>>> greet('Steve')
Hello Steve

The lambda function can have only one expression. Obviously, it cannot substitute a function whose body may have conditionals, loops, etc.

The following lambda function contains multiple parameters:

>>> sum = lambda x, y, z : x + y + z 
>>> sum(5, 10, 15)
30

The following lambda function can take any number of parameters:

>>> sum = lambda *x: x[0]+x[1]+x[2]+x[3]  
>>> sum(5, 10, 15, 20)
50

Parameterless Lambda Function

The following is an example of the parameterless lambda function.

>>> greet = lambda : print('Hello World!')
>>> greet()
Hello World!

Anonymous Function

We can declare a lambda function and call it as an anonymous function, without assigning it to a variable.

>>> (lambda x: x*x)(5)
25

Above, lambda x: x*x defines an anonymous function and call it once by passing arguments in the parenthesis (lambda x: x*x)(5).

In Python, functions are the first-class citizens, which means that just as literals, functions can also be passed as arguments.

The lambda functions are useful when we want to give the function as one of the arguments to another function. We can pass the lambda function without assigning it to a variable, as an anonymous function as an argument to another function.

>>> def dosomething(fn):
	    print('Calling function argument:')
	    fn()
>>> dosomething(lambda : print('Hello World')) # passing anonymous function
Calling function argument:
Hello World
>>> myfn = lambda : print('Hello World') 
>>> dosomething(myfn) # passing lambda function

Above, the dosomething() function is defined with the fn parameter which is called as a function inside dosomething(). The dosomething(lambda : print('Hello World')) calls the dosomething() function with an anonymous lambda function as an argument.

Python has built-in functions that take other functions as arguments. The map(), filter() and reduce() functions are important functional programming tools. All of them take a function as their argument. The argument function can be a normal function or a lambda function.

>>> sqrList = map(lambda x: x*x, [1, 2, 3, 4]) # passing anonymous function
>>> next(sqrList)
1
>>> next(sqrList)
4
>>> next(sqrList)
9
>>> next(sqrList)
16
>>> next(sqrList)
25

What are anonymous functions in Python?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

Which of the following is called an anonymous function in Python Mcq?

Explanation: lambda is an anonymous function in Python.

Which function is also known as anonymous function?

Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class.

What is anonymous function with example?

An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation. Normal function definition: function hello() { alert('Hello world'); } hello();