1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Here’s an example Python code to print the pattern you described:
# Define a variable to store the number of rows in the pattern num_rows = 5 # Loop through each row of the pattern for i in range(1, num_rows+1): # Print the i-th number i times for j in range(i): print(i, end=" ") # Print a newline character to move to the next row print()
In this code, we first define a variable num_rows
to store the number of rows in the pattern.
We then use a nested loop to print the pattern. The outer loop iterates over each row of the pattern, from 1 to num_rows
. The inner loop prints the i-th number (i.e., the value of the outer loop counter) i times, using the end
parameter of the print()
function to prevent it from adding a newline character after each number. Finally, we use another print()
statement without any arguments to add a newline character and move to the next row.
When you run this code, it should output the following pattern:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Note that you can change the value of num_rows
to print the pattern with a different number of rows.