
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Lambda Function एक anonymous (nameless) function होता है जिसे “lambda” keyword का use करके बनाया जाता है। जब हमे कोई छोटा function (जो एक ही line में define हो जाए) बनाना होता है तो हम lambda function बनाते है।
SYNTAX
lambda arguments : expression
square = lambda x: x * x
print(square(5))
# Output: 25
जब एक lambda function के अंदर किसी दूसरे lambda function को use करते है या फिर वो lambda function किसी दूसरे function को return करता है तो उसे nested lambda function कहा जाता है।
nested = lambda x: (lambda y: x + y)
add5 = nested(5) # ab ye ek lambda return karega
print(add5(10)) # 5 + 10
# Output: 15
Lambda function में एक line का if-else condition यानि की ternary operator को use किया जा सकता है।
check = lambda x: "Even" if x % 2 == 0 else "Odd"
print(check(10))
print(check(7))
'''Output: Even
Odd
Lambda function को हम list comprehension (यानि list body में lambda function define करना) में use कर सकते है। इसका mostly use list transformation में किया जाता है, for example किसी number list के सारे items का square list बनाना हो तो हम इस technique से बना सकते है।
nums = [1, 2, 3, 4, 5]
squares = [(lambda x: x*x)(n) for n in nums]
print(squares)
# Output: [1, 4, 9, 16, 25]
IIFE (Immediately Invoked Function Expression) एक ऐसा function होता है जो बनते ही execute हो जाता है, but python में हम normal function (जो “def” keyword के use से बनते है) से IIFE function नहीं बना सकते, क्योंकि ऐसा करने पर syntax error आ जाएगा, इसीलिए हम lambda function का use करके IIFE function को create करते है।
result = (lambda x, y: x + y)(10, 20)
print(result)
# Output: 30
उम्मीद करते है कि आपको Lanbda Function अच्छे समझ मे आ गया होगा। अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...