0
How does this code work
3 ответов
+ 3
It is really simple actually. Think of what happens in a simple for loop, take this for example
`for elem in [1, 2, 3]:`
A loop will be started and in each iteration of the loop, an element of the array will be assigned to the variable `elem`. So first `elem` will be 1, then 2 and so on.
Similarly, here instead of `elem` there is a[1]. So in each iteration of the loop, an element of `a` will be assigned to the 1th index of the `a`. The last value a[i] will be 8, as it will be the value in the last iteration. That is why when the loop ends, a[1] is 8
+ 4
The last element of the list will be the new value of the element in the for loop:
For Example:
for a[2] in a:
print(a)
This will iterate all elements and replace the 3rd element (a[2]) and because you used "pass" keyword the last iteration where the last element is the value is the one that replaced that value in that index.
See this code:
https://code.sololearn.com/cA5A0a5A25A1
+ 1
Pass is a placeholder for code you will write later. So it does nothing and then print the list as what your code says.