Lambda Functions in Python: trickcode
What is Lambda?
Lambda function used to create a small anonymous function without name. Lambda is a powerful python function with some limited capabilities. Lambda function is also referred to as an anonymous function . Lambda function can accept any number of elements as input but only one expression is evaluated and returned.
Why Use Lambda Function?
Lambda Function is anonymous. Anonymous means we don't need to give a name to lambda function when we are writing single-line function prefer lambda function instead of using normal functions. To write a normal function we need a def keyword to define a function and return keyword is used to return something from a function. Lambda function requires a lambda keyword to define a lambda function and don’t need any keyword for return values from lambda functions.
Syntax
Python lambda single lines
Python lambda single lines
lambda a: a+10
The above function accepts 1 variable and returns it after adding 10 into it.
Example Of Addition of Two-Variable
lambda a,b : a+b
Above function accepts 2 variables a and b and return addition
of a and b.
Example Single Inputs
Using Lambda Function
The output of the Above Both Programs
Example Multiple Inputs
Using Normal Function
Using Lambda Function
The output of the Above Both Programs
Lambda Function can be used with the Following python
built-in functions
● map
● filter
● Reduce
Note :
Lambda Function has some limited
capabilities such as only one expression can be evaluated.
What is a Map?
map() is a python
a built-in function used to apply a specific function on every element of an iterable object such as list, tuple, strings, etc . map() returns a
mapped object after applying a function to every element from an iterable object. map() can be used with a normal function or lambda function.
Syntax
map(func, *iterables)
Where
func: a normal function
or a Lambda function. Iterbales: Any iterable Object such as list, string,
tuple, etc..
Examples
We are going to practice here multiple Examples
using map() function with the help of normal user-defined function
and a lambda function.
Add 5 To every list Element (normal
function)
Add 5 To every list Element (lambda
function)
The output of the above Examples
Result of Mapped Function :<map object at 0x7ffaeb8714a8>
Original List : [1, 2, 3, 4]
After Applying Map Function(Add) : [6, 7, 8, 9]
Original List (Datatype)
type(lst[0]) : <class 'str'>
Converted List (Datatype)
type(final_result[0]) : <class 'int'>
Addition of Two numbers List
Output
Addition of list1 and list2
list1
|
1
|
2
|
3
|
4
|
list2
|
5
|
6
|
7
|
8
|
Addition
|
6
|
8
|
10
|
12
|
Reduce
Reduce() function is not
a part of python built-in function you need
to import
it from functools. In python 3 most of the developers ignore
the use of reducing () method. reduce() is a function used to
reduces a
iterable object to a single value by combining element via a supplied function.
Syntax
from functools import reduce
reduce(function, sequence[, initial])
Where
function : Any lambda or normal user-defined function. Sequence :
any sequence iterable list
Example
Consider
Lst1 = [1,2,3,4,5] Output Should be
15
Reduce Calculate the Sequence if reduce(lambda a,b :a+b,Lst1)
Res = ((((1+2)+3)+4)+5) Res= 15
Example
# Import reduce from functools from functools import reduce
Lst1 = [1,2,3,4,5]
result = reduce(lambda a,b :a+b,Lst1)
print(f"Addition of All Elements in Lst1 is : {result}")
Output
Addition of All Elements in Lst1 is: 15
Exercise
1. Write a Program using lambda to calculate the square of a given user input radius.
2.
Write a Program using lambda to multiply three elements
3.
Write a Program For Addition
of Following Lists
Lst1 = [0,1,2]
Lst2 = ["3","4","5"]
4.
Write a Program to find out numbers from range(1,121) divisible by 3
5.
Write a Program to
calculate the sum of odd numbers (1,100)
See Also :- What is Firebase? , SQL vs NoSQL or MySQL vs MongoDB,Best R vs Python for data science and Analysis
Post A Comment:
0 comments: