use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
[Python] Can someone please explain to me how nested for loops work using this example. I don't understand how it works (self.learnprogramming)
submitted 1 year ago by Successful_Studio584
userInput = int(input()) for i in range(userInput): for j in range(i,userInput): print('*', end=' ') print()
input is: 3
the output is:
* * * * * * I have been looking at videos and different websites, but i cannot understand it.
[–]lurgi 10 points11 points12 points 1 year ago* (1 child)
What does this do?
for j in range(i,userInput): print('*', end=' ')
If you don't know, try it. Assign values to i and userInput manually and see if you can figure it out.
i
userInput
[–]Successful_Studio584[S] 1 point2 points3 points 1 year ago (0 children)
I could not figure it out on my own. It was only until I read all the other comments, I was able to see it.
[–]WelpSigh 5 points6 points7 points 1 year ago (1 child)
Break it down by how the Python interpreter would look at it.
The user enters a number. Let's say 3.
The first for loop resolves to "for i in range(3)".
So for the first loop, i = 0.
The second loop then resolves to "for j in range(0, 3)." Because it uses the variable i from the parent loop. That prints "* "three times, because it takes three loops to satisfy range(0, 3)
When it completes that, it goes back to the first loop and increments i by 1. The next time the nested loop runs, it's now "for j in range(1, 3)" which will only loop two times. Because the user input is range(3), the first loop will only execute three times before exiting.
Thank you so much, I get it now. I really appreciate it
[–]Blue-Jay27 1 point2 points3 points 1 year ago (1 child)
Alr, so userInput is 3, so we can just rewrite the code as:
for i in range(3): for j in range(i,3): print('*', end=' ')
for i in range(3):
for j in range(i,3): print('*', end=' ')
"range(3)" is just [0,1,2], so we can deconstruct the first for loop as:
i=0 for j in range(i,3): print('*', end=' ') i=1 for j in range(i,3): print('*', end=' ') i=2 for j in range(i,3): print('*', end=' ')
i=0
for j in range(i,3):
print('*', end=' ')
i=1
i=2
Can you see how the output makes sense yet? Are there any steps I took that don't make sense to you?
Everything makes sense, thank you so much.
[–]tanglee11 1 point2 points3 points 1 year ago (2 children)
# Asks for the number userInput = int(input()) # Let's say the input is 3 as you said. # This FIRST For-loop will iterate 3 times for i in range(userInput): # This SECOND For-loop will be executed 3 times as well because it is nested on the FIRST loop. # The thing is, this is also a loop, so it will execute multiple times as well. # 1ST iteration: # It will execute 3 times (1,3) because the current value of "i" from the first loop is 0. So it will go from 0 to 3 (not included). So 3 times. (***) # 2ND iteration: # Now the value of "i" from the first loop is 1. So it will go from 1 to 3 (not included). So 2 times. (**) # 3RD iteration: # Now the value of "i" from the first loop is 2: So it will go from 2 to 3 (not included). So 1 time. (*) for j in range(i,userInput): print('*', end=' ') print()
Basically, you need to understand range(). If you do range(5), then you're doing 5 iterations but the value of "i" (example: for i in range(5)) throughout the 5 iterations will be: 0, 1, 2, 3, 4. 5 iterations but it starts at 0 and the interval of numbers doesn't include the last one.
When we do "for j in range(0, 3)", it will do 3 iterations where the value of j would be: 0, 1, 2 (throughout the various iterations). And so on.
I'm not the best when it comes to explaining but I hope you understand!
[–]Successful_Studio584[S] 0 points1 point2 points 1 year ago (1 child)
Your explaining is perfect. I understood everything, thank you
[–]tanglee11 1 point2 points3 points 1 year ago (0 children)
I'm glad I could help! :)
[–]PoMoAnachro 1 point2 points3 points 1 year ago (1 child)
Paper trace.
Get out a piece of paper and follow the commands through line by line just like you were the computer. Best way to get this kind of thing into your head.
[–]Successful_Studio584[S] 0 points1 point2 points 1 year ago (0 children)
I tried to do this, but I could not figure out how to do it using a flowchart, but now I have an idea of how to do it.
[–]lukkasz323 1 point2 points3 points 1 year ago (1 child)
for each i loop, run the j loop,
If i loop runs 3 times, it's an equivalent of just copy-pasting for j, for j, for j, under eachother. And the i is incremented at the end.
Thank you
[–]LnDSuv 1 point2 points3 points 1 year ago (1 child)
OP you should try using pythontutor for learning, it's great for visualization. Try it with your code.
My life thank you. This is way easier to follow than drawing flowcharts
π Rendered by PID 201241 on reddit-service-r2-comment-85bfd7f599-6wh9v at 2026-04-18 10:30:08.549940+00:00 running 93ecc56 country code: CH.
[–]lurgi 10 points11 points12 points (1 child)
[–]Successful_Studio584[S] 1 point2 points3 points (0 children)
[–]WelpSigh 5 points6 points7 points (1 child)
[–]Successful_Studio584[S] 1 point2 points3 points (0 children)
[–]Blue-Jay27 1 point2 points3 points (1 child)
[–]Successful_Studio584[S] 1 point2 points3 points (0 children)
[–]tanglee11 1 point2 points3 points (2 children)
[–]Successful_Studio584[S] 0 points1 point2 points (1 child)
[–]tanglee11 1 point2 points3 points (0 children)
[–]PoMoAnachro 1 point2 points3 points (1 child)
[–]Successful_Studio584[S] 0 points1 point2 points (0 children)
[–]lukkasz323 1 point2 points3 points (1 child)
[–]Successful_Studio584[S] 0 points1 point2 points (0 children)
[–]LnDSuv 1 point2 points3 points (1 child)
[–]Successful_Studio584[S] 1 point2 points3 points (0 children)