Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

Can anybody explain this following program? why does this program execute so many 0s and 1s at the end of this program? x = 4 for i in range(x): for i in range(x): print i x = 2 When the code executes, the shell displays the following: 0 1 2 3 0 1 0 1 0 1 Thank you in advance!!

OpenStudy (rsmith6559):

The outer loop initializes with x = 4, so it executes four times, count the zeroes in the output. The first time that the inner loop initializes with x = 4, so the first output counts 0 through 3. The very first iteration of the inner loop it sets x =2, so the next three times that the inner loop executes x = 2, outputting 0 and 1. The lesson to be learned is that with the range function, the iterator is set up before the program can fudge up the value of x.

OpenStudy (anonymous):

The program is using a nested loop. The range counters are set before FOR invocation stack. Subsequent changes to the range counters will occur when the program returns back to a point before the FOR statement. In your example the outer loop has only one FOR so the value of the range counter is set to 4, but the inner loop is executed 4 times by the outer loop. The first time the value of in the inner loop is the same as the value in the outer loop so the inner loop executes 4 times. On subsequent invocations the value of x has changed to 2, so the inner loop only executes twice and that is why you see three sets of 0 and 1.

OpenStudy (anonymous):

Yeah. Essentially ``` for i in range(x): do() ``` Does NOT work like this: ``` i = 0 while i < x: do() ``` This is what you might expect in languages with the `for i = 0 to x` or `for i = 0, i < x, i++` style loops. Instead Python's for ( `for x in y` ) works like this ``` x = y.first() while x != None: do() x = y.next() ``` The point here being that no matter what you do to `x` within the loop ( `do()` ), `y` will remain unaffected. When `y` is an expression like `range(x)` then it will be called once the loop is reached but not after each iteration. The outer `range(x)` is called only once as `range(4)`. The inner `range(x)` is called 4 times, the first time it is called as `range(4)` and the remaining three times it is called as `range(2)` Consider the following code: ``` y = range(4) for i in y: for i in y: print i # changing y to [0,1] which is range(2) if len(y) == 4: y.pop() y.pop() ``` This prints out ``` 0 1 0 1 ``` Which is what you would have expected were `y` to be updated with every iteration.

OpenStudy (anonymous):

You're just printing the loop

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!