SlideShare a Scribd company logo
Python recursion
Recursion
Recursion is a common mathematical and programming concept. It means that a function calls itself. This
technique is used for solving large computational problem by repeatedly applying the same procedure to reduce
it to successive smaller problems.
Recursion refers to a programming technique in which a function calls itself either directly or indirectly.
Recursion can be:
1. direct recursion- if function calls itself directly from its body.
example
def fun(): def recurse() :
fun() recurse()
2. indirect recursion – if function calls another function, which calls its caller function.
example
def fun() :
recurse()
def recurse() :
fun()
Iteration vs Recursion
Iteration Recursion
It makes code longer comparatively. It makes code short and simple
It is faster than recursion. It is slower than Iteration due to
overhead of multiple functions calls.
As it do not handle multiple function
calls need not maintain stack.
It maintains stack to handle multiple
function calls.
All recursive problems can be solved
iteratively.
All iterative problems can be solved
recursively.
Every recursive function has two parts:
1. Base case/ stopping case- there can be one or more base cases. It
must have a case (if) whose result is known. ( eg. Factorial of 0 is 1,
sum of 0 and n is n, 0th power of any base is 1 and any power to the base
0 is 0). If BASE case is not provided infinite recursion will occur.
2. Recursive Case /indicative case - providing solution using recursive
call to same function.
** Discuss problems like
a^b, sum of natural n nos, n!, Fibonacci no,GCD,
binary search
# program to find a^b
def power(a,b):
if b==0:
return 1
elif a==0:
return 0
elif b==1:
return a
else:
return
a*power(a,b-1)
print(power(2,4))
def factorial_recursive(n):
# Base case: 1! = 1
if n == 1:
return 1
# Recursive case: n! = n * (n-1)!
else:
return n * factorial_recursive(n-1)
Print(factorial_recursive(n))
def factorial(n):
Fact=1
If n==1:
return fact
else:
For i in range(1,n+1):
Fact*=I
Return fact

More Related Content

What's hot (20)

PPT
Recursion
James Wong
 
PPT
5 2
FALLEE31188
 
PPT
16 subroutine
fyjordan9
 
PDF
27.2.9 lab regular expression tutorial
Freddy Buenaño
 
PPTX
Types of function call
ArijitDhali
 
PPTX
Introduction of calculus in programming
Afaq Siddiqui
 
PDF
User Defined Functions in C Language
Infinity Tech Solutions
 
DOCX
Maharishi University of Management (MSc Computer Science test questions)
Dharma Kshetri
 
PPTX
functions in C
Mehwish Mehmood
 
PPTX
Recursion | C++ | DSA
Sumit Pandey
 
PDF
Daa chapter10
B.Kirron Reddi
 
PDF
14 Jo P Feb 08
Ganesh Samarthyam
 
PPTX
Pointers in C Language
madan reddy
 
PDF
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
PDF
Pointers
Swarup Kumar Boro
 
PPT
Pointers - DataStructures
Omair Imtiaz Ansari
 
PPT
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
PPT
Pointers in c
Mohd Arif
 
PPT
Function-Definition, Need, Declaration, Definition, Arguments, Return Value
manish maurya
 
Recursion
James Wong
 
16 subroutine
fyjordan9
 
27.2.9 lab regular expression tutorial
Freddy Buenaño
 
Types of function call
ArijitDhali
 
Introduction of calculus in programming
Afaq Siddiqui
 
User Defined Functions in C Language
Infinity Tech Solutions
 
Maharishi University of Management (MSc Computer Science test questions)
Dharma Kshetri
 
functions in C
Mehwish Mehmood
 
Recursion | C++ | DSA
Sumit Pandey
 
Daa chapter10
B.Kirron Reddi
 
14 Jo P Feb 08
Ganesh Samarthyam
 
Pointers in C Language
madan reddy
 
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
Pointers - DataStructures
Omair Imtiaz Ansari
 
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
Pointers in c
Mohd Arif
 
Function-Definition, Need, Declaration, Definition, Arguments, Return Value
manish maurya
 

Similar to Python recursion (20)

PDF
Python recursion
Prof. Dr. K. Adisesha
 
PPTX
Recursive Algorithms with their types and implementation
Ahmad177077
 
PPTX
lecture4-recursion.pptx
Lizhen Shi
 
PDF
14. Recursion.pdf
VivekBhimajiyani
 
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
usha raj
 
PPTX
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
PPTX
Full_Fixed_Enhanced_Recursion_Presentation.pptx
AliAbbas574107
 
PPTX
Enhanced_Recursion_Presennnnntation.pptx
AliAbbas574107
 
PDF
Lecture 2.4 Recursion.pdf
MianSaeedAkbar1
 
