Table of Contents
This guide covers anonymous function, lambda in python with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
In Python, anonymous functions are functions defined without names. If functions are normally defined using thedefKeyword, the anonymous function is defined using thelambdaKeyword. For that reason, the anonymous function is also called the Lambda function.
A Lambda function in Python has the following syntax;
lambda tham_so: bieu_thuc
Lambda function can have many parameters but only 1 expression. Expressions will be evaluated and returned. Lambda jaw can be used wherever the function object is required.
This is the Lambda function example with the task of doubling the number of entries.
nhan_doi = lambda a: a * 2# Kết quả: 20 # By TipsMake.comprint(nhan_doi(10))
In this example,lambda a: a * 2Is Lambda function.aHere is the parameter anda * 2Is the expression (taking into account the calculation and returning the result). This function has no name, it returns a function object - assigned anhan_doi.You can call it as a normal function, the following command:
nhan_doi = lambda a: a * 2
Almost like:
def nhan_doi(a):return a * 2
Usually Lambda function is used when an anonymous function is needed in a short time, for example as an argument to a higher order function. Lambda function is often used with built-in Python functions such as filter () or map (), .
For example, using Lambda function with filter ():
The filter () function takes parameters as a function and a list. The function is called with all the items in the list and the new list will be returned, containing the items that the function evaluates to as True. This is an example of using the filter () function to filter out even numbers in the list.
list_goc = [10, 9, 8, 7, 6, 1, 2, 3, 4, 5]list_moi = list(filter(lambda a: (a%2 == 0) , list_goc))# Kết quả: [10, 8, 6, 2, 4] # By TipsMake.comprint(list_moi)
For example, using Lambda function with map ():
The map () function also takes parameters as a function and a list. The function is called with all the items in the new list and list returned containing the items returned by the corresponding function for each item. Saying it seems ambiguous, you see the following example will be much easier to understand:
list_goc = [10, 9, 8, 7, 6, 1, 2, 3, 4, 5]list_moi = list(map(lambda a: a*2 , list_goc))# Kết quả: [20, 18, 16, 14, 12, 2, 4, 6, 8, 10]# By TipsMake.comprint(list_moi)
Is there anything in the anonymous function that we missed? Please comment below to comment. Don't forget to do Python exercises and look forward to the next lesson.
Next lesson: Global variable (global), local variable (local), nonlocal variable in Python
Previous lesson: Python recursive function
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.