+ 2

what is the answer of this code and how.Can someone please explain !

x = 4 for j in range(x): for i in range(x): print (i) x = 2

17th May 2019, 5:35 AM
R. Krishna
2 odpowiedzi
+ 3
The range of a loop is evaluated once at the beginning of a loop. Changing the variable that defines it afterwards has no effect. So the outer loop, that runs only one time, has four iterations. The inner loop has four iterations the first time it runs, but then x is changed to 2, so the next three inner loops will have two iterations. So the first time you'll get the numbers from 0 to 3, the next three times only the numbers from 0 to 1.
17th May 2019, 7:27 AM
HonFu
HonFu - avatar
+ 2
thank you HonFu !
17th May 2019, 1:36 PM
R. Krishna