@@ -387,6 +387,48 @@ The user need not to worry about memory management as __the process of allocatio
387
387
388
388
389
389
#### 25. What are lambda expressions in Python ?
390
+ A lambda function is a small anonymous function.<br >
391
+ A lambda function can take any number of arguments, but can only have one expression.<br >
392
+ __ Syntax:__ ` lambda arguments : expression `
393
+
394
+ ```
395
+ A lambda function that adds 10 to the number passed in as an argument, and print the result:
396
+
397
+ x = lambda a : a + 10
398
+ print(x(5))
399
+ # prints 15
400
+ ```
401
+
402
+ - Lambda functions can take any number of arguments
403
+
404
+ ```
405
+ A lambda function that multiplies argument a with argument b and print the result:
406
+
407
+ x = lambda a, b : a * b
408
+ print(x(5, 6))
409
+ # prints 30
410
+ ```
411
+ - The power of lambda is better shown when you use them as an __ anonymous function inside another function.__
412
+
413
+ Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number:
414
+
415
+ ```
416
+ def myfunc(n):
417
+ return lambda a : a * n
418
+ ```
419
+
420
+ Use that function definition to make a function that always doubles the number you send in
421
+
422
+ ```
423
+ def myfunc(n):
424
+ return lambda a : a * n
425
+
426
+ mydoubler = myfunc(2)
427
+
428
+ print(mydoubler(11))
429
+ # prints 22
430
+ ```
431
+ Note: __ Use lambda functions when an anonymous function is required for a short period of time__
390
432
391
433
#### 26. What are generators in Python ?
392
434
0 commit comments