Lambda and Recursive functions in python

By

Lambda function in python:

  • Normal functions have their names and they are defined using the def keyword.An anonymous functions do not have names and they are defined using the lambda keyword.
Syntax:-
lambda arguments:expression
  • An anonymous function defined using lambda can take any number of arguments but can return only one value.
  • Lambda functions can be used wherever function objects are required usually.It is used as an argument to other functions.
  • An anonymous function contains a small piece of code.
e.g.
# In below code,a is argument and a+20 is an expression,which gets evaluated and return to the function
m=lambda a:a+20
#printing the function object
print(m)
x=int(input("Enter the value: "))
print("Output of the lambda function is:",m(x))
Output:-
<function <lambda> at 0x00000168A74D6048>
Enter the value: 40
Output of the lambda function is: 60
>>> 
Descriptions:-
In above example, we have defined an anonymous function lambda a : a+20. Where a is an argument and a+20 is an expression .The given expression is evaluated and the result returned to the calling function.
e.g.
fun=lambda a,b,c:a+b+c
#printing the function object
print(fun)
p=int(input("Enter the value: "))
q=int(input("Enter the value: "))
r=int(input("Enter the value: "))
print("Output of the lambda function is:",fun(p,q,r))
Output:-
<function <lambda> at 0x000002A4950E6048>
Enter the value: 50
Enter the value: 40
Enter the value: 60
Output of the lambda function is: 150
>>> 
Lambda function with filter():
The python filter() function accepts a function and a list as an argument.This provides an effective way to filter out of all elements of the sequence in which the function implements to true.
e.g.
x=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
Even_values=tuple(filter(lambda a:(a%2==0),x))
print("Filered Even Numbers are: ",Even_values)
Output:-
Filered Even Numbers are:  (2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
>>> 
Lambda function with map():-
The map() function accepts a function and a list.It return new tuple/list which holds all modified items.
e.g.
p=(2,4,6,8,10,12,14,16,18,20)
cube_value=tuple(map(lambda a:a**3,p))
print("Modified lists are: ",cube_value)
Output:-
Modified lists are:  (8, 64, 216, 512, 1000, 1728, 2744, 4096, 5832, 8000)
>>> 
Sort a dictionary by values using sorted function:-
We can sort the dictionary by values using the sorted() function.The lambda function takes a dictionary(dict) item and returns a value.
e.g.
dict={1:'ram',2:'MNT LAB',3:'India',4:'Neha'}
dict1=sorted(dict.items(),key=lambda d:d[1])
print("Sorted values are ",dict1)
Output:-
Sorted values are  [(3, 'India'), (2, 'MNT LAB'), (4, 'Neha'), (1, 'ram')]
>>>
Recursive Functions in python:-

  • When a function calls itself then it is called recursive functions.
  • A function can be called from within its body,is known as recursive function.
Syntax:-
def fun():
    #statements
    #statements
     if(condition):
            #statements
     else:
        #statements
  • Generally recursive call always create an infinite loops.To avoid this infinite loop,we generally use if and else statements in our program.Any of these two block should contain the end condition logic.
  • When a function return a value then in this condition variable is die.
  • Recursive function may or may not have a return statements .  

e.g.
def fact(a):
    if(a==1):
       return 1
    else:
        return(a*fact(a-1))
n=int(input("Enter the Number:"))
print("Factorial of given number is:=",fact(n))
Output:-
Enter the Number:5
Factorial of given number is:= 120
>>> 
Descriptions:-
In above example,I have calculated factorial of given number.Here i have used recursive function to calculate the factorial value of given number as given below:-
n!=n*n-1*n-2*n-3*...1
fact(5)                                                First recursive function call
fact(5)=5*fact(4)                                second recursive function call
fact(4)=4*fact(3)                                third recursive function call
fact(3)=3*fact(2)                                fourth recursive function call
fact(2)=2*fact(1)                                fifth recursive function call
                                      
Total factorial values:
fact(5)=5*fact(4)*fact(3)*fact(2)*fact(1)
fact(5)=5*4*3*2*1
fact(5)=120
Watch complete lecture Video:-
For More....

0 comments:

Post a Comment

Powered by Blogger.