QuizCure
Find Available Solution Here!

Python Supports the Creation of Anonymous functions at Runtime

Deepak Nov 09, 2024

Yes, Python supports runtime anonymous functions. A function without any name is known as an anonymous function.

Lambda function Syntax

lambda parameters: expression

Let's understand with following single-line example for adding two numbers

def add(x, y):
    return x + y
print("Summation Value:",add(5, 2))

Output: ('Summation Value:', 7)

Here function add has one expression to execute therefore we can replace such a single expression by defining the anonymous function Lambda.

Normal function in Python is declared using the def keyword whereas an anonymous function is declared using a construct known as lambda.

Let's rewrite the above example code using Lambda as below

sum_number = lambda a, b: a + b
print("Summation Value:",sum_number(5, 2))
    

Output: : ('Summation Value:', 7)

This lambda function is invoked run time and returns results based on the input provided. Let's go through more relevant commonly asked Quizzes and Definitions to learn Python Supports the Creation of Anonymous functions at Runtime.

Filter Odd number from the list of mixed even and odd numbers

numbers = [13, 90, 17, 59, 21, 60, 5] 
odd_list = list(filter(lambda x: (x%2 != 0) , numbers))
print(odd_list)
    

Output:

[13, 17, 59, 21, 5]
    

Explanation:
Here lambda expression lambda x: (x%2 != 0) executed at runtime and returns results with odd numbers. Lambda is used here with an inbuilt function filter to select only odd numbers from the given list.

Lambda construct can have multiple input arguments with one expression.

Does Lambda support the inclusion of a return statement?

No. Anonymous only includes expression. It does not include return, assert, or pass. It will raise SyntaxError: invalid syntax if used with such statements.

Let's explore with the following program:

sum_number = lambda a, b: return a + b
print("Summation Value:",sum_number(5, 2))
   

Output: SyntaxError: invalid syntax

Explanation: In the above example used a return statement inside the lambda function which caused an error as invalid syntax. Lambda function already returns the return result of expression a + b. We don't need to use a return statement here. Let's remove the return and rewrite the above code in the following way

sum_number = lambda a, b:  a + b
print("Summation Value:",sum_number(5, 2))
   

Output: ('Summation Value:', 7)

Was this post helpful?

Send Feedback

Practice Multiple-Choice Questions

  • ?
    What is the output for the following code?
    def getEmployee(index):
    	employee = {"J" : "John" "p" : "Peter", "A" : "Alex", "C" : "Camren"}
    	return employee[index]
    
    print(getEmployee("J"))
    
    Options are:
  • ?
    Anonymous functions are created with the help of keyword:
    Options are:
    Try your hand at more Multiple-choice Exercises

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up