Lambda, map, filter and reduce
Lambda, map, filter और reduce
What are Lambda Functions?
Lambda Functions means a lambda function is a small anonymous function usually used for short operations.
In real programs, this topic helps in short sorting/filtering logic. Learn the idea first, then type the program yourself and compare the output.
| Point | Details |
|---|---|
| Course Area | Advanced Python Professional concepts used to make code reusable, clean and project-ready. |
| Main Use | short sorting/filtering logic |
| Example File | lambda-functions.py |
| Practice Focus | Run, change values, and explain the output line by line. |
Why should you learn this?
- It is useful for short sorting/filtering logic.
- It connects with simple callbacks.
- It improves your ability to read, write and debug Python programs.
Important Terms
These terms are used directly in this lesson. Understand them before memorising the code.
| Term | Meaning |
|---|---|
| anonymous function | Function without a name, created using lambda. |
| lambda | lambda is an important term in this topic. |
| map | Applies a function to each item of an iterable. |
| filter | Keeps only items that satisfy a condition. |
| sort key | sort key is an important term in this topic. |
Syntax / Basic Pattern
The simple pattern is: prepare data, apply the concept, then show the result.
students = [("Aarav", 82), ("Riya", 91), ("Kabir", 76)]
students_sorted = sorted(students, key=lambda item: item[1], reverse=True)
print(students_sorted)
square = lambda x: x * x
print(square(7))Complete Example Program
students = [("Aarav", 82), ("Riya", 91), ("Kabir", 76)]
students_sorted = sorted(students, key=lambda item: item[1], reverse=True)
print(students_sorted)
square = lambda x: x * x
print(square(7))Expected Output
Program Explanation
students = [("Aarav", 82), ("Riya", 91), ("Kabir", 76)]stores a value in students.students_sorted = sorted(students, key=lambda item: item[1], reverse=True)stores a value in students_sorted.print(students_sorted)displays information or calculated result on the screen.square = lambda x: x * xstores a value in square.print(square(7))displays information or calculated result on the screen.
Where will you use it?
- Short sorting/filtering logic.
- Simple callbacks.
- One-line functions.
Common Mistakes
- Making code complex when a simple function or class is enough.
- Not handling possible errors or edge cases.
- Mixing project dependencies instead of using a virtual environment.
Practice Tasks
- Type the program in
lambda-functions.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to short sorting/filtering logic.
- Write 5 lines explaining the logic in your own words.
Summary
Lambda Functions is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.
Lambda Functions क्या है?
Lambda Functions ka matlab hai: A lambda function is a small anonymous function usually used for short operations. Simple words me, ye topic practical Python programs likhne me direct use hota hai.
Is topic ko sirf definition ke liye nahi, balki short sorting/filtering logic jaise real examples ke liye practice karein.
यह क्यों सीखना जरूरी है?
- Ye short sorting/filtering logic me kaam aata hai.
- Ye simple callbacks se bhi connected hai.
- Isse aap code ka output aur errors better samajh paate hain.
Important Terms
| Term | Meaning |
|---|---|
| anonymous function | Function without a name, created using lambda. |
| lambda | lambda is an important term in this topic. |
| map | Applies a function to each item of an iterable. |
| filter | Keeps only items that satisfy a condition. |
| sort key | sort key is an important term in this topic. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
students = [("Aarav", 82), ("Riya", 91), ("Kabir", 76)]
students_sorted = sorted(students, key=lambda item: item[1], reverse=True)
print(students_sorted)
square = lambda x: x * x
print(square(7))Complete Example Program
students = [("Aarav", 82), ("Riya", 91), ("Kabir", 76)]
students_sorted = sorted(students, key=lambda item: item[1], reverse=True)
print(students_sorted)
square = lambda x: x * x
print(square(7))Expected Output
Program Explanation
students = [("Aarav", 82), ("Riya", 91), ("Kabir", 76)]stores a value in students.students_sorted = sorted(students, key=lambda item: item[1], reverse=True)stores a value in students_sorted.print(students_sorted)displays information or calculated result on the screen.square = lambda x: x * xstores a value in square.print(square(7))displays information or calculated result on the screen.
Practical Uses
- Short sorting/filtering logic.
- Simple callbacks.
- One-line functions.
Common Mistakes
- Making code complex when a simple function or class is enough.
- Not handling possible errors or edge cases.
- Mixing project dependencies instead of using a virtual environment.
Practice Tasks
- Program ko
lambda-functions.pyfile me type karke run karein. - Values change karke output compare karein.
- short sorting/filtering logic par ek छोटा example banayen.
- Logic ko apne words me 5 lines me likhein.
सारांश
Lambda Functions ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.