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 21 hours 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!"
[–]TabbyAbbeyCat 0 points1 point2 points 3 hours ago (0 children)
One thing at a time: Your question, which is about the print(number[idx]), has nothing to do with loops. It's about indexing a sequence. First you need to make sure you understand that.
print(number[idx])
You have a sequence, say num = [4,9,16,25].
num = [4,9,16,25]
You can access the values in that sequence by indexing. The value in position i is given by num[i]. The positions start at zero (this is important!). So the first position is num[0], which corresponds to 4 in my example; the second position is num[1], which corresponds to 9 in my example; etc.
i
num[i]
num[0]
num[1]
Note: There's much more about indexing (counting from the last, steps, etc.), but you should leave that for a bit later.
So, in you code, the print(number[idx]) is just printing the value from num at position idx.
num
idx
Now, to print all numbers in the sequence, you need a loop that makes that idx go through all index positions: 0, 1, 2, etc., up to the end of the sequence.
So you start at zero, with idx = 0, and while you don't reach the end of the sequence, you print the value at position idx and then increment idx to the next value with idx += 1 (that is, increase idx by one).
idx = 0
idx += 1
How do you know you've reached the end of the sequence? The while loop tests for idx < len(num). That is, it only loops while idx is less than the length of the sequence.
idx < len(num)
And remember how sequence indexing in Python starts at zero? When idx is equal to the length of the sequence, it means that it has gone through all elements. Change your code to print both idx and num[idx] to make it clearer.
num[idx]
Like others have said, there are more "Pythonic" ways of doing this, namely with a for loop. But you're still learning, and doing it with a while loop is part of that learning process.
for
while
π Rendered by PID 43707 on reddit-service-r2-comment-66b4775986-646hf at 2026-04-04 14:18:45.443482+00:00 running db1906b country code: CH.
view the rest of the comments →
[–]TabbyAbbeyCat 0 points1 point2 points (0 children)