-1

I was surfing YouTube and found a random video of a python quiz. The question goes like this:

What will be the output of this python code:

a = []  
for i in range(10):  
    a.append(i * ++i)  
for a[i] in a:  
    print(a[i])

I paused the video, tried to figure out the output, but I couldn't, because python doesn't have any ++ operator. Then I continued the video and I understood the first for loop, but the second for loop is very tricky, as is mentioned in the video. The guy explained in the video what is happening in the second for loop, but the explanation was too brief for me to understand it clearly. I tried to run the code in VS code, trying print statements and other stuff but still no help.
So, can you please help me in understanding the second for loop, maybe a step-by-step explanation?
Here's the video link (Hindi language): https://youtu.be/HbjaAm4Bib0

  • wel simply i * ++i i equal to i*i – Leonardo Scotti Dec 11 '21 at 17:06
  • 2
    The Youtube video should be downvoted because `++i` will confuse any new python developpers – Cid Dec 11 '21 at 17:08
  • I remember a joke video about the Python `,=` operator with such examples as `x ,= [2]` – Frank Yellin Dec 11 '21 at 17:16
  • The first loop leaves `i` with a value of 9, so the second loop is simply using `a[9]` (the last element of the list) to store the loop value rather than a separate variable as normal. This has no visible effect for the first nine iterations; the final iteration prints something other than the expected 100, because that list value was repeatedly overwritten during the previous iterations. I'm not going to bother analyzing what exactly it does print, because it *doesn't matter* - this is not code you'd ever see in the real world, this is not code that you would ever write. – jasonharper Dec 11 '21 at 17:18
  • @LeonardoScotti Sorry, but I actually asked about the second for loop. – Divyanshu Tiwari Dec 11 '21 at 17:29
  • @MrGeek I need an explanation for the second for loop, not the first one. – Divyanshu Tiwari Dec 11 '21 at 17:39
  • @Cid Why would a beginner python developer head into a python quiz? – Divyanshu Tiwari Dec 11 '21 at 17:42
  • @jasonharper That's what I actually want to understand, either if it matters in the real world or not. – Divyanshu Tiwari Dec 11 '21 at 17:44

1 Answers1

1

You can run it to see the output. The code is misleading and error prone (was that a quiz on bad practices?) .

The i * ++i part seems to suggest some special additive process for the ++i but ++ is not an operator in Python and it is simply interpreted as indicating that the sign of i is unchanged twice (in short, ++ does nothing here so the code is equivalent to i*i)

The for a[i] in a: part is really bad. It uses the residual value of i (which is the last index of a in this case) and makes that item in a the iteration variable for the loop. This means that at every iteration, the last item in a will be assigned with the current item value. This will cause the last value printed to be the same as the one-before-last (thus losing the last value in a)

Alain T.
  • 40,517
  • 4
  • 31
  • 51