0% found this document useful (0 votes)
36 views2 pages

Python - Lambda

The document explains lambda functions in Python, which are small anonymous functions defined using the 'lambda' keyword. It provides examples of lambda functions for doubling a number, calculating the product of two numbers, and using them with higher-order functions like map and filter. Additionally, it illustrates how lambda functions can have multiple arguments and include print statements, though they are limited to a single expression.

Uploaded by

rudysid947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Python - Lambda

The document explains lambda functions in Python, which are small anonymous functions defined using the 'lambda' keyword. It provides examples of lambda functions for doubling a number, calculating the product of two numbers, and using them with higher-order functions like map and filter. Additionally, it illustrates how lambda functions can have multiple arguments and include print statements, though they are limited to a single expression.

Uploaded by

rudysid947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

21/10/2024, 13:13 Python

Python : Lecture 53 - Lambda Function

Lambda Functions in Python


In Python, a lambda function is a small anonymous function without a name. It is defined using the lambda
keyword and has the following syntax:

lambda arguments: expression

Lambda functions are often used in situations where a small function is required for a short period of time.
They are commonly used as arguments to higher-order functions, such as map, filter, and reduce.
Here is an example of how to use a lambda function:

# Function to double the input

def double(x):

return x * 2

# Lambda function to double the input

lambda x: x * 2

The above lambda function has the same functionality as the double function defined earlier. However, the
lambda function is anonymous, as it does not have a name.
Lambda functions can have multiple arguments, just like regular functions. Here is an example of a lambda
function with multiple arguments:

# Function to calculate the product of two numbers

def multiply(x, y):

return x * y

# Lambda function to calculate the product of two numbers

lambda x, y: x * y

Lambda functions can also include multiple statements, but they are limited to a single expression. For
example:

# Lambda function to calculate the product of two numbers,

[Link] 1/2
21/10/2024, 13:13 Python

# with additional print statement

lambda x, y: print(f'{x} * {y} = {x * y}')

In the above example, the lambda function includes a print statement, but it is still limited to a single
expression.
Lambda functions are often used in conjunction with higher-order functions, such as map, filter, and reduce
which we will look into later.

EXAMPLE:

def appl(fx, value):

return 6 + fx(value)

double = lambda x: x*2

cube = lambda x: x*x*x

avg = lambda x,y: (x+y)/2

print(double(5))

print(cube(5))

print(avg(3,5))

print(appl(lambda x: x * x * x, 2))

[Link] 2/2

You might also like