Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

(python) In nested loops, why and how does indenting print() affect the result

Hi was just playing around and saw that these 2 codes result in two different outcomes. Obviously the outcomes going to be different but why? How do they work differently? Thanks in advance!

1st code

number = [5,2,5,2,2]

for x_count in number:

    output = ""

    for count in range(x_count):

        output = output + "x"

    print(output)

result

2nd code

number = [5,2,5,2,2]

for x_count in number:

    output = ""

    for count in range(x_count):

        output = output + "x"

        print(output)

result

question from:https://stackoverflow.com/questions/65850101/python-in-nested-loops-why-and-how-does-indenting-print-affect-the-result

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The range() function returns a sequence of numbers, starting from 0 and increments by 1, and stops BEFORE a specified number.

You just need to walk down the loops in steps to figure out what's happening:

The First Program

You have a list of integers of 5 and 2. with a total of 5 items.

number = [5,2,5,2,2]

In the first outer loop, the x_count variable holds the value of each item in the list

for x_count in number:

That means:

  • first iteration, x_count = 5
  • second iteration, x_count = 2
  • third iteration, x_count = 5 ...and so on

Next, you're creating a variable output to hold a string of characters:

output = ""

The inner loop is where it gets tricky:

for count in range(x_count):

the range function would generate a range of values from zero to x_count, remember the value of the x_count variable is dependent on the current iteration of the outer loop. so the range function argument would look like this:

  • first outer loop iteration - range(5)
  • second outer loop iteration - range(2)
  • third outer loop iteration - range(5) ...and so on

Within the inner loop, you're concatenating the string "x" to the existing value of output. And ONLY WHEN the inner loop is complete, you print the value of output (because the print statement is outside the inner loop i.e within the outer loop context). That means:

  • First inner loop iteration, value of output is "xxxxx" because the inner loop counts up to 5 (range(x_count) where x_count = 5)
  • Second inner loop iteration, value of output is "xx" because the inner loop counts up to 2 (range(x_count) where x_count = 2)

...and so on.

Printing the value of range outside the inner loop would only give you 5 strings, because the print statement is within the outer loop and the outer loop only counts up to 5 (length of the number list).

The Second Program

Everything in the first program applies except the context of the print statement. in this case, the print statement is within the inner loop context which means it only prints values when an iteration happens within the context of the inner loop. it will print the value of output in this order:

  • On first iteration of the outer loop, x_count = 5 therefore on the first iteration of the inner loop, the print statement will be called 5 times because the loop counts from 0 - 5 (range(5)),
    • for each iteration in the inner loop, concatenate a new "x" string to output.

Result:

x
xx
xxx
xxxx
xxxxx
  • On the second iteration of the outer loop, x_count = 2, therefore on the second iteration of the inner loop, the print statement will execute 2 times because the loop counts from 0 - 2 (range(2))
    • output is reset back to an empty string
    • for each iteration concatenate "x" to output

Result:

x
xx

Repeating this flow for the next 3 items in the list. you get:

range(5):

x
xx
xxx
xxxx
xxxxx

range(2):

x
xx

range(2):

x
xx

Putting it all together when both loops are complete, you have this: Result:

x
xx
xxx
xxxx
xxxxx
x
xx
x
xx
xxx
xxxx
xxxxx
x
xx
x
xx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...