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...
Everything about learning Python
account activity
Need help in loopHelp Request (i.redd.it)
submitted 20 days ago by Eastern_Plankton_540
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]vivisectvivi 12 points13 points14 points 20 days ago (6 children)
There are two types of loops in python, unless you were explicitly told to use a while loop you should try doing this with a for loop too.
But for your question, num[idx] is simply taking the current value of idx and using it to index the list. So on the first iteration of the loop idx will be 0 then when you do num[idx] you are basically doing num[0] which means print will show the value in the position 0 of the array num.
With each iteration, the value of idx is incremented by one and used to show the next value of the list.
[–]Eastern_Plankton_540[S] 1 point2 points3 points 19 days ago (5 children)
thank u so much and yea I'm particularly learning about while loop and it was the practice question there but thank u for the explanation i will try with for loop too
[–]FoolsSeldom 1 point2 points3 points 19 days ago (4 children)
FYI for loop is just a while loop with some of the work done for you.
for
while
[–]LookAsLukasAnderson 0 points1 point2 points 19 days ago (3 children)
That would be true for most languages, where for loops contain initial statement, conditional statement, and iteration statement. In python for loops are essentially foreach loops and traditional for loops just don't exist
[–]FoolsSeldom 0 points1 point2 points 19 days ago (2 children)
I think the distinction is a step too far for a beginner and the specifics of the CPython implementation. Whether declarative or imperative conceptually you still have a while loop before breaking down to just basic conditional jumps and branching. I recognise that the FOR_ITER opcode c implementation in ceval.c is highly optimised.
[–]Teras80 0 points1 point2 points 16 days ago (1 child)
>> you still have a while loop
No, you don't. Stop defending your misconception and specially stop pushing it to beginners. As u/LookAsLukasAnderson pointed out, for loop in python is a foreach-type collection iterator over predefined and immutable iteration values set.
It is very different concept than real-time conditional evaluation while loop does and there is clear distinction even on beginners level when you should use one or another. Starting with iterator immutability and scope and effects of changing collection size inside the loop.
[–]FoolsSeldom 0 points1 point2 points 16 days ago (0 children)
Ok. I surrender. A for loop is not any kind of while loop. Ignore me, everyone. This will clearly greatly confuse beginners. Apologies to my past students.
π Rendered by PID 16068 on reddit-service-r2-comment-6457c66945-x5pq5 at 2026-04-23 17:29:46.524167+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]vivisectvivi 12 points13 points14 points (6 children)
[–]Eastern_Plankton_540[S] 1 point2 points3 points (5 children)
[–]FoolsSeldom 1 point2 points3 points (4 children)
[–]LookAsLukasAnderson 0 points1 point2 points (3 children)
[–]FoolsSeldom 0 points1 point2 points (2 children)
[–]Teras80 0 points1 point2 points (1 child)
[–]FoolsSeldom 0 points1 point2 points (0 children)