Skip to content

Commit 2c0592e

Browse files
author
Amogh Singhal
authored
Update Python_Programming_Quiz.md
1 parent 916d396 commit 2c0592e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Python_Programming_Quiz.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,35 @@ The `with` statement in Python ensures that cleanup code is executed when workin
250250
`PYTHONPATH` is the variable that tells the __interpreter where to locate the module files imported into a program__.<br>
251251
Hence, it must include the Python source library directory and the directories containing Python source code. <br>
252252
You can manually set PYTHONPATH, but usually, the Python installer will preset it.
253+
254+
#### 16. Can you do functional programming (FP) in Python ? If yes, then list the commonly used functions to enforce FP in Python.
255+
256+
Function | Description |
257+
--- | --- |
258+
`filter()` | Filter lets us filter in some values based on conditional logic. |
259+
`map()` | Map applies a function to every element in an iterable. |
260+
`reduce()` | Reduce repeatedly reduces a sequence pair-wise until we reach a single value. |
261+
262+
#### filter()
263+
```
264+
>>> list(filter(lambda x:x>5,range(8)))
265+
# range(8) -> [0,1,2,3,4,5,6,7]
266+
# now filter all numbers greater than 5
267+
[6, 7]
268+
```
269+
#### map()
270+
```
271+
>>> list(map(lambda x:x**2,range(8)))
272+
# range(8) -> [0,1,2,3,4,5,6,7]
273+
# now map will apply function x -> x**2 to all numbers from 0 to 7
274+
[0, 1, 4, 9, 16, 25, 36, 49]
275+
```
276+
#### reduce()
277+
```
278+
>>> from functools import reduce
279+
>>> reduce(lambda x,y:x-y,[1,2,3,4,5])
280+
# step 1 : [-1,3,4,5]
281+
# step 2 : [-4,4,5]
282+
# step 3 : [-8,5]
283+
-13
284+
```

0 commit comments

Comments
 (0)