Submitted by moazkhan on Saturday, April 11, 2020 - 10:39.

**In this tutorial you will learn:**

- Recursive Functions in Python
- Lambda Functions in Python
- Use of Recursive Functions
- Use of Lambda Functions

**Recursive Functions in Python**

These functions call themselves within the function but need a base case to stop the recursion otherwise it would call it self indefinitely. So a base case defines a condition which returns a base value of the function and it allows us to calculate the next value which intern would be used to calculate the next value until we reach to the top most call off the function.

**Example:**

print("Output Result") num = 7<br /> def factorial(num): if num == 1: return 1 else: return num * factorial(num-1) print("Factorial of 7 is: ", factorial(num))

Another example would be to calculate fibonacci series using recursion

def fibonacci(n): if n <= 1: return 1 else: return(fibonacci(n-1) + fibonacci(n-2)) print("Output Result of Fib") print(fibonacci(10), end=" “)

**Lambda Functions in Python**

There are functions that have no name and they are known as anonymous functions. In Python we can make anonymous functions as well and they are known as lambda functions. Lambda functions in Python are usually small and concise they have all properties of a normal function like we can pass arguments and get the results but we cannot give it a name.

print("Output Result") rem = lambda num1: num1 % 5 print('Remainder is', rem(24))

list = [5,2,1,0,7,9,18,22, 98] filter_lambda= filter (lambda x: x > 4, list) print(list(filter_lambda)) #it will print 5 7 9 18 22 98

list = [5,2,1,0] result = map (lambda num: num*num, list) print(list(result)) # it will print 25 4 1 0

list = [5,2,1,0,7,6] totalSum = reduce((lambda x, y: x + y), list) print (totalSum) #it will print 21 which is the result of summing all values in a list

**Usage of Recursive Functions**

Recursive functions are used when a function wants to call it self. They are useful because many times they provide a much simpler and easier to understand solution than iterative function. One good example of a recursive case is traversing directories and files.

**Usage of Lambda Functions**

We use lambda function when we want to pass function as an argument to a function or when we want a function to live for a very limited time.

- 202 views