Lambda Functions in Python
lambda is a one-line function without a def name. Shape: lambda x: x * x. Useful as a key= in sorted, or a tiny map/filter. square = lambda n: n * n then square(5) is 25. Keep it tiny. If you need two statements, write a normal def. Complex lambda looks clever and reads badly.
Viva: “anonymous one-line function”. Then sorted(students, key=lambda s: s["marks"]). Prefer def when the logic has a name worth saying out loud.
Lambda Functions in Python — output — 5 then ['Asha', 'Neha', 'Ravi'] (or same lengths stay). add is a tiny function.
lambda a, b: a + b
│ (2, 3)
▼
5lambda with sorted key idea.