3083b631cfbce4a34dbdbf297c6c1a12c1eed819
Python/Functional Programming in Python.md
| ... | ... | @@ -0,0 +1,40 @@ |
| 1 | +https://medium.com/@pandirisiddhardha/functional-programming-in-python-7275ac39af0 |
|
| 2 | + |
|
| 3 | +# Functional Programming in Python. Functional programming is used in the… | by Pandiri Siddhardha | Medium |
|
| 4 | + |
|
| 5 | + |
|
| 6 | +[Pandiri Siddhardha](https://medium.com/@pandirisiddhardha) |
|
| 7 | + |
|
| 8 | +Functional programming is used in the production environment where a certain blocks of code is used very often during the run time. A **function** is a block of certain code which is placed under some identifier, called function name and this function is called with its name instead of block of code again and again. A function/method is defined using **“def”** keyword, input arguments are received in some variables and the output of the function is sent through the **return** keyword. Ex: `def add(a, b, c): return a+b+c here def is a function keyword, add is method name and return is used to send the output from function.` |
|
| 9 | + |
|
| 10 | +Lets look at an example of famous pattern printing method. |
|
| 11 | + |
|
| 12 | + |
|
| 13 | + |
|
| 14 | +code for the pattern printing with output |
|
| 15 | + |
|
| 16 | + |
|
| 17 | + |
|
| 18 | +output of pattern printing with size 5 |
|
| 19 | + |
|
| 20 | +**Positional Arguments and Keyword Arguments:** |
|
| 21 | + |
|
| 22 | +Arguments are the input variables in a function, if you know the number of inputs we use variables to collect. But if we do not know the number of arguments then we use positional and Keyword arguments. def testT1(\*args,\*\*kwargs) : — — — — where \*args is the positional arguments and accepts direct values, \*\*kwargs is keyword argument where it is in the form of key-value pairs. we will send those arguments when calling the function. To make some arguments as optional we will define the argument and value during the function definition itself. `def f(b, a=6,c=10)`but all the non-default arguments should be before default(a=6) Ex for positional and keyword: |
|
| 23 | + |
|
| 24 | + |
|
| 25 | + |
|
| 26 | +\*args takes a tuple of values and \*\*kwargs as dict values and hence retrieved based on them. |
|
| 27 | + |
|
| 28 | +**DOC String:** This are the comments that are added to function just below the line a function is defined. Usually doc strings are used as description of the code what your code is supposed to do, input and output. It is used as a standard coding practice such that even a new developer who uses your code after 5years must understand. The new developer may call docstring of the function using **help() or alt-tab** in jupyter to know about your method. |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + |
|
| 32 | +doc string is called using fun.\_\_doc\_\_ |
|
| 33 | + |
|
| 34 | +**Anonymous/Lambda Function**: an Anonymous function is a function with no name. These functions are defined using lambda. Ex: **m=lambda x,y: x\*y** this is a function which takes x,y as input and returns x\*y as output. Called using m(5,4). **Filter** is a keyword which filters from input based on the function given, **map** is a keyword which maps from the input based on the function and **reduce** is used to reduce input to a single value based on the function given. Examples of all the above are: |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + |
|
| 38 | +**Iterator, Iterable and Generator** : Iterable is an object which have multiple values to iterate and Iterator is an entity which is used to extract each object one by one from iterable. **First the object must be Iterable to apply iterator on it. The Iterable objects are str, list, tuple, dict, set** and we use >>`s="Siddhardha" >>i=iter(s) >>next(S) o/p:'s' where as >>next(s) o/p: str is not an iterator, str is iterable and we use iter() to make it as iterator. Usually for loop is used to convert iterable list to iterator.` |
|
| 39 | + |
|
| 40 | +> A Generator is used for a very large operations. It is done using Yield keyword. yield is used instead of return statement. The difference between the two is return is used to send the output and remove all the local variables in function where as yield is used to send temporary output without killing local variables in function. After the intermediate temporary output is sent, the program execution resumes from the point it is paused in function. |
|
| ... | ... | \ No newline at end of file |