KwickAcademy Python · 6 min · free
Lambda Functions
Learn Python lambda functions: anonymous one-expression functions, their syntax and limits, sorted, map and filter, and when to use def.
On screen in this lesson
A quick reminder: functions
| Function: a named block of code you can reuse |
| Made with def, then a name and parameters |
| Parameters receive the values you pass in |
| return sends a value back |
What is an anonymous function?
| Anonymous means without a name |
| Made with the keyword lambda |
| Holds just one expression |
| Returns that value by itself |
lambda syntax
| Part | Job | Example |
|---|---|---|
| lambda | the keyword | lambda |
| parameters | inputs, commas | a, b |
| colon | separates parts | : |
| expression | value returned | a + b |
Limits of lambda
| Only one expression, no multi-line body |
| No statements like return, x = 5, for or while |
| Writing return inside gives SyntaxError |
| One-line if-else is allowed: "P" if m >= 33 else "F" |
| No docstring, and errors show just <lambda> |
Where lambda helps
| Function | lambda gives | Result |
|---|---|---|
| sorted(key=) | value to sort by | sorted list |
| map() | new value | changed items |
| filter() | True or False | kept items |
When to use def instead
| The logic needs more than one line |
| You need loops, try-except or several ifs |
| You will reuse it and want a clear name |
| You are storing it in a variable anyway |
| Others must read, test and debug it |
Quick answers
What is a lambda function?
A small function with no name that holds one expression and returns its value.
Why wrap map() in list()?
map gives back a map object, not a list, so list() shows the values.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What does anonymous mean?42 sec
Why does return inside a lambda give a syntax error?41 sec
What does the lambda do in filter()?39 sec
When should you use def instead of lambda?41 secThe full lesson, in text
Hello students, welcome to Kwickprep. Do you always need a name to make a function? Not in Python. Today we will learn lambda functions, their rules and their limits. We will use them with sorted, map and filter, and see when def is better.
First, a quick reminder. A function is a block of code with a name, which you can run again and again. We make one with the def keyword, then a name, then parameters in round brackets. Parameters are names that receive the values you pass in. The return keyword sends a value back to the line that called the function.
Now, a new word. Anonymous means without a name. In Python, we make an anonymous function with the keyword lambda. A lambda holds just one expression, which is a piece of code that gives a value, like x times two. It returns that value by itself, so we never write return.
Both of these do the same job. The def version has a name, double, and a return line. The lambda version has no name at all. We wrap the lambda in round brackets, then pass five straight to it. Both print ten.
The syntax has four parts. First comes the keyword lambda. Next come the parameters, separated by commas, and there can be zero, one or many. Then a colon separates the inputs from the work. Last comes one expression, and its value is returned.
We can also store a lambda in a variable, so we can call it later. Add takes two parameters and returns a plus b, so forty plus twenty five is sixty five. Check uses greater than or equal to, which tests if marks are thirty three or more. Pause and predict. What does check of thirty three print? True, because greater than or equal to includes thirty three.
Lambda has strict limits. It can hold only one expression, so there is no body of many lines. A statement is a full instruction, like return, an assignment, a for loop or a while loop, and none are allowed. If you write return inside a lambda, Python gives a syntax error. A one line if else, called a conditional expression, is allowed, because it gives a value. A lambda has no docstring, and error messages show only the word lambda, not a helpful name.
Lambdas shine when another function needs a small function for a moment. A tuple is a fixed group of values in round brackets. Here team is a list of tuples, each holding a name and marks. Sorted makes a new sorted list. The key parameter tells sorted what to compare. Here t is each tuple, and t index one is the marks, so students are sorted by marks. Add reverse equals True inside sorted, and the topper comes first.
Map runs a function on every item of a list. Here the lambda adds five grace marks to each mark m. Map gives back a map object, which is not a list yet. So we wrap it in list to see the values. Twenty eight becomes thirty three, and every student gets five more.
Filter keeps only the items that pass a test. Here m is the marks list, x is each mark, and p is the passed list. The lambda gives True when x is thirty three or more. Filter keeps the items where the answer is True. So twenty eight is dropped, and thirty three stays.
Let us put the three together. With sorted, the lambda gives the value to sort by. With map, the lambda gives the new value for each item. With filter, the lambda gives True or False, and only True items are kept.
So when should you use def instead? Use def when the logic needs more than one line. Use def when you need loops, error handling or many decisions. Use def when you will reuse the function, because a clear name explains the job. If you are storing a lambda in a variable anyway, the Python style guide says to use def. And use def when others must read, test and debug your code.
Here is a toy train ride at a mela. Children under five ride free, children under twelve pay thirty rupees, and others pay sixty. There are three paths, so def is much clearer than a lambda. The less than sign checks if age is smaller. Pause and predict. What is the fare at exactly twelve? Sixty rupees, because twelve is not less than twelve.
Let us revise what we learned today. A lambda is a small function with no name. Its pattern is lambda, parameters, colon, expression. It can hold only one expression, with no return and no statements. It is most useful with sorted, map and filter. And when the job needs many steps or a name you will reuse, use def. Try sorting your friends by age with a lambda today.
Courses that teach this
| Course | Unit |
|---|---|
| Programming All levels Python | Functions |
| Programming All levels C++ | Templates, Exceptions and File Handling |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

