Python Code Example: pattern reverse

This Python code uses nested loops to generate a specific pattern of numbers. The outer loop (for i in range(1, 9)) iterates from 1 to 8, controlling the number to be printed and the number of lines. The inner loop (for j in range(9, i, -1)) iterates in reverse from 9 down to i + 1, controlling how many times the current value of i is printed on each line. The print(i, end="") statement prints the value of i without moving to a new line, while the final print() moves to the next line after the inner loop completes. This creates a decreasing number of repeated digits, starting from 8 down to 1, on each subsequent line.

Code Example

for i in range(1, 9):
    for j in range(9, i, -1):
        print(i, end="")
    print()

Output

11111111
2222222
333333
44444
5555
666
77
8

Code Explanation

Outer Loop: for i in range(1, 9)

  1. This loop initializes i starting from 1 and increments it by 1 in each iteration.
  2. The loop continues as long as i is less than 9 (i.e., from 1 to 8 inclusive).

Inner Loop: for j in range(9, i, -1)

  1. For each value of i, the inner loop initializes j starting from 9 and decrements it by 1 in each iteration.
  2. The loop continues as long as j is greater than i.

Inner Block: print(i, end="")

  1. For each combination of i and j, this statement prints the value of i without moving to a new line (end="" ensures that the output stays on the same line).

End of Inner Loop: print()

  1. After the inner loop completes for a particular value of i, this statement prints a newline character, effectively moving the output to the next line.

Detailed Execution:

Let’s go through each iteration to see what gets printed:

  1. When i = 1:
    • Inner loop: j ranges from 9 down to 2 (since i is 1).
    • The inner block prints 1 a total of 8 times (from j=9 to j=2).
    • Output: 11111111
  2. When i = 2:
    • Inner loop: j ranges from 9 down to 3.
    • The inner block prints 2 a total of 7 times (from j=9 to j=3).
    • Output: 2222222
  3. When i = 3:
    • Inner loop: j ranges from 9 down to 4.
    • The inner block prints 3 a total of 6 times (from j=9 to j=4).
    • Output: 333333
  4. When i = 4:
    • Inner loop: j ranges from 9 down to 5.
    • The inner block prints 4 a total of 5 times (from j=9 to j=5).
    • Output: 44444
  5. When i = 5:
    • Inner loop: j ranges from 9 down to 6.
    • The inner block prints 5 a total of 4 times (from j=9 to j=6).
    • Output: 5555
  6. When i = 6:
    • Inner loop: j ranges from 9 down to 7.
    • The inner block prints 6 a total of 3 times (from j=9 to j=7).
    • Output: 666
  7. When i = 7:
    • Inner loop: j ranges from 9 down to 8.
    • The inner block prints 7 a total of 2 times (from j=9 to j=8).
    • Output: 77
  8. When i = 8:
    • Inner loop: j ranges from 9 down to 9 (which means the loop doesn’t execute because j isn’t greater than i).
    • The inner block doesn’t execute, so nothing is printed.
    • Output: (blank line)

Final Output

Putting it all together, the output of the entire code is:

11111111
2222222
333333
44444
5555
666
77

Each line corresponds to the value of i, printed multiple times based on the decreasing value of j. The number of repetitions decreases as i increases.