Python Tutorial|Lambda Expression

* Lambda Expression *

What is Lambda Expression? :-

   In python we can define function in 2 ways

1.def  Keyword Using :-

   This Type of Function Defination we  always Use In Python. This Function  Name We Define  Properly So it is Simple to Understand That.
 
def double(x):
   return x * 2 

2.Lambda Keyword Using :-

    In Python,anonymous function is a function that is defined without any name.
  While normal functions are defined using the def keyword, in Python 
  anonymous functions are defined using the lambda keyword.
  Hence, anonymous functions are also called lambda functions.

   Syntax of Lambda Function in python

lambda arguments: expression

Why Lambda Function Is Better Than Instead def Function ?:-


Just Because The Lambda Expression provide some benefit below as follow :-

1. We can pass Lambda Function As a argument to another function
2. We can Store function In variable
3. For Small Process Like Mutiplication Between Two Number etc.. we Can perform In That      Lambda  Function
4. We doesn't have to think about the name of function

Many More..

Example of Lambda Function in python


double = lambda x: x * 2
In the above program, lambda x: x * 2is the lambda function. Here x is the argument and x * 2 is the expression that gets evaluated and returned.

This function has no name. It returns a function object which is assigned to the identifier double. We can now call it as a normal function. The statement



Use of Lambda Function in python

We use lambda functions when we require a nameless function for a short period of time.
In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions like filter(),map() etc.

Example use with filter()

The filter() function in Python takes in a function and a list as arguments.
The function is called with all the items in the list and a new list is returned which contains items for which the function evaluate to True.
Here is an example use of filter()function to filter out only even numbers from a list.
For Example Look This Code :-
# 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)
The Output Of This Code :- 
[4, 6, 8, 12]

Example use with map()

The map() function in Python takes in a function and a list.
The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item.
Here is an example use of map()function to double all the items in a list.
Example Code :-
# 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)
Output :-
[2, 10, 8, 12, 16, 22, 6, 24]

********************************************* That's all **********************************************
********************************************************************************************************
if you have any doubt in any topic related to python than tell us in comment section we see every comment.

follow our Facebook Page:- https://www.facebook.com/pythonLesson/

Thank you for Visiting keep Sharing Keep Loving Keep Supporting 

Comments

Popular posts from this blog

Python Class and Objects

exception handling in python

Fibonacci series in python