0% found this document useful (0 votes)
2 views2 pages

Python - Recurrsion

The document discusses recursion in Python, explaining that it is a process where a function can call itself. It provides examples of recursive functions, including calculating the factorial and generating Fibonacci numbers. The document includes code snippets and their outputs to illustrate how recursion works in practice.

Uploaded by

rudysid947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Python - Recurrsion

The document discusses recursion in Python, explaining that it is a process where a function can call itself. It provides examples of recursive functions, including calculating the factorial and generating Fibonacci numbers. The document includes code snippets and their outputs to illustrate how recursion works in practice.

Uploaded by

rudysid947
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

21/10/2024, 13:12 Python

Python - Lecture 30 : Recursion

Recursion in python
Recursion is the process of defining something in terms of itself.

Python Recursive Function


In Python, we know that a function can call other functions. It is even possible for the function to call itself.
These types of construct are termed as recursive functions.

Example:
def factorial(num):

if (num == 1 or num == 0):

return 1

else:

return (num * factorial(num - 1))

# Driver Code

num = 7;

print("Number: ",num)

print("Factorial: ",factorial(num))

Output:
number: 7

Factorial: 5040

Input:

def fibonacci(n):
if(n == 0):
return 0
elif(n == 1):
return 1
else:

https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9od334643e2116417f9a2b8d6852ecc235 1/2
21/10/2024, 13:12 Python

return (fibonacci(n - 1) + fibonacci(n - 2))

x=int(input("Enter terms : "))


for i in range(x):
print(fibonacci(i)) # Inside the loop, the fibonacci function is called with i as an
argument, and the result is printed to the console.

Ouput :

Enter terms : 8
0
1
1
2
3
5
8
13

https://notebook.zoho.in/app/index.html#/notebooks/dcr5z062df283b6d34515b57bede07611926f/notecards/9wn9od334643e2116417f9a2b8d6852ecc235 2/2

You might also like