Functional programming:It is basically programming based on functions which are present within the library of the python and how we have to use them .It has high order functions which take other functions as arguments or return them as results.
def twice(func,argu):
return func(func(argu))
def add_ten(x):
return x+10
print(twice(add_ten,10))
Lambdas
Lambda functions are called anonymous and provide the facility of creating objects of strings and integers without variable assigning. This approach is used when passing a simple function as an argument to another function.
def one(x):
return x**2+5*x+4
print(one(-4))
print((lambda x:x**2+5*x+4,(-4)))
Assigning a value
double=lambda x:x*2
print(double(7))
Map and filter:the map and filter function are used to operate on list in which map uses the function an iterable argument and returns a new iterable function
def add(x):
return x+2
num=[11,22,33,44,55]
res=list(map(add,num))
print(res)
Filter:
The function filter an iterative by removing items that does not match a function return value
num=[11,22,33,44,55]
res=list(filter(lambda x:x%2==0,num))
print(res)
Bình luận