- Learning Objectives
- Prerequisites
- Introduction
- What is a Lambda Function?
- Why are Lambda Functions Required?
- Syntax of Lambda Function
- Characteristics of Lambda Functions
- Lambda Function vs Normal Function
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand Lambda Functions.
- Differentiate normal functions and lambda functions.
- Write lambda expressions.
- Use lambda functions in real-world applications.
- Understand when lambda functions should and should not be used.
Before learning this chapter, you should know:
- Variables
- Operators
- Functions
- Return Statement
In Python,
functions are usually created using the def keyword.
However,
sometimes we need a small function that performs only one simple operation.
Writing a complete function using def becomes unnecessary.
Python provides Lambda Functions to solve this problem.
A lambda function is a short, anonymous function that can be written in a single line.
It is commonly used with functions such as:
- map()
- filter()
- reduce()
- sorted()
- max()
- min()
A Lambda Function is an anonymous function that contains a single expression and returns its result automatically.
A lambda function is a one-line anonymous function.
Suppose we want to add two numbers.
Using a normal function
def add(a, b):
return a + bUsing lambda
add = lambda a, b: a + bThe lambda function is shorter and easier to write.
lambda parameters : expressionExample
square = lambda x: x * xlambda
↓
Parameters
↓
:
↓
Expression
↓
Return Value
Unlike normal functions,
lambda functions do not require
- def
- return
- function name
The result of the expression is returned automatically.
- Anonymous function.
- One-line function.
- Contains only one expression.
- Automatically returns the result.
- Can accept multiple parameters.
- Can be assigned to variables.
- Frequently used with higher-order functions.
| Normal Function | Lambda Function |
|---|---|
Uses def |
Uses lambda |
| Has a name | Usually anonymous |
| Multiple statements allowed | Only one expression |
Uses return |
Returns automatically |
| Suitable for large logic | Suitable for small logic |
Lambda functions are useful when:
- A function is used only once.
- The logic is very small.
- Passing a function as an argument.
- Working with
map(),filter(), orsorted().
Avoid lambda functions when:
- Multiple statements are required.
- Complex business logic is involved.
- Readability decreases.
- The function will be reused many times.
Program Starts
↓
Lambda Function Created
↓
Parameters Received
↓
Expression Evaluated
↓
Result Returned Automatically
↓
Program Ends
- Short syntax.
- Easy to write.
- Improves readability for simple operations.
- No explicit return statement.
- Useful with functional programming.
- Only one expression is allowed.
- Cannot contain multiple statements.
- Less readable for complex logic.
- Not suitable for large functions.
- Use lambda only for simple operations.
- Prefer normal functions for complex logic.
- Use meaningful variable names.
- Avoid nested lambda expressions.
- Keep expressions short.
Trying to write multiple statements.
❌ Incorrect
lambda x:
print(x)
return xUsing lambda for large programs.
Writing complicated expressions inside lambda.
Using lambda when a normal function improves readability.
Write a lambda function to add two numbers.
add = lambda a, b: a + b
print(add(10, 20))30
The lambda function accepts two parameters and returns their sum.
square = lambda x: x * x
print(square(6))36
cube = lambda x: x ** 3
print(cube(4))64
maximum = lambda a, b: a if a > b else b
print(maximum(25, 40))40
even = lambda n: "Even" if n % 2 == 0 else "Odd"
print(even(18))
print(even(25))Even
Odd
length = lambda text: len(text)
print(length("Python"))6
upper = lambda text: text.upper()
print(upper("python"))PYTHON
multiply = lambda a, b, c: a * b * c
print(multiply(2, 3, 4))24
positive = lambda n: "Positive" if n > 0 else "Not Positive"
print(positive(15))Positive
area = lambda r: 3.14159 * r * r
print(area(5))78.53975
Lambda Created
↓
Arguments Passed
↓
Expression Evaluated
↓
Result Returned
↓
Output Displayed
Memory
│
├── add
│ │
│ └── Lambda Function
│
├── square
│
├── cube
│
└── maximum
- Small and concise.
- Less code.
- Automatic return.
- Excellent for functional programming.
- Easy to pass as arguments.
- Keep lambda expressions simple.
- Use descriptive variable names.
- Prefer normal functions for lengthy logic.
- Use with
map(),filter(), andsorted()where appropriate.
- Multiple statements inside lambda.
- Deeply nested lambda expressions.
- Poor variable names.
- Using lambda everywhere.
A lambda function is an anonymous function containing a single expression.
lambdaNo.
Only one expression is allowed.
No.
The result is returned automatically.
Yes.
Because they are created without defining a named function using def.
With map(), filter(), reduce(), sorted(), and other higher-order functions.
- Find the square of a number.
- Find the cube of a number.
- Find the maximum of two numbers.
- Check whether a number is even or odd.
- Find the area of a circle.
- Lambda → Anonymous function
- One expression only
- Automatic return
- Uses
lambda - No
def - No explicit
return - Best for short operations
In this chapter, we learned:
- Lambda Functions
- Syntax
- Characteristics
- Advantages
- Limitations
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision