0% found this document useful (0 votes)
31 views

Lambda Operator/Function

The document discusses lambda functions in Python, which allow for the creation of small anonymous functions without a name using the lambda keyword. Lambda functions consist of an argument list and expression, and are useful when only a simple throw-away function is needed. While less general than regular defined functions, lambdas are commonly used with functions like filter, map and reduce for functional programming in Python.

Uploaded by

Asher E David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lambda Operator/Function

The document discusses lambda functions in Python, which allow for the creation of small anonymous functions without a name using the lambda keyword. Lambda functions consist of an argument list and expression, and are useful when only a simple throw-away function is needed. While less general than regular defined functions, lambdas are commonly used with functions like filter, map and reduce for functional programming in Python.

Uploaded by

Asher E David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Python Programming

Hi Class!

1
Lambda
Operator/Function

2
Lambda Operator/Function

• The lambda operator or lambda function is


a way to create small anonymous
functions, i.e. functions without a name.
• These functions are throw-away functions,
i.e. they are just needed where they have
been created.
Syntax of a lambda function
• The general syntax of a lambda function is
quite simple:
lambda argument_list: expression
• The argument list consists of a comma
separated list of arguments.
• The expression is an arithmetic expression
using these arguments. You can assign the
function to a variable to give it a name.
Example

>>> f = lambda x, y : x + y
>>> f(1,1)
2
More about Lambda functions
• Python supports the creation of
anonymous functions (i.e. functions that
are not bound to a name) at runtime, using
a construct called lambda.
• Its well integrated into Python and is often
used in conjunction with typical functional
concepts like filter(), map() and reduce().
More about Lambda functions

Like def, the lambda creates a function to


be called later. But it returns the function
instead of assigning it to a name. This is
why lambdas are sometimes known
as anonymous functions.
Difference between normal and
lambda function

Normal function Lambda function

def func(x): return x ** 3 lamb = lambda x: x ** 3


print(func(5)) print(lamb(5))
125 125
Lambda is an expression, not a
statement.
• Because of this, a lambda can appear in
places a def is not allowed. For example,
places like inside a list literal, or a function
call's arguments.
• As an expression, lambda returns a value
that can optionally be assigned a name. In
contrast, the def statement always assigns
the new function to the name in the
header, instead of returning is as a result.
lambda's body is a single
expression, not a block of
statements.
• The lambda's body is similar to what we'd
put in a def body's return statement. We
simply type the result as an expression
instead of explicitly returning it. Because it
is limited to an expression, a lambda is
less general that a def.
• lambda is designed for coding simple
functions, and def handles larger tasks.
Example
Default work
on lambda arguments:
Example
Example
Lambda Example

You might also like