How to use RECURSIVE Function in Python? (Recursion in Python)

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ How to use Recursive Function in Python? (Recursion in Python)

In Python, you already 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.

A Recursive Function in Python, is a function that calls itself and repeats its behaviour, UNTIL some condition is met – or return a result.

def my_function():
...
my_function()


○ Advantages of Recursion in Python
– Recursive functions make the code look clean and elegant.
– A complex task can be broken down into simpler sub-problems using recursion.
– Sequence generation is easier with recursion than using some nested iteration.

○ Disadvantages of Recursion in Python
– Recursive functions are hard to debug.
– Sometimes the logic behind recursion is hard to follow through.
– Recursive calls are expensive (inefficient) as they take up a lot of memory and time.

○ Example: Recursive Function in Python

In this example, countdown() is a Recursive function in Python that calls itself, recursively, to countdown. If number is 0 or negative, it prints the word “Stop” then automatically exits this function. Otherwise, it prints number and then calls itself agin, passing “number-1” as the new argument.

def countdown(num):
if num == 0:
print('Stop')
else:
print(num)
countdown(num-1)

countdown(5)

# OUTPUT: 5
# OUTPUT: 4
# OUTPUT: 3
# OUTPUT: 2
# OUTPUT: 1
# OUTPUT: Stop


Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap