BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Anonymous(Lambda) Function

Last Updated: June 16, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

In this tutorial, we will discuss Anonymous functions in Python. In Python, anonymous function is also known as lambda function.

Table of Contents

  • Anonymous(Lambda) Function
  • Syntax of Anonymous Function in python
  • Python lambda function Example
  • Example of filter() with lambda function
  • Example of map() with lambda function

Python Anonymous(Lambda) Function

An anonymous function in Python is a function without name.

A function in Python is defined using def keyword. In Python, anonymous/lambda function is defined using lambda keyword.

Syntax of Anonymous Function in python

In Python a lambda function has following syntax:

lambda <arguments>: expression

Anonymous function allows multiple arguments but only one expression, the expression is evaluated based on the passed arguments and the result of the expression is returned.

Python lambda function Example

In the following example we are using a lambda function that returns the square of a given number.

my_square = lambda num: num * num

n = 4

# Output: Square of number 4 is:  16
print("Square of number", n, "is: ",my_square(n))

Output: