+ 5

Is using recursion in python better than using for loop and while loop for getting the desired result?

Is it necessary to use recursion, does it make you look like a professional programmer ? Because I sometimes get stuck at recursion. Is there any way I can make my recursion technics strong?

27th Mar 2022, 2:51 PM
Shubhranshu Sahu
Shubhranshu Sahu - avatar
3 Answers
+ 2
It depends. Sometimes recursion can save you a lot of lines and the code looks clean. But sometimes it's not efficient (because it uses stack for computation and results in a higher space complexity) and it's better to use a loop instead. But, recursion is something you just can't ignore. It is very useful when dealing with data structures such as trees and graphs. I recommend you watch this video by the author of a book called "Think like a programmer". I am pretty sure it will take your fear of recursion away. https://youtu.be/oKndim5-G94
27th Mar 2022, 4:31 PM
Abhishek Tandon
Abhishek Tandon - avatar
27th Mar 2022, 3:10 PM
William Owens
William Owens - avatar
- 2
Avoid both if you can.
28th Mar 2022, 10:01 AM
Bob_Li
Bob_Li - avatar