Introduction of Python
Getting Started with Python
Syntax in Python
Comment in Python
Variables in Python
Data Types in Python
Numbers in Python
Castings in Python
Strings in Python
Boolean in Python
Operators in Python
List in Python
Tuples in Python
Sets in Python
Dictionary in Python
if Else Statement in Python
While Loop in Python
For Loop in Python
Functions in Python
Lamba Function in Python
Arrays in Python
Classes and Objects in Python
Inheritance in Python
Iterators in Python
Scope in Python
Modules in Python
Dates in Python
Math in Python
JSON in Python
RegEx in Python
Try and Except in Python
User Input in Python
String Formatting in Python
Python me Lambda Function kya hai?
Python me Lambda function normal function ka chhota(small) format function hota hai. Iss Lamba function me jitne chahe utne arguments pass kiye ja sakte hain. Lambda function me sirf ek hi expression hota hai. Jaise:
lambda arguments : expression
Yaha expression me hum ek chhota sa code likhte hain jo finally pass kiye gaye arguments ke sath manipulate karke Result return kar dega.
Isse ek example se samajhte hain jaha hum ek variable ki value assign karke 10 se multiply karenge :
a = lambda x : x * 10
print(a(6))
output : 60
Yaha x ek argument hai.
‘a’ ek variable hai jaha lambda function apne result ko return karega.
print function ke call hone par ‘x’ ki value value 6 assign ho jayegi aur baad me “x * 10” ki wajah se uski value 60 ho jayegi, jo finally return bhi ho jayega.
yah to sirf ek argument ka result tha, chaliye ab kuch aur arguments ko pass karke isse example se samajhte hain.
a = lambda x, y : x * y
print(a(6, 7))
output : a = 42
b = lambda x, y : (x * y) + z
print(a(6, 7, 8))
output : b = 50
Yaha x, y arguments hain aur ‘a’ ek variable hai jaha return hui value store hogi.
print function ke call hone par ‘x’ ki value value 6 aur ‘y’ ki value 7 assign ho jayegi. jiske baad “x * y” ka operation hoga aur jo output par finally ’42’ return kar dega.
Issi tarah 3 arguments ka bhi operation perform hoga jo finally ‘b’ ki value 50 return karega.
Kyu kiya jata hai Python me Lambda function ka use?
Lamba function ek Anonymous function hota hai, Anonymous matlab jiska koi naam nhi hota hai aur jo kuch samay or temporary basis ke liye hi upyog me laya jata hai. Lamba function bahut hi asani ke sath dusre functions ke sath perform karne ki kabiliyat rakhta hai aur iska execution process bhi fast hota hai.
Isse kuch examples se samjhate hain:
def mul(n):
return lambda x : x * n
Yaha mul(n) ek function hai jaha ‘n’ ki value function ke call ke dauran assign hogi. Lamba function me iss ‘n’ ki value ‘x’ ki value (jo abhi unknown hai) se multiply karke o/p par return karega. Chaliye iss Lambda function ka use karke “n” me pass ki gai value ka 3 guna (three times) o/p generate karte hain.
def mul(n):
return lambda x : x * n
tripler = mul(3)
print(tripler(20))
Final Output : 60
Yaha print(tripler(20)) call hone par mul function ke output to return karega. Step by Step dekhte hain:
(1) tripler = mul(3) ko call karega jiske through ‘n’ ki value 3 assign ho jayegi means lamba ka expression kuch aisa dikhega – lambda x : x * 3 , jise kuch iss tarah assume kar sakte hain – tripler = lambda x : x * 3
(2) Ab print(tripler(20)) call hoga, jo x ki value ko 20 se assign kar dega aur final operation karne ke baad output ki value 60 return kar dega.