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

Number Pattern 1

Print an increasing digit triangle with Python nested loops. Students distinguish column values from row values and predict why numbering restarts on every row.

IntermediatePhase 2 - Level 4: Pattern PrintingExample 4 of 13
number-pattern-1.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(j, end="")
8 print()

Output

Enter number of rows: 4
1
12
123
1234

In this one

  • Print a column counter
  • Restart a row sequence
  • Distinguish i from j

How it works

  1. 1

    Visit row sizes one through n.

  2. 2

    For each row, print j from one through i.

  3. 3

    Finish the line after the inner loop.

Edge cases

Does numbering continue across rows?

Does numbering continue across rows?

No. j restarts at one.

What does n = 0 do?

What does n = 0 do?

It prints no rows.

What's going on

The outer i sets the width. The inner j starts at one for every new row and prints its own value.

For three rows, the lines are 1, 12, and 123. Printing j produces an increasing sequence across a row; printing i instead would repeat the row number. Multi-digit values after nine make the visual width less regular.

Does numbering continue across rows? No. j restarts at one.

What does n = 0 do? It prints no rows.

Quiz

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

Question 1

Which variable is printed?

Pick an answer to check it.

Question 2

Does numbering continue across rows?

Pick an answer to check it.

Questions people ask

Does numbering continue across rows?

No. j restarts at one.

What does n = 0 do?

It prints no rows.

Which variable is printed?

j.