Lambda Functions in Python
A Lambda function in Python is a small anonymous function. It is used to perform short and simple operations in a single line.
1. What is a Lambda Function?
A lambda function is a short function that normally does not have an explicit function name. Therefore, it is also called an anonymous function bhi kehte hain.
lambda x: x * 2
This function x takes as input and x * 2 returns
2. Normal Function vs Lambda
The same operation can be performed using both a normal function and a lambda.
Normal Function:
def square(number):
return number * number
print(square(5))
Lambda Function:
square = lambda number: number * number
print(square(5))
Output:
25
3. Lambda Syntax
lambda arguments: expression
| Part | Meaning |
|---|---|
| lambda | Keyword used to define a lambda function |
| arguments | Input values of the function |
| expression | Expression to be evaluated |
4. Lambda with One Argument
double = lambda x: x * 2
print(double(10))
print(double(25))
Output:
20
50
5. Lambda with Multiple Arguments
add = lambda a, b: a + b
print(add(10, 20))
print(add(50, 30))
Output:
30
80
6. Lambda with Three Arguments
total = lambda a, b, c: a + b + c
print(total(10, 20, 30))
Output:
60
7. Lambda with Condition
A conditional expression can also be used in a lambda.
check = lambda number: "Even" if number % 2 == 0 else "Odd"
print(check(10))
print(check(7))
Output:
Even
Odd
8. Lambda with Maximum Number
maximum = lambda a, b: a if a > b else b
print(maximum(10, 25))
Output:
25
9. Lambda with List
A lambda can also be used with list data.
numbers = [1, 2, 3, 4, 5]
square = lambda x: x * x
for number in numbers:
print(square(number))
Output:
1
4
9
16
25
10. Lambda with map()
map() is used to apply an operation to each element of a list.
numbers = [1, 2, 3, 4, 5]
squares = list(
map(lambda x: x * x, numbers)
)
print(squares)
Output:
[1, 4, 9, 16, 25]
11. Lambda with filter()
filter() is used to select elements that satisfy a condition.
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(
filter(lambda x: x % 2 == 0, numbers)
)
print(even_numbers)
Output:
[2, 4, 6]
12. Lambda with sorted()
A lambda can also be used to define a custom sorting rule.
students = [
("Amit", 85),
("Rahul", 72),
("Sneha", 92)
]
students.sort(
key=lambda student: student[1]
)
print(students)
Output:
[('Rahul', 72), ('Amit', 85), ('Sneha', 92)]
13. Lambda with Descending Sort
students = [
("Amit", 85),
("Rahul", 72),
("Sneha", 92)
]
students.sort(
key=lambda student: student[1],
reverse=True
)
print(students)
Output:
[('Sneha', 92), ('Amit', 85), ('Rahul', 72)]
14. CIIT Example 📩🤓
Suppose we have marks of CIIT students and we only want to filter the passing students.
marks = [35, 48, 72, 29, 65, 80]
passing_marks = list(
filter(lambda mark: mark >= 40, marks)
)
print(passing_marks)
Output:
[48, 72, 65, 80]
15. CIIT Course Example 📩
courses = [
("Python", 35000),
(".NET", 40000),
("Java", 38000)
]
courses.sort(
key=lambda course: course[1]
)
print(courses)
Output:
[('Python', 35000), ('Java', 38000), ('.NET', 40000)]
16. Lambda Automatically Returns Result
In a lambda function, return keyword explicitly does not need to be written explicitly. The result of the expression is returned automatically.
square = lambda x: x * x
print(square(6))
Output:
36
17. Lambda Function Limitations
- A lambda is generally suitable for a single expression.
- A normal function is better for complex business logic.
- Excessive use of lambda can reduce code readability.
- A large function with multiple statements is not suitable for a lambda.
18. Normal Function vs Lambda
| Normal Function | Lambda Function |
|---|---|
The |
The |
| Usually named function | Useful for anonymous functions |
| Can contain multiple statements | Designed for a single expression |
| Better for complex logic | Better for short operations |
19. Common Mistakes
# Wrong
square = lambda x:
return x * x
Normal multi-line return syntax is not used inside a lambda.
# Correct
square = lambda x: x * x
print(square(5))
Another Example:
# Avoid unnecessarily complex lambda
result = lambda x: (x * 2 + 10) if x > 5 else (x * 3 - 2)
If the logic is becoming complex, it is better to use a normal def def function.
20. Interview Points
- What is a lambda function?
- Why is a lambda called an anonymous function?
- What is the syntax of a lambda function?
- How many expressions are allowed in a lambda?
- What is the difference between a lambda and a normal function?
- How do you use a lambda with map() ?
- How do you use a lambda with filter() ?
- How is a lambda used in sorting?
CIIT Learning Point
Lambda functions are powerful tools for short and simple operations. powerful tool hain. They are especially used map(), filter(), sorted() functions ke saath frequently kiya jata hai. For complex logic, it is better to prefer readable normal functions.
CIIT Practice Tasks
-
Square:
Create a lambda function that returns the square of a given number. -
Cube:
Create a lambda function that returns the cube of a given number. -
Addition:
Create a lambda function to add two numbers. -
Even Numbers:
filter()usingfilter()and lambda to find only the even numbers in a list. -
Squares:
map()usingmap()and lambda to find the square of each number in a list. -
Student Marks:
Use lambda andfilter()to find 40 students with marks greater than or equal to 40. -
Course Fee Sorting:
Use lambda andsort()to find sort CIIT courses in ascending order based on their fees.
Summary :
A lambda function in Python is a short anonymous function that generally performs a single expression. The syntax of a lambda is lambda arguments: expression hota hai. It is commonly used for short calculations and conditions, along with map(), filter() aur sorted() functions ke saath commonly kiya jata hai.