-
Hajipur, Bihar, 844101
A lambda function is a small anonymous function in Python.
It can take any number of arguments, but has only one expression.
lambda arguments: expression
Example:
add = lambda a, b: a + b
print(add(5, 3)) # 8
Feature | lambda | def |
---|---|---|
Named | Optional | Required |
Use case | One-liner function | Multi-line logic |
Return | Implicit | Can use return keyword |
def multiplier(n):
return lambda x: x * n
double = multiplier(2)
print(double(5)) # 10
map()
nums = [1, 2, 3]
squared = list(map(lambda x: x**2, nums))
print(squared) # [1, 4, 9]
filter()
nums = [1, 2, 3, 4, 5]
even = list(filter(lambda x: x % 2 == 0, nums))
print(even) # [2, 4]
sorted()
data = [(1, 'a'), (3, 'c'), (2, 'b')]
sorted_data = sorted(data, key=lambda x: x[1])
print(sorted_data) # [(1, 'a'), (2, 'b'), (3, 'c')]
Q1. Write a Python program to create a lambda function that multiplies two numbers.
Q2. Write a Python program to create a lambda function that checks if a number is even.
Q3. Write a Python program to use a lambda function to return the cube of a number.
Q4. Write a Python program to use lambda
with map()
to double all values in a list.
Q5. Write a Python program to filter out numbers greater than 10 from a list using lambda
and filter()
.
Q6. Write a Python program to sort a list of names by length using a lambda
function.
Q7. Write a Python program to create a function that returns a lambda which triples a number.
Q8. Write a Python program to combine def
and lambda
to create dynamic multipliers, like make_multiplier(5)(2)
→ 10.
Q9. Write a Python program to use lambda
with filter()
to find multiples of 3 in a list.
Q10. Write a Python program to create a lambda function that takes 3 parameters and returns their sum.
Q1: What is a lambda function in Python?
Q2: What is the syntax of a lambda function?
Q3: How many expressions can a lambda have?
Q4: What does the lambda function return?
Q5: Can lambda be used inside another function?
Q6: What does map() do when used with lambda?
Q7: Which function filters a list using lambda?
Q8: What does sorted(data, key=lambda x: x[1]) do?
Q9: Can lambda take multiple arguments?
Q10: What does a lambda return by default?