Skip to content
Home » Anonymous functions

Anonymous functions


Anonymous Functions in Python (Lambda Functions)

An anonymous function is a function without a name.
In Python, anonymous functions are created using the lambda keyword.
These are also called lambda functions.

They are small, one-line functions used for simple operations.


1. What is a Lambda Function?

A lambda function is a short, single-expression function that does not use def.

Syntax

lambda arguments: expression

Example:

square = lambda x: x * x
print(square(5))

Output:

25

2. Characteristics of Anonymous (Lambda) Functions

✔ No name (anonymous)
✔ One-line function only
✔ Can take multiple arguments
✔ Can be assigned to variables
✔ Often used inside other functions
✔ Faster for simple operations

But:

❌ Cannot contain multiple statements
❌ Cannot contain loops or complex logic
❌ Less readable for long operations


3. Why Use Lambda Functions?

Lambda functions are used when:

✔ A simple function is needed temporarily
✔ Used inside functions like map(), filter(), sorted()
✔ Avoid defining full def functions

They help write shorter and cleaner code.


4. Assigning a Lambda to a Variable

add = lambda a, b: a + b
print(add(10, 20))   # 30

5. Lambda with No Argument

msg = lambda: "Hello"
print(msg())

6. Lambda with Multiple Arguments

mul = lambda a, b, c: a * b * c
print(mul(2, 3, 4))

7. Lambda with Conditional Expression

max_num = lambda a, b: a if a > b else b
print(max_num(10, 20))

8. Lambda Inside Built-in Functions

A. Using lambda with map()

Applies a function to every element.

nums = [1, 2, 3, 4]
squares = list(map(lambda x: x * x, nums))
print(squares)

B. Using lambda with filter()

Filters elements based on condition.

evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens)

C. Using lambda with sorted()

Sort list of tuples by second element.

pairs = [(1, 3), (2, 1), (5, 2)]
sorted_pairs = sorted(pairs, key=lambda x: x[1])
print(sorted_pairs)

9. Lambda in a User-Defined Function

def make_increment(n):
    return lambda x: x + n

inc = make_increment(5)
print(inc(10))   # 15

10. Limitations of Anonymous Functions

❌ Only one expression allowed
❌ Cannot use loops (for, while)
❌ Cannot include statements (if only as expression)
❌ Less readable for complex tasks

Therefore, use lambda for simple, short tasks only.


11. Practical Examples

Example 1: Find square

square = lambda x: x * x

Example 2: Check Even or Odd

check = lambda x: "Even" if x % 2 == 0 else "Odd"

Example 3: Add 10 to a number

add10 = lambda x: x + 10

Example 4: Maximum of three numbers

largest = lambda a, b, c: a if a > b and a > c else (b if b > c else c)

12. Difference: def vs lambda

def FunctionLambda Function
Can have multiple linesOnly one expression
Has a nameAnonymous
Supports loopsNo loops
Supports complex logicSimple logic
Better readabilityCompact but less readable

Summary (for exam)

Anonymous functions are created using the lambda keyword.
They are single-expression, nameless functions used for short, temporary tasks.
Mostly used with map(), filter(), reduce(), sorted(), and inside other functions.