Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02f39e0

Browse files
author
Amogh Singhal
authored
Update Python_Programming_Quiz.md
1 parent 6e332b0 commit 02f39e0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎Python_Programming_Quiz.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,48 @@ The user need not to worry about memory management as __the process of allocatio
387387

388388

389389
#### 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__
390432

391433
#### 26. What are generators in Python ?
392434

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /