26 AnswersNew Answer
How a general for loop for instance, for var in list, works is like: 1. Assign the first element in list to var. #var = list 2. Do code which is inside of for loop. 3. Code ends, assign the next element to var. #var = list Repeat 2-3 until the last element is used. An important note that I mention "assign Nth element to var". And the var can be, well, anything that can be assigned. (Anything that can be placed at the left side of =) So if we says for list in list, that means: 1. Assign the first element in list to list. #list = list 2. Do code which is inside of for loop. 3. Code ends, assign the next element to list. #list = list And repeat 2-3 After the loop ends, which means the last element is used in the last loop, list becomes list[-1].
I will explain each iteration to understand why a ends up equal to 8: First iteration: a = 1 (first element of the list) Second iteration: a = 1 (second element) Third iteration: a = 2 (third element of the list) Last iteration: a = 8 (last element of the list) So in this exemple you are only changing the value of a to be equal to every element of the list during these iterations. I hope this can help you.
1. Variable "a" defined as list with 6 item inside it.. 2. You assign a loop that will fill second index item of the list until the list is done.. 3. You assign pass statement which executed but won't do anything.. Only as executable 4. You set the list into output Here some explanation: So.. This is how python for in list work List have 3 numbers, let says 1, 8, and 5 Make it as loop, but we want to use the x as POV So every single round of loop x will be filled with the next item of the list Example: 1st round ~ x filled with the first index, that 1 2nd round ~ x filled with the second index, that 8 and so.. What happen in your code here is.. You set the second index of your list as POV which always change and filled every round 1st round ~ change to 1 2nd round ~ change to himself(1) 3rd round ~ change to 2 And so until all item in the list get counted So if you want to know what happen in every round.. I make you this: a=[1,1,2,3,5,8] for a in a: print(a) You don't need the pass just inspect it
Well explained CarrieForle
The code prints the list. The for loop means nothing because it does nothing. The first and the 4th line are the only ones that do anything. make the list, print the list
Yes I am also not understand Impervious use break u get all results opposite to it
CarrieForle thanks for the answer. I got it
CarrieForle but now im confused why list equals to list when we write list instead list
Impervious What is list after fifth loop? list. list is assigned by list after the fifth loop. The sixth loop list is gonna be assigned by itself (list = list), which, yeah, is still the same.
CarrieForle wow its true. Thanks at all
For loop change the a value in list order
a=[1,1,2,3,5,8] for a in a: pass print(a) This is the code and in this code there is a variable (a) in line 1 and the value of that variable is [1,1,2,3,5,8]. And since the it is print (a) in line 4 that's why the output of this code is [1,1,2,3,5,8] I hope this answer will be helpful ☺️☺️
CarrieForle very well explained! :)
that code is equivalent to below one: a=[1,1,2,3,5,8] for i in a: a=i print(a)
for a in a, that means in 2nd position change by 8 then it's pass
Slick i removed for part but output has changed.
pass means to do nothing. why not remove the whole thing? why the for loop at all?
the point i don't understand why a turns to a[-1]. (1,8,2,3,5,8)
if you could give me the reason as to why youre doing that? try it again, but with a purpose and it will work. but with no reason, things get out of control. Don't do it unless you don't have to for n in a: print(n) will work just fine
Slick This question from sololearn challenge. i know how for loop works. But this a bit confused me.