Python‎ > ‎Control Flow‎ > ‎

lambda

http://www.adilwaseem.com/


Definition
Lambda execute two arguments and it ideal whenever we need to use the function objects.

Syntax
lambda x,y: "x: "operator" "y"
execution of lambda variable

Parameters
number or string

Examples
The lambda function returns with sum of two lists.
first_list=[1,3,4,5,6,7]
second_list=[1,3,5,6,7,8]
calculate=sum(map(lambda x,y: x+y,first_list, second_list))
print("Sum of two lists are :" , calculate)
>>> 
Sum of two lists are : 56
>>> 

tuple()
The method map the tuple values using lambda and for statement.
x=(1,2,3)
y=(2,1,3)
z=(1,3,4)
for i in map(lambda a,b,c: a+b+c, x,y,z):
    print(i)
>>> 
4
6
10
>>> 

map.__eq__
The method return with the equality test whether the max() of the map() is equal to pow(23, 3).
x=(11,23,444,333)
y=max(map(lambda a: a, x)).__eq__(pow(22,3))
print(y)
>>> 
False
>>>

map(lambda(function, iterable).__sizeof__()
The function returns with the str(object) in bytes.
a="python centre research"
i=(map(lambda x: x, a)).__sizeof__()
print(i)

>>> 
32
>>> 


max(map(lambda))
The determined that the maximum value of set is not equal to round(.99).
import math
tuple=(math.acos(1),math.asin(.22),math.cos(-1.1), math.atan(.99))
y=max(map(lambda a: a, tuple)).__ne__(round(1.1))
print(y)
>>> 
False
>>>

sum(map(lambda))
The function returns with the sum of three tuple.
import math
x=(math.acos(1),math.asin(1),math.atan(1))
y=(math.sin(-1),math.cos(-1),math.tan(-1))
z=(math.cosh(.1),math.sinh(.1),math.tanh(.1))
print("The sum is", sum(map(lambda a,b,c: a+b+c, x,y,z)))
>>>
The sum is 1.7024569992982896
>>>

for statement
Method run the loop for sets.
import math
set1={math.trunc(1.1),math.sqrt(49),math.pow(4,2),math.e,math.exp(44)}
set2={math.floor(3.12),math.fmod(3,2),math.fabs(22), math.ceil(1)}
for i in map(lambda a,b: a+b, set1, set2):
print(i)

>>> 
17.0
4
24.718281828459045
>>>

if statement sum(map(lamda))__lt__(value)
If statement returns that the sum of the x/y is not less than 2.
 import math
x={math.factorial(81),math.sqrt(49),math.pow(4,2),math.e,math.exp(44)}
y={math.floor(3.12),math.fmod(3,2),math.fabs(22), math.ceil(1)}
if sum(map(lambda a,b: a/b, x,y)).__lt__(2):
                                                        print("The sum of x and y is less than 2")
else:
       print("The sum of x and y is greater than 2.")
>>> 
The sum is greater than 2.
>>>

if statement sum(map(lambda).__le__(value)
The lambda returns with equation computation.
import math
x=[math.log(2,10),math.log10(22), math.log2(2)]
y=[math.trunc(2.1)]
if sum(map(lambda a,b: a+b/2, x,y)).__le__(y):
    print("The return is large or equal to 2.0")
else:
   print ("The value does not meet the criteria")

>>> 
The return is large or equal to 2.0
>>>


The method used the math values to generate random using operator.truediv()
import operator
import random
import math

a=math.acos(.65)
b=math.asin(.45)
c=math.atan(1)

d=random.betavariate(a,b)
f=random.gauss(b,c)
g=random.gammavariate(c,a)

h=operator.truediv(d,f)
i=operator.truediv(f,g)
j=operator.truediv(g,d)

list=[h,i,j]

l=max(map(lambda x:x, list))

print(l)

>>> 
426.5194858354276
>>>


for loop( starmap(Lambda))
The return is:
from itertools import *
list = [("Python", "Lake"), ("Adil", "Waseem")]
for i in starmap(lambda x,y:(x, y, x+y), list):
print( i)

>>> 
('Python', 'Lake', 'PythonLake')
('Adil', 'Waseem', 'AdilWaseem')
>>>

Comments