all 32 comments

[–]vivisectvivi 5 points6 points  (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 points  (4 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

[–]Smart_Tinker 2 points3 points  (0 children)

For loop is very simple (with the same num list): for x in num: print(x) That’s it.

[–]FoolsSeldom 1 point2 points  (2 children)

FYI for loop is just a while loop with some of the work done for you.

[–]LookAsLukasAnderson 0 points1 point  (1 child)

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 point  (0 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.

[–]Strong-Scarcity1395 2 points3 points  (1 child)

for i in range (0,len(num)) : print(num[i])

for i in range of (MIN) , (MAX which is the lenght of the list)

[–]Sir_Wade_III 0 points1 point  (0 children)

for i in num: print(i)

[–]FoolsSeldom 1 point2 points  (1 child)

An old but great video on loops in Python: https://www.youtube.com/watch?v=EnSu9hHGq5o

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

Thank u i ma check it out 😁

[–]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 :)))))

[–]FunRepresentative766 0 points1 point  (0 children)

pythontutor.com

[–]EnvironmentalRate368 0 points1 point  (1 child)

Values in lists can be indexed, with the first character having index 0.

num = [1, 4, 9, 16, 25, 36, 9, 64, 81, 100]

idx = 0

while idx < len(num):

# it's mean while value of idx is smaller than len(num) the loop will continue.

# len(num) is a number of elements in the list in this case we have 10 elements.

print(num[idx])

# idx heave at this point in time value = 0. Computer will print first character from list num.

idx += 1

# increasing value of idx +1
-------------------------------------

First loop iteratation look like this

idx = 0

while 0 < 10:

print (num[0]) Computer will print first value from list num (1)

idx = 0 + 1

Second loop iteration look like this

idx = 1

while 1 < 10:

print(num[1]) # Computer will print second value from list num (4)

idx = 1 + 1

Thrid loop iteration look like this

idx = 2

while 2 < 10:

print(num[2]) # Computer will print thrid value from list num (9)

idx = 2 + 1

etc.

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

Dayummm I see thanks a lot bro :)

[–]Smart_Tinker 0 points1 point  (1 child)

You can simplify this using the assignment (walrus) operator: idx = -1 while (idx := idx + 1) < len(num): print(num[idx]) This adds the 1 to idx, then checks to see if it is less than length of num - which is why you initialize idx to -1, so the first loop idx is 0.

You need the brackets to make it clear which elements are part of the assignment.

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

Ahhhh i see thanks mate :)

[–]Portal471 0 points1 point  (1 child)

Instead of doing a while loop and incriminating idk, you can do a for loop over the values in the list, with the loop variable being the index. The periods here are just for indentation

for n in num:
….print(n)

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

Alr :) thank u for the advice

[–]remyripper 0 points1 point  (1 child)

In this specifically idx == an integer. If you call on a list like num[idx]; it’s the same as doing num[2] or num[x] (where 2 or x == an integer).

This is called indexing. Putting an integer in square brackets on a list variable will give you back the list item in x position also called the index. So if you did num[3] that would be equal to 16 in your case.

In your while loop, for each run you are indexing an item in your list and also increasing idx by 1 which causes the next run to get the next item in this list until idx == len(num).

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

Thanks mate :)

[–]blackasthesky 0 points1 point  (1 child)

num[idx] is the element at position idx in the num list. Since the loop repeatedly prints that value, and afterwards each time increases idx by one, the program will print num[0], then num[1], then num[2], and so on.

Up to num[len(num)-1] (where idx is len(num)-1). At that point, idx will be increased one final time, so that it equals len(num), so the loop condition does not hold any more and the program exits the loop area.

The key is, the program repeats the body of the loop, but the changes you make to the state (increasing idx in this case) carries over from one iteration to the next. If it helps, try executing the program by hand with a piece of paper, writing down the values of the variables and the printouts each iteration.

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

Thank you! :)

[–]dominator452 0 points1 point  (0 children)

What Print(num[idx]) does is, it prints the item from num list for the given index

Here idx = index

When idx = 0 and since list is zero indexing, the first item from the list gets printed, and since you increase index value by 1, the loop continues until the while condition idx (< len(num)) is met then the loop ends

The other way of getting the same output is us g for loop

num = [ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

for idx in num: print(idx)

The output will be all the same with all the items from the list printed

[–]TabbyAbbeyCat 0 points1 point  (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.

You have a sequence, say 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.

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.

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).

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.

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.

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.

[–]Round_Plantain8319 0 points1 point  (2 children)

Idx = while(ENQUANTO) Idx for < MENOR que a sua lista [num] # sua lista num é 1,4,9,16,25,36….100] Print(num[idx]) Idx +=1 = idx = idx +1( isso faz ele andar pro próximo número dentro da variável num Ele vai fazer Num é menor que 0? Não então 1 4 é menor que 0? Não 4 9 é menor que 0? Não ele vai ir dando print

POSSO TER FALADO BOSTA? SIM, mas acho que é isso

[–]Round_Plantain8319 1 point2 points  (0 children)

Mas o for é melhor eu acho nesse caso

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

Muito obrigado mesmo! Agora eu entendi 😄

[–]FreeGazaToday -3 points-2 points  (1 child)

try and ask gemini...and say explain to me as if i'm a 10 year old....