+ 14
Can Someone Explain This?
I don't understand how this code works. Could you explain this? https://code.sololearn.com/cl53nXUC1LRI/?ref=app
26 Respuestas
+ 13
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[0]
2. Do code which is inside of for loop.
3. Code ends, assign the next element to var. #var = list[1]
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[1] in list, that means:
1. Assign the first element in list to list[1]. #list[1] = list[0]
2. Do code which is inside of for loop.
3. Code ends, assign the next element to list[1]. #list[1] = list[1]
And repeat 2-3
After the loop ends, which means the last element is used in the last loop, list[1] becomes list[-1].
+ 5
I will explain each iteration to understand why a[1] ends up equal to 8:
First iteration: a[1] = 1 (first element of the list)
Second iteration: a[1] = 1 (second element)
Third iteration: a[1] = 2 (third element of the list)
Last iteration: a[1] = 8 (last element of the list)
So in this exemple you are only changing the value of a[1] to be equal to every element of the list during these iterations.
I hope this can help you.
+ 4
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[1] in a:
print(a)
You don't need the pass just inspect it
+ 1
Well explained CarrieForle
0
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
0
Slick i removed for part but output has changed.
0
the point i don't understand why a[1] turns to a[-1]. (1,8,2,3,5,8)
0
Yes I am also not understand
Impervious use break u get all results opposite to it
0
Slick This question from sololearn challenge. i know how for loop works. But this a bit confused me.
0
CarrieForle thanks for the answer. I got it
0
CarrieForle but now im confused why list[5] equals to list[4] when we write list[5] instead list[1]
0
Impervious What is list[5] after fifth loop? list[4]. list[5] is assigned by list[4] after the fifth loop. The sixth loop list[5] is gonna be assigned by itself (list[5] = list[5]), which, yeah, is still the same.
0
CarrieForle wow its true. Thanks at all
0
For loop change the a[1] value in list order
0
a=[1,1,2,3,5,8]
for a[1] 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 ☺️☺️
0
CarrieForle very well explained! :)
0
that code is equivalent to below one:
a=[1,1,2,3,5,8]
for i in a:
a[1]=i
print(a)
0
for a[1] in a, that means in 2nd position change by 8 then it's pass
- 1
pass means to do nothing. why not remove the whole thing? why the for loop at all?
- 1
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