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

PDF
Iteration, induction, and recursion
PPT
Induction
PPT
Data Structures- Part5 recursion
PDF
Iterations and Recursions
PPT
Ch10 Recursion
PPTX
Function Pointer in C
PPT
17recursion
PDF
Pointers in C language
Iteration, induction, and recursion
Induction
Data Structures- Part5 recursion
Iterations and Recursions
Ch10 Recursion
Function Pointer in C
17recursion
Pointers in C language

What's hot (20)

PPT
Recursion
PPT
PPT
16 subroutine
PDF
27.2.9 lab regular expression tutorial
PPTX
Types of function call
PPTX
Introduction of calculus in programming
PDF
User Defined Functions in C Language
DOCX
Maharishi University of Management (MSc Computer Science test questions)
PPTX
functions in C
PPTX
Recursion | C++ | DSA
PDF
Daa chapter10
PDF
14 Jo P Feb 08
PPTX
Pointers in C Language
PDF
[ITP - Lecture 13] Introduction to Pointers
PDF
PPT
Pointers - DataStructures
PPT
Lecture 18 - Pointers
PPT
Pointers in c
PPT
Function-Definition, Need, Declaration, Definition, Arguments, Return Value
Recursion
16 subroutine
27.2.9 lab regular expression tutorial
Types of function call
Introduction of calculus in programming
User Defined Functions in C Language
Maharishi University of Management (MSc Computer Science test questions)
functions in C
Recursion | C++ | DSA
Daa chapter10
14 Jo P Feb 08
Pointers in C Language
[ITP - Lecture 13] Introduction to Pointers
Pointers - DataStructures
Lecture 18 - Pointers
Pointers in c
Function-Definition, Need, Declaration, Definition, Arguments, Return Value
Ad

Similar to Python recursion (20)

PDF
Python recursion
PPTX
Recursive Algorithms with their types and implementation
PPTX
lecture4-recursion.pptx
PDF
14. Recursion.pdf
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
PPTX
Recursion and Sorting Algorithms
PPTX
Full_Fixed_Enhanced_Recursion_Presentation.pptx
PPTX
Enhanced_Recursion_Presennnnntation.pptx
PDF
Lecture 2.4 Recursion.pdf
PPTX
DSA. Data structure algorithm dRecursion.pptx
PDF
Python Programming unit5 (1).pdf
PDF
Recursion CBSE Class 12
PPTX
Recursion-in-Python for class third.pptx
PPTX
Recursion is used in programming languages to use a procedure multiple times ...
PPT
14 recursion
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
PDF
Chapter 7 recursion handouts with notes
PDF
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
PDF
Recursion in Python.pdf
PDF
6-Python-Recursion.pdf
Python recursion
Recursive Algorithms with their types and implementation
lecture4-recursion.pptx
14. Recursion.pdf
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
Recursion and Sorting Algorithms
Full_Fixed_Enhanced_Recursion_Presentation.pptx
Enhanced_Recursion_Presennnnntation.pptx
Lecture 2.4 Recursion.pdf
DSA. Data structure algorithm dRecursion.pptx
Python Programming unit5 (1).pdf
Recursion CBSE Class 12
Recursion-in-Python for class third.pptx
Recursion is used in programming languages to use a procedure multiple times ...
14 recursion
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
Chapter 7 recursion handouts with notes
12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf
Recursion in Python.pdf
6-Python-Recursion.pdf
Ad

More from ToniyaP1 (6)

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

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
RMMM.pdf make it easy to upload and study
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
GDM (1) (1).pptx small presentation for students
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Computing-Curriculum for Schools in Ghana
Renaissance Architecture: A Journey from Faith to Humanism
O5-L3 Freight Transport Ops (International) V1.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Basic Mud Logging Guide for educational purpose
Final Presentation General Medicine 03-08-2024.pptx
Pharma ospi slides which help in ospi learning
VCE English Exam - Section C Student Revision Booklet
Anesthesia in Laparoscopic Surgery in India
RMMM.pdf make it easy to upload and study
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPH.pptx obstetrics and gynecology in nursing
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Classroom Observation Tools for Teachers
GDM (1) (1).pptx small presentation for students
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Computing-Curriculum for Schools in Ghana

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