Recursion in lambda function in python by R4R Team

Lambda function-
-In Python, anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions.

Syntax-

lambda argument:expression

Recursion-

Function call itself is called recursion.


program-

#lambda function with recursion
func=lambda x: None if x==0 else print(x,end=" ") or func(x-1)

print(func(6))

print(func(7))
print(func(5))
print(func(1))
print(func(3))


output-

6 5 4 3 2 1 None
7 6 5 4 3 2 1 None
5 4 3 2 1 None
1 None
3 2 1 None


-In this program, we create a lambda function which call itself by func(x-1) and for each value of x it print x then again call func().
-When x become 0 then this function return None otherwise call itself by x-1 value and print that x.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!