Friday, July 3, 2020

python lambda function best practices, for Class 12


Lambda function in python
In [2]:
fn = lambda x, y, z: (x ** 2) + (y * 2) + z
In [3]:
fn(4, 5, 6)
Out[3]:
32
In [6]:
format = lambda obj, category: 'The "%s" is a "%s".' % (obj,category,)
In [7]:
format('pine tree', 'conifer')
Out[7]:
'The "pine tree" is a "conifer".'
In [10]:
si=lambda p,r,t:p*r*t/100
In [11]:
si(4,5,6)
Out[11]:
1.2
In [13]:
 a = lambda x, y: (x * 3, y * 4, (x, y))
In [14]:
a(3,4)
Out[14]:
(9, 16, (3, 4))
In [18]:
rect=lambda l,w:(l*w,2*(l+w))
In [19]:
rect(4,5)
Out[19]:
(20, 18)
In [21]:
cir=lambda r:(2*3.14*r,3.14*r*r)
In [22]:
c=cir(4)
print('perimeter is ',c[0],'area of circle is',c[1])
perimeter is  25.12 area of circle is 50.24
In [25]:
rect=lambda l,w:(l*w,2*(l+w))
r=rect(7,8)
print('Area of Rectangle is',r[0],'Perimeter is ',r[1])
Area of Rectangle is 56 Perimeter is  30
In [ ]:
 

No comments:

Post a Comment