How to print number pattern

Опубликовано: 03 Июнь 2026
на канале: skillofy_ai
42
18

This code prints a number pattern with all rows filled with the same digit (n). Here's a breakdown of how it works:

Step-by-Step Explanation:

Initialization:

n = 7 assigns the value 7 to the variable n. This value will be used to determine the number of rows and the digit to be printed.
digit = n assigns the value of n (which is 7) to the variable digit. This variable stores the digit that will be printed repeatedly in the pattern.
Outer Loop:

for k in range(n, 0, -1): This loop iterates n times, starting from n (7) and decrementing by 1 until it reaches 0. The variable k takes on the values in this range during each iteration.
Inner Loop:

for m in range(0, k): This loop iterates k times in each iteration of the outer loop. The variable m takes on values from 0 to k-1 (since the range excludes the upper limit).
Printing:

print(digit, end=' ') Inside the inner loop, the code prints the value of digit (which is 7) followed by a space (' '). The end=' ' argument prevents the cursor from moving to a new line after each print, allowing characters to be printed on the same line.
New Line:

print("\r") After the inner loop completes for a particular value of k, this statement prints a carriage return (\r) which moves the cursor to the beginning of the current line without printing a new line character. This effectively positions the cursor at the start of the next row for the next iteration of the outer loop.