Anonymous function, Lambda in Python
We have gone halfway through the Python function, in this lesson you will learn about anonymous functions, also known as Lambda functions. What is Lambda jaw, how is Lambda function syntax and how to use Lambda function with specific example.
What is Lambda function in Python?
In Python, anonymous functions are functions defined without names. If functions are normally defined using the def
keyword, the anonymous function is defined using the lambda
keyword. For that reason, the anonymous function is also called the Lambda function.
Lambda function syntax in Python
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.
Lambda function example in Python
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.com
print(nhan_doi(10))
In this example, lambda a: a * 2
is Lambda function. a
here is the parameter and a * 2
is the expression (taking into account the calculation and returning the result). This function has no name, it returns a function object - assigned a nhan_doi.
We 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
Using Lambda in Python
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.com
print(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.com
print(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
You should read it
May be interested
- Recursive function in Pythonin the previous articles, you learned about python functions, built-in python functions, and user-defined python functions. in this article we will learn more about recursive functions in python, which call itself, as well as how to create recursive functions and examples.
- Python function parameterin the previous article we learned about the built-in python function and the user-defined python function with customizable number of parameters. you will know how to define a function using the default parameters, keyword and custom parameters in this article.
- Python functions are user definedbesides the built-in python functions, you can also define python functions yourself, these functions are called user-defined functions (python). what are the benefits of using these self-defined functions, the best way to define functions in python, we'll learn in this python lesson.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.
- Functions in Pythonwhat is python function? how is the syntax, components, and function types in python? how to create functions in python? these questions will be answered in the python lesson below.
- The loop technique in Pythonin this python lesson, you'll learn how to control the execution of a loop using loop control statements like break and continue.