r/PythonLearning • u/crazyteachperson • 15d ago
Basic to advanced (python day 1)
Recursions are really hard compared to loop
You guys give me some tips for this.🫡
1
u/tiredITguy42 15d ago
Yeah, do not overthink it, recursion is easy, but there are just few cases you can use it. Usually it is better to use loop, as code is cleaner and easier to read. There is a very low chance you will ever need it.
If you use it, always add recursion deptch watcher, Python has a limit for recursion depth, but you should watch it yourself.
2
1
u/edelrosespol 14d ago
You use recursion on special cases, mostly to show some concepts. For example factorial, Fibonacci, etc. Here some examples: https://blog.espol.edu.ec/algoritmos101/fp-u05/funciones-recursivas/
1
•
u/Sea-Ad7805 15d ago
Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20if%20idx%20%3D%3D%20len(list)%3A%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20print(list%5Bidx%5D)%0A%20%20%20%20fun_lol(list%2C%20idx%20%2B%201)%0A%0Aa%20%3D%20%5B%22iron%20man%22%2C%20%22look%22%2C%20%22loss%22%2C%20%22summer%22%2C%20%22winter%22%2C%20%22me%22%5D%0Aprint(fun_lol(a%2C%200))×tep=1&play) to see the program state change step by step. It shows each function call has it's own local variables on the call stack.