Lambda functions are small anonymous functions in Python. They are useful when you need a simple function for a short period of time. In this practice set, you’ll learn how to create lambda functions, pass arguments, use them with built-in functions, and solve beginner-friendly Python programs. Python lambda functions practice questions with solutions help to understand this concepts.
1. Python Program to Create a Simple Lambda Function
Difficulty: Foundation
Problem Statement
Write a Python lambda function to add 10 to a given number.
Python Solution
add_ten = lambda number: number + 10
print(add_ten(15))
Sample Output
25
Explanation
A lambda function is created using the lambda keyword and returns the result automatically.
Concepts Covered
- lambda keyword
- Anonymous Function
2. Python Program to Add Two Numbers Using Lambda
Difficulty: Foundation
Problem Statement
Write a Python lambda function to add two numbers.
Python Solution
add = lambda a, b: a + b
print(add(20, 15))
Sample Output
35
Explanation
Lambda functions can accept multiple arguments just like normal functions.
Concepts Covered
- Lambda Arguments
3. Python Program to Find the Square of a Number Using Lambda
Difficulty: Foundation
Problem Statement
Write a Python lambda function to calculate the square of a number.
Python Solution
square = lambda number: number ** 2
print(square(9))
Sample Output
81
Explanation
The lambda function returns the square of the given number.
Concepts Covered
- Exponent Operator
- Lambda Function
4. Python Program to Find the Maximum of Two Numbers Using Lambda
Difficulty: Foundation
Problem Statement
Write a Python lambda function to return the larger of two numbers.
Python Solution
maximum = lambda a, b: a if a > b else b
print(maximum(25, 40))
Sample Output
40
Explanation
The conditional expression selects the greater value.
Concepts Covered
- Conditional Expression
- Lambda Function
5. Python Program to Sort a List of Numbers Using Lambda
Difficulty: Practice
Problem Statement
Write a Python program to sort a list in ascending order using a lambda function.
Python Solution
numbers = [45, 12, 78, 23, 9]
numbers.sort(key=lambda number: number)
print(numbers)
Sample Output
[9, 12, 23, 45, 78]
Explanation
The key parameter accepts a lambda function that defines the sorting rule.
Concepts Covered
- sort()
- key
- lambda
6. Python Program to Sort a List of Tuples Using Lambda
Difficulty: Practice
Problem Statement
Write a Python program to sort students by age.
Python Solution
students = [
("Amit", 22),
("Rahul", 19),
("Neha", 21)
]
students.sort(key=lambda student: student[1])
print(students)
Sample Output
[('Rahul', 19), ('Neha', 21), ('Amit', 22)]
Explanation
The lambda function uses the second value (age) as the sorting key.
Concepts Covered
- List of Tuples
- Lambda Sorting
7. Python Program to Use Lambda with map()
Difficulty: Practice
Problem Statement
Write a Python program to double every number in a list using map() and a lambda function.
Python Solution
numbers = [2, 4, 6, 8]
result = list(map(lambda number: number * 2, numbers))
print(result)
Sample Output
[4, 8, 12, 16]
Explanation
The map() function applies the lambda function to every element.
Concepts Covered
- map()
- Lambda Function
8. Python Program to Use Lambda with filter()
Difficulty: Challenge
Problem Statement
Write a Python program to filter even numbers from a list using filter() and a lambda function.
Python Solution
numbers = [10, 15, 20, 25, 30, 35]
result = list(filter(lambda number: number % 2 == 0, numbers))
print(result)
Sample Output
[10, 20, 30]
Explanation
The filter() function keeps only the elements that satisfy the condition.
Concepts Covered
- filter()
- Lambda Function
9. Python Program to Use Lambda with sorted()
Difficulty: Challenge
Problem Statement
Write a Python program to sort words based on their length.
Python Solution
words = ["Python", "AI", "Programming", "Code"]
result = sorted(words, key=lambda word: len(word))
print(result)
Sample Output
['AI', 'Code', 'Python', 'Programming']
Explanation
The sorted() function uses the lambda function to compare word lengths.
Concepts Covered
- sorted()
- len()
- Lambda Function
10. Python Program to Find the Cube of a Number Using Lambda
Difficulty: Challenge
Problem Statement
Write a Python lambda function to calculate the cube of a number.
Python Solution
cube = lambda number: number ** 3
print(cube(5))
Sample Output
125
Explanation
The lambda function returns the cube of the given number using the exponent operator.
Concepts Covered
- Lambda Function
- Exponent Operator
Chapter Summary
After completing this chapter, you have learned:
- Lambda functions
- Anonymous functions
- Lambda with arguments
- Conditional expressions
- Lambda with
sort() - Lambda with
sorted() - Lambda with
map() - Lambda with
filter() - Real-world sorting using lambda
- Writing concise Python functions
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