PPTX
DSA. Data structure algorithm dRecursion.pptx
siddiqsid0006
 
PDF
Python Programming unit5 (1).pdf
jamvantsolanki
 
PDF
Recursion CBSE Class 12
chinthala Vijaya Kumar
 
PPTX
Recursion-in-Python for class third.pptx
raju909783
 
PPTX
Recursion is used in programming languages to use a procedure multiple times ...
najiyanasrink
 
PPT
14 recursion
Himadri Sen Gupta
 
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
PDF
Chapter 7 recursion handouts with notes
mailund
 
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
arijitghosal14
 
PDF
Recursion in Python.pdf
MayankSinghRawat6
 
PDF
6-Python-Recursion.pdf
AshishPalandurkar2
 
Python recursion
Prof. Dr. K. Adisesha
 
Recursive Algorithms with their types and implementation
Ahmad177077
 
lecture4-recursion.pptx
Lizhen Shi
 
14. Recursion.pdf
VivekBhimajiyani
 
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
usha raj
 
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
Full_Fixed_Enhanced_Recursion_Presentation.pptx
AliAbbas574107
 
Enhanced_Recursion_Presennnnntation.pptx
AliAbbas574107
 
Lecture 2.4 Recursion.pdf
MianSaeedAkbar1
 
DSA. Data structure algorithm dRecursion.pptx
siddiqsid0006
 
Python Programming unit5 (1).pdf
jamvantsolanki
 
Recursion CBSE Class 12
chinthala Vijaya Kumar
 
Recursion-in-Python for class third.pptx
raju909783
 
Recursion is used in programming languages to use a procedure multiple times ...
najiyanasrink
 
14 recursion
Himadri Sen Gupta
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Chapter 7 recursion handouts with notes
mailund
 
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
arijitghosal14
 
Recursion in Python.pdf
MayankSinghRawat6
 
6-Python-Recursion.pdf
AshishPalandurkar2
 
Ad

More from ToniyaP1 (6)

PDF
Numpy
ToniyaP1
 
PPTX
Data structures: linear lists
ToniyaP1
 
PPTX
Python library
ToniyaP1
 
PPTX
Python algorithm efficency
ToniyaP1
 
PPTX
Python data file handling
ToniyaP1
 
PPTX
Python functions
ToniyaP1
 
Numpy
ToniyaP1
 
Data structures: linear lists
ToniyaP1
 
Python library
ToniyaP1
 
Python algorithm efficency
ToniyaP1
 
Python data file handling
ToniyaP1
 
Python functions
ToniyaP1
 
Ad

Recently uploaded (20)

PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PPTX
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PPTX
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PPTX
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
ROLE OF ANTIOXIDANT IN EYE HEALTH MANAGEMENT.pptx
Subham Panja
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPT on the Development of Education in the Victorian England
Beena E S
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 

Python recursion

  • 2. Recursion Recursion is a common mathematical and programming concept. It means that a function calls itself. This technique is used for solving large computational problem by repeatedly applying the same procedure to reduce it to successive smaller problems. Recursion refers to a programming technique in which a function calls itself either directly or indirectly. Recursion can be: 1. direct recursion- if function calls itself directly from its body. example def fun(): def recurse() : fun() recurse() 2. indirect recursion – if function calls another function, which calls its caller function. example def fun() : recurse() def recurse() : fun()
  • 3. Iteration vs Recursion Iteration Recursion It makes code longer comparatively. It makes code short and simple It is faster than recursion. It is slower than Iteration due to overhead of multiple functions calls. As it do not handle multiple function calls need not maintain stack. It maintains stack to handle multiple function calls. All recursive problems can be solved iteratively. All iterative problems can be solved recursively.
  • 4. Every recursive function has two parts: 1. Base case/ stopping case- there can be one or more base cases. It must have a case (if) whose result is known. ( eg. Factorial of 0 is 1, sum of 0 and n is n, 0th power of any base is 1 and any power to the base 0 is 0). If BASE case is not provided infinite recursion will occur. 2. Recursive Case /indicative case - providing solution using recursive call to same function. ** Discuss problems like a^b, sum of natural n nos, n!, Fibonacci no,GCD, binary search
  • 5. # program to find a^b def power(a,b): if b==0: return 1 elif a==0: return 0 elif b==1: return a else: return a*power(a,b-1) print(power(2,4))
  • 6. def factorial_recursive(n): # Base case: 1! = 1 if n == 1: return 1 # Recursive case: n! = n * (n-1)! else: return n * factorial_recursive(n-1) Print(factorial_recursive(n)) def factorial(n): Fact=1 If n==1: return fact else: For i in range(1,n+1): Fact*=I Return fact