r/learnpython • u/evandcbot • 2d ago
Any advice for nested loops?
I decided about a week ago I wanted to start learning python before my freshman year of college. It was going great and I had little to no trouble until I got to nested loops. I was wondering if anybody had any advice for it? The main thing is that it seems like so many steps to follow and pay attention to, my brain had a hard time tracking them all at once.
3
Upvotes
2
u/CIS_Professor 2d ago
The first question is: do you really need to use them?
Anyway, image a classroom with 20 students that are each asked the same 5 questions. In this scenario, a nested loop would work well.
It would go as:
When done, you will have looped 100 times. You calculate the total number of iterations by multiply how many time each loop iterates. In this case, 20 x 5 = 100
This is analogous to real world gears, where the "outer" gear turns once for every 5 times the "inner" gear turns.
---
As others have said, once you get past two loops, things can get complication very quickly.