lambda functions in python

 Anonymous Functions:

  • 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 as lambda functions.

Use of lambda or anonymous function:

  • Lambda function is mostly used for creating small and one-time anonymous functions. That means, Lambda function can take any number of arguments and must return one value in the form of an expression
  • Lambda functions are mainly used in combination with the functions like filter(), map() and reduce().

Syntax:

  1. Variable=lambda [argument(s)] :expression

Example program:

  1. sum = lambda val1, val2: val1 + val2

    print ('The Sum is :', sum(20,40))

    print ('The Sum is :', sum(-10,40))

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me