🟢 Beginner  ·  Lesson 11

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.

💡 At a Glance
PointDetails
Course AreaAdvanced Python
Professional concepts used to make code reusable, clean and project-ready.
Main Useshort sorting/filtering logic
Example Filelambda-functions.py
Practice FocusRun, 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.

TermMeaning
anonymous functionFunction without a name, created using lambda.
lambdalambda is an important term in this topic.
mapApplies a function to each item of an iterable.
filterKeeps only items that satisfy a condition.
sort keysort key is an important term in this topic.

Syntax / Basic Pattern

The simple pattern is: prepare data, apply the concept, then show the result.

Basic Pattern
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

Python – lambda-functions.py
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

[('Riya', 91), ('Aarav', 82), ('Kabir', 76)] 49

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 * x stores 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

  1. Type the program in lambda-functions.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to short sorting/filtering logic.
  4. 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

TermMeaning
anonymous functionFunction without a name, created using lambda.
lambdalambda is an important term in this topic.
mapApplies a function to each item of an iterable.
filterKeeps only items that satisfy a condition.
sort keysort key is an important term in this topic.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
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

Python – lambda-functions.py
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

[('Riya', 91), ('Aarav', 82), ('Kabir', 76)] 49

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 * x stores 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

  1. Program ko lambda-functions.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. short sorting/filtering logic par ek छोटा example banayen.
  4. 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.

← Back to Python Tutorial