Python Anonymous / Lambda Function
In this article, you will learn about confidential activities, also known as lambda. You will learn what it is, the word and how it is used (examples).
Table of Contents
- What are lambda functions in Python?
- How to use lambda Functions in Python?
- Syntax of Lambda Function in python
- Example of Lambda Function in python
- Use of Lambda Function in python
What is the lambda function in Python?
Python, anonymous secret is a non-named name.
With the usual work described in the DEF word, Python's activities are defined using the keyword number.
Therefore, secret services are also called lambda.
How to use lambda Functions in Python?
The Lamb works in Python has the following word.
Syntax of Lambda Function in python:
lambda arguments: expression
The number plate numbers may have only one reason, but only one word. This guide is evaluated and returned to the Lamb using it whenever needed.
Example of Lambda Function in python:
Below is a sample for the lambda price.
# Program to show the use of lambda functions
double = lambda x: x * 2
# Output: 10
print(double(5))
In the first program, the x + x: 2 is a function. Here is a list of X and X * 2 are the words to be assessed earlier and later.
This job does not have a name. This technique returns the product identification number two times. Now we can call the normal work. statement in
double = lambda x: x * 2
is nearly the same as
def double(x):
return x * 2
Use of Lambda Function in python:
We use the number when we need a short time without the name.
In Python, we generally use as a discussion of high-action (techniques for other arguments). A function is used for constructions, such as filter (), map () and so on.
Examples of use filter ():
The Python filter () function performs the list of functions.
The activities are called various elements of the list and the list of new features on the back to evaluate Real activities.
The following examples use a filter () to limit even numbers even on the list.
# Program to filter out only the even items from a list
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
# Output: [4, 6, 8, 12]
print(new_list)
Examples of the use of map ()
The Python traffic map (Python) performs the functions of the list.
The activities are called various items from the list and the list of new items that have been restored to the workpiece.
The following examples use the map function () that contains all the items in the list.
# Program to double each item in a list using map()
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2 , my_list))
# Output: [2, 10, 8, 12, 16, 22, 6, 24]
print(new_list)
Comments
Post a Comment