-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
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.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms