Anonymous / Lambda Function
Python Basics
Python Introduction
Python Installation
Overview of Jupyter IDE
Identifiers & Reserved Keywords
Python Variables
Python Numbers
Python Operators
Python Operators and Arithmetic Operators
Comparison and Logical Operators
Assignment and Bitwise Operators
Identity and Membership Operators
Python Flow Control
if else if else statement
While Loop Statement
Python For Loop
Break and Continue Statement
Python Data Types
Python Strings
Python Strings Methods
Python Lists
Python Tuples
Python Dictionary
Python Functions
Introduction to Python Functions
Function Arguments
Recursion Function
Lambda/Anonymous Function
Python - Modules
Python Files
Python - Files I/O
Python - Exceptions Handling
Python - Debugging
Anonymous / Lambda Function
In Python, anonymous function is a function that is defined without a name.
Rules of Lambda Function
Syntax
lambda [arg1,arg2,....,argn]:expression
Example 1: Normal Function
# Function to adds two numbers and returns the output def add(x,y): return x + y # Calling the Function add(10,25) Output: 35
Example 2: Lambda Function
# Lambda Function to adds two numbers,returns the output and called through the variable sum = lambda x, y: x + y # call the lambda function print("Sum of two numbers : ",sum(10,25)) Output: Sum of two numbers : 35
(OR)
# Lambda Function to adds two numbers and returns the output (lambda x, y: x + y)(10,25) Output: 35
Lambda functions along with built-in functions
Example 3 :
# Program to filter out only the odd items from a list mylist = [1 , 5 , 15 , 22 , 58 , 98] oddlist = list(filter(lambda x: (x%2 != 0), mylist)) print(oddlist) Output: [1, 5, 15]
Example 4:
# Program to display cube of each item in a list using map() mylist2 = [1, 2, 3, 4, 5, 6, 7] cubelist = list(map(lambda x: x ** 3, mylist2)) print(cubelist) Output: [1, 8, 27, 64, 125, 216, 343]