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
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- 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.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.