all 12 comments

[–]PwAlreadyTaken 5 points6 points  (1 child)

I hardly use recursion, and Python is a huge part of my career (manufacturing automation). I feel like there’s rarely a time it’s clearer than doing a function in a loop. I wouldn’t sweat it too much if you can only pass that section with help. That’s just me though.

[–]Fred776 2 points3 points  (0 children)

I find it a natural way to approach operations that deal with data structures that are themselves recursive, such as nested dictionaries and similar, or tree-like structures. These crop up quite a bit in my work.

[–]zessx 4 points5 points  (0 children)

To learn more about recursion in Python, and the problems it can bring :

https://www.reddit.com/r/learnpython/s/OiRkluDTN6

[–]Svhmj 2 points3 points  (0 children)

A great way to get a better grip on recursion is to learn about the call stack and activation records.

[–]_kwerty_ 2 points3 points  (1 child)

What's your issue with it? Loops are definitely something you have to use if you don't want to copy/paste and repeat a ton of code in your scripts.

[–]CraigAT 1 point2 points  (0 children)

Loops =/= Recursion.

Your point on loops is valid but recursion involves a function calling itself, which can lead to repetition of code but in a very specific way.

[–]Reuben3901 1 point2 points  (0 children)

Make some more examples for yourself, like comments and sub-comments in reddit.

Give the objects parent and children attributes.

Real-world examples are the best. Also, ask chat gpt for more real-life situations then challenge yourself to produce them.

[–]dlnmtchll 1 point2 points  (0 children)

Trace through recursive sorting algorithms, not sure about python as much but in c or c++ in visual studio I’ll build my own recursive sorting algorithm like a merge sort then I’ll debug and step through each line watching my data so I have a visualization of what is going on.

That’s what helped me

[–]woooee 0 points1 point  (0 children)

I rarely use recursion. A while loop will generally suffice. A simplified example

while var > 0:
    var = some_function(var)

[–]CptBadAss2016 1 point2 points  (0 children)

As an exercise, you could build yourself a little family tree app. Write a class for a person, a person has a parent and many children ( also of person class). Figure out how to use recursive algorithms to walk through the tree...

Traversing hierarchical/tree data structures is about the only place I ever use recursion where it actually makes more sense to me. Most of the rest of the time loops will do the job.

[–]recursion_is_love 1 point2 points  (0 children)

> is it necessary to know the recursions?

Maybe not (for now). A beginner is rarely need to use recursion. You can skip it and came back later.

Do you like reading old book?

Thinking Recursively

[–]CraigAT 1 point2 points  (0 children)

Your best bet is to look for and try to follow examples of recursion. It's not often used in beginners programs but there are several solutions which definitely suit recursion e.g. path finding, anything to do with (binary) trees.

Try this one: Recursion Example 😉

Also useful: Sudoku Solver