Python Lambda Function With Examples | How To Create A New Anonymous function

Python Lambda function is also called Python anonymous function. Python lambda function is created using the ‘lambda’ keyword. Since the User-defined function is defined using ‘def’ keyword whereas lambda function is defined with ‘lambda’ keyword.

Lambda function can take multiple arguments and return one expression as result. Python lambda function can be used inside another function.

Syntax of Python lambda function:

Anonymous function in python is created using ‘lambda’ keyword. below is the syntax

lambda arguments: expression

EXAMPLE: Write a lambda function in python that takes two numbers as input and returns multiplication of both numbers.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun  9 11:27:04 2021
website : http://mycloudplace.com
@author: mycloudplace
"""

def main():
    #Lambda function to multiple two numbers
    print("Example of Lambda function to multiple two numbers")
    multiple = lambda a,b : a*b
    
    first_number = 12
    second_number = 14
    result = multiple(first_number, second_number)
    print("Result of two numbers after multiplication (", first_number , "*" ,second_number ,") = ", result)

if __name__ == "__main__":
    main() 

OUTPUT:

Example of Lambda function to multiple two numbers
Result of two numbers after multiplication ( 12 * 14 ) =  168

In an interview, the below questions can be asked

  1. Question: What is the lambda function in python explain with an example?
    AnswerIn python, the lambda function or anonymous function is defined without any name using the ‘lambda’ keyword.
    For lambda function syntax, definition, and example please go to the top.
  2.  Question: How do you write a lambda function in Python?
    or
    How do you declare an anonymous function in Python?
    or
    What keyword is used to create anonymous functions?
    Answer: Using the ‘lambda’ keyword, we can create the anonymous function/lambda function in python.
  3. Question: Are lambda functions faster than standard user-defined functions in python?
    AnswerUsually anonymous functions/lambda functions are inline functions and thus they execute comparatively faster than user-defined functions.
    Another benefit is lambda functions make code much more readable.
  4. Question: Can the python lambda function return multiple variables?
    AnswerLambda function can return multiple values but in form of a tuple. Internally we are returning a single tuple value.
    example : lambda a, b: (a+a, b+b)

The lambda function is extensively used in machine learning, data science, and artificial intelligence projects. Python is a preferable language in machine learning projects.

Use of lambda function in python

Below are the places where we can use a lambda function

  1. When we want to create a function without any name.
  2. When we want to write an inline function.
  3. To pass a function as arguments to another function.
  4. Lambda function can be used with existing python built-in functions.

EXAMPLE: Write a python program using the lambda function to get the square of even number and cube of odd numbers from a list of numbers.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun  10 11:27:04 2021
website : http://mycloudplace.com
@author: mycloudplace
"""

def main():
    #python program using lambda function to get the square of even number and cube of odd numbers from a list of numbers   
    print("python program using lambda function to get the square of even number and cube of odd numbers from a list of numbers")
    input_numbers = [2,3,4,5,6,7,8,9,10]
    square = lambda x: x * x
    cube = lambda x: x * x * x
    
    result = list(map(lambda x: square(x) if(x % 2==0) else cube(x),input_numbers))
    print(result)

if __name__ == "__main__":
    main() 

OUTPUT:

python program using lambda function to get the square of even number and cube of odd numbers from a list of numbers
[4, 27, 16, 125, 36, 343, 64, 729, 100]

Our previous python articles are:

Wiki For details of Anonymous function/Lambda function,

7 thoughts on “Python Lambda Function With Examples | How To Create A New Anonymous function”

  1. Pingback: Python Useful Built-in Functions With Examples | How to create a new function - Mycloudplace

  2. Pingback: Python array | numpy for python | How to create numpy or np array in python - Mycloudplace

  3. Pingback: Python reporting libraries | Python reporting tools to generate interactive and beautiful reports - Mycloudplace

Leave a Comment

Your email address will not be published. Required fields are marked *