简体   繁体   中英

Don't understand nested For Loop in Python

I don't understand how the i in the 2nd for loop of the code below works.

di = [96, 15, 33, 87]
for i in range(len(di)):
    total = di[i]
    for j in range(i+1,len(di)):
        print(i)
0
0
0
1
1
2

Why is the output 0,0,0,1,1,2. How does i in the 2nd for loop get affected from the first loop? Is there some inheritance? Pardon the newbie here.

In programming language, a variable is available for usage within a scope. When you start for loop with a new variable then it would be available for usage until you end it.

As you are beginning the journey to learn python, one of the really good practice is to read official document. https://docs.python.org/3/tutorial/controlflow.html

To help your understanding, try this:

di = [96, 15, 33, 87]
for i in range(len(di)):
    print("first loop, i =", i)
    total = di[i]
    for j in range(i+1,len(di)):
        print("second loop, j =", j)
        print("second loop, i =", i)

The i is the same in both loops. each time the outer loop runs, it then runs the inner loop until the "for" statement is complete.

len(di) is 4. So the loop

for i in range(len(di)):

will repeat 4 times. Because of the way range works (from lower bound, which is 0 by default if not specified, to 1 below the upper bound), i will be 0 in the first repetition, 1 in the second repetition, and so on. To calculate out how many objects range(x, y) generates, in this case, how often for i in range(x, y) will repeat, you can simply do number of repetitions = y - x . So in this case: len(di) - 0 (default lower bound) = 4 .

The loop

for j in range(i+1, len(di)):
    print(i)

will repeat the print(i) command len(di) - (i + 1) times. Keep in mind, i is defined by the outer loop. So, during the first loop of

for i in range(len(di)):

i equals 0 , so the print(i) command will be executed 4 - (0+1) = 3 times - it will print i(=0) 3 times. In the second loop, i equals 1, so it will be printed 2 times, and so on. So here's whats happening, formatted as code for better readability:

First outer loop: 
i = 0
total = di[i] = di[0] = 96
--> first inner loop of first outer loop:
    j = i + 1 = 1
    i is printed -> prints 0

    second inner loop of first outer loop:
    j = j+1 = 2
    i is printed -> prints 0 again

    third inner loop of first outer loop:
    j = j+1 = 3 --> since len(di) = 4, the upper bound of range(i+1, len(di)) is reached, so this is the last Repetition
    i is printed -> prints 0 again

Second outer loop:
i = 1
total = di[1] = 15
--> first inner loop of second outer loop:
    j = i+1 = 2
    i is printed -> prints 1

    second inner loop of second outer loop:
    j = i+1 = 3  -> upper bound of range reached, last repetition
    i is printed -> prints 1 again

Third outer loop:
i = 2
total = di[2] = 33
--> first inner loop of third outer loop:
    j = i+1 = 3  -> upper bound of range is reached, only Repetition
    i is printed -> prints 2

Fourth (and final) outer loop:
i = 3 -> upper bound of range(len(di)) reached, last Repetition
total = di[3] = 87
since j = i+1 = 4, the inner loop does not get executed at all (both bounds of range are equal), so 3 doesn't get printed

end of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM