Social Media

2/07/2019

Anonymous function (Lambda Expression)

Anonymous function is that function which has no name.
lambda keyword is used for defining anonymous function.

Script1: Write anonymous function to calculate the sum of two variables.

sm=lambda a,b: a+b
n1=int(input())
n2=int(input())
ans=sm(n1,n2)
print(ans)

Script2: Write anonymous function for calculating factorial of a given number.

f=lambda n: 1 if n==0 or n==1 else n*f(n-1)
n=int(input("Enter the number "))
ans=f(n)
print("The factorial of",n," is",ans)

Be First to Post Comment !
Post a Comment