Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Number Pattern 2

Print rows of repeated numbers with Python nested loops. Students compare row and column roles and see why the same row value appears several times per line.

IntermediatePhase 2 - Level 4: Pattern PrintingExample 5 of 13
number-pattern-2.py
1# Take n
2n = int(input("Enter number of rows: "))
3
4# Print pattern
5for i in range(1, n + 1):
6 for j in range(1, i + 1):
7 print(i, end="")
8 print()

Output

Enter number of rows: 4
1
22
333
4444

In this one

  • Print a row counter
  • Repeat a value by width
  • Separate content from repetition

How it works

  1. 1

    Choose row number i.

  2. 2

    Print i once for each of i columns.

  3. 3

    End the repeated-number row.

Edge cases

What is the fourth row?

What is the fourth row?

4444.

Does the inner counter appear in the output?

Does the inner counter appear in the output?

No. The code prints i.

What's going on

The outer i is both the value and the number of repetitions. The inner loop's j counts how many copies to print but is not displayed.

For three rows, the output is 1, 22, and 333. The newline runs only after each repetition loop ends. Once i reaches two digits, character width no longer equals the number of loop iterations.

What is the fourth row? 4444.

Does the inner counter appear in the output? No. The code prints i.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why does row three contain three copies?

Pick an answer to check it.

Question 2

What is the fourth row?

Pick an answer to check it.

Questions people ask

What is the fourth row?

4444.

Does the inner counter appear in the output?

No. The code prints i.

Why does row three contain three copies?

The inner range has three values.