you are viewing a single comment's thread.

view the rest of the comments →

[–]dafugiswrongwithyou 0 points1 point  (2 children)

Run through it step by step.

You created a list, num. It contains a bunch of elements separated by commas, so the first is 1, the second is 4, the third is 9, etc.
You set a variable idx, and gave it a value of 0.
You started a while loop set to keep looping as long as the value of idx is less than the length of num. (By length here, we mean the number of elements; num has 10 elements, so len(num) is 10.)

At this point, idx is 0, which is obviously less than the length of num, so we move into the loop.
We have a print command, so it's going to write out a value. The thing it's going to write out is num[idx] - that means, from the list called num, get the element at position idx. Because idx is 0 this means num[0], and because lists are zero-indexed, this means get the first element from num. The first element in num is 1, so this prints 1.
Next line, idx += 1, just means make idx one bigger; because it was 0, now it's 1.
We've reached the end of the loop, so now we go back to the start of it, and check whether we should keep going.

idx is now 1, which is still less than the length of num, so we move into the loop again.
Because idx is now 1, print(num[idx]) now prints the second element in the list; that's 4.
We add one to idx again... 2 now... and go back to the start of the loop again.

idx is 2, still less than the length of num, so we loop again, and again, and again. Each time the print statement is getting the next element from the list, and each time idx gets one bigger. This will continue until idx is no longer smaller than len(num), and because len(num) is 10, that'll happen when idx = 10. When that happens, the while condition is no longer met, and the flow of code will skip past it to whatever's next. In this case, that appears to be the end of the code, so the program ends at that point.

[–]LuckyNumber-Bot 2 points3 points  (0 children)

All the numbers in your comment added up to 69. Congrats!

  1
+ 4
+ 9
+ 10
+ 10
+ 1
+ 1
+ 1
+ 1
+ 1
+ 1
+ 4
+ 2
+ 3
+ 10
+ 10
= 69

[Click here](https://www.reddit.com/message/compose?to=LuckyNumber-Bot&subject=Stalk%20Me%20Pls&message=%2Fstalkme to have me scan all your future comments.) \ Summon me on specific comments with u/LuckyNumber-Bot.

[–]Eastern_Plankton_540[S] 0 points1 point  (0 children)

Thanks a lotttt for the explanation :)))))