Continuous Number Triangle Pattern in Python

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Running Counter

What You’ll Learn

The continuous number triangle uses a running counter k so digits keep increasing across rows — a natural step after the fill-with-5 pattern in Program 19. This tutorial covers the shape rule, counter logic, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.

Shape Rule

Continuous k += 1

Row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on without restarting.

Outer Loop

1..rows

for i in range(1, rows + 1) makes row i print i numbers.

Counter k

k += 1 each print

print(k, end=" "); k += 1 prints k then increments — value carries to the next row.

print end= vs print()

Same line / next line

Numbers use print(k, end=" "); k += 1; end each row with print().

Live Preview

1–15 rows

Pick a row count and draw the continuous counter triangle instantly in the browser.

O(n²)

Complexity

Total prints = rows(rows+1)/2; extra memory stays O(1).

Introduction

A continuous number triangle prints an ascending counter across rows — numbers never restart at 1 on each new line. With rows = 4, the output is 1, 2 3, 4 5 6, 7 8 9 10.

In Python you declare k = 1 once, print k += 1 in the inner loop for i iterations per row, then print() ends each row.

Why it matters?

It introduces a running counter variable — a key step after Program 19 and before jump-number patterns.

Key Highlights

Counter k

Declare k = 1 once before both loops.

k += 1

Print then increment — sequence continues across rows.

Print Then Break

print(k, end=" "); k += 1 in the inner loop; print() after.

Series Foundation

Follow Program 19; continue to Program 21 (jump number triangle).

In short: set k = 1 once, for each row i print k += 1 for i numbers, then call print().

📝 Problem & Approach

Given a positive integer rows, print a continuous number triangle: row i prints i numbers from a running counter k that starts at 1 and increments with k += 1 on every print.

Python
# rows = 4 (conceptual shape)
# 1
# 2 3
# 4 5 6
# 7 8 9 10
k = 1
for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(k, end=" ")
        k += 1
    print()

Inputs & Outputs

ItemTypeDescription
rowsintNumber of triangle lines to print (typically ≥ 1).
kintRunning counter — declared once, incremented each print.
Printed outputtextRow i has i spaced numbers — continuous sequence.

Minimal workflow

Pseudocode
k = 1
for i from 1 to rows:
    for j from 1 to i:
        print k then k += 1
    print newline

Approach comparison

ApproachIdeaBest for
Running counter k += 11, 2 3, 4 5 6, …Learning and interviews
User-input rowsn = int(input(...))Flexible console programs
Custom start kk = 10 before loopsShift the whole sequence

⚡ Quick Reference

GoalPattern
Walk each rowfor i in range(1, rows + 1)
Init counterk = 1 before both loops
Print and stepprint(k, end=" "); k += 1
End the rowprint()
Custom startk = 10 to shift sequence
User inputrows = int(input(...))

📋 k += 1 vs Custom Start vs User Input

Same continuous triangle — different ways to control the counter.

k += 1
counter

Print k then increment — sequence continues

Declare once
k = 1

Before both loops — not inside outer loop

Custom k
k = 10

Shift start value in Example 3

Learning tip
no reset

Do not reset k each row for continuous output

Context

When This Pattern Shows Up

Reach for this pattern when teaching running counters and continuous sequences inside nested loops.

  1. First lab exercise

    Natural follow-up after Program 19 — introduces a single running counter.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with input() for a flexible row count.

  4. Gateway to variants

    Compare Program 19 (fill-with-5) and Program 21 (jump number triangle) next.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Choose a row count between 1 and 15 and draw the continuous number triangle in the browser.

Try 4, 5, or 7. Larger values still work up to 15.

Live result
Press "Draw pattern".

Examples Gallery

Three complete Python programs — fixed row count, user input, and custom start value for k. Click View Output to reveal sample console results.

📚 Getting Started

Print four rows of the continuous counter triangle with k += 1.

Example 1 — Fixed rows = 4

Hard-coded height — ideal for first demos and screenshots.

Python
rows = 4
k = 1

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(k, end=" ")
        k += 1
    print()

How It Works

When i = 1, k prints once as 1. When i = 2, k prints 2 then 3. When i = 4, k runs from 7 to 10 — the counter never resets. print() after the inner loop starts the next row.

📈 User Input

Read the row count with input() instead of hard-coding 4.

Example 2 — User Input

Read rows with input() and int(); k still starts at 1.

Python
rows = int(input("Enter the number of rows: "))
k = 1

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(k, end=" ")
        k += 1
    print()

How It Works

Same nested-loop core as Example 1; only the source of rows changes. k is still declared once before the loops. Non-numeric input raises ValueError from int(input()) — wrap it in try/except in safer labs.

⚡ Custom Start

Start the counter from a value other than 1.

Example 3 — Custom Start k = 10

Shift the whole sequence by starting k at 10 instead of 1.

Python
rows = 4
k = 10

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(k, end=" ")
        k += 1
    print()

How It Works

Change only the initial value of k — the inner loop and k += 1 logic stay the same. The sequence continues from 10 instead of 1.

🧠 How the Algorithm Prints Rows

1

Set up

print is built in; use input() when reading input. Set rows and k = 1.

Setup
2

Outer loop (row width)

for i in range(1, rows + 1) makes row i print i numbers.

Row
3

Inner loop + k += 1

print(k, end=" "); k += 1 prints k then increments — value carries to the next row.

Sequence
4

New line

print() ends the row so the next outer iteration starts fresh.

Break
=

Continuous triangle complete

Total prints: rows(rows+1)/2O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — rows = 4

Trace each outer-loop value of i, the starting k, and the numbers printed on each row.

iStart kNumbers printedRow output
1111
222, 32 3
344, 5, 64 5 6
477, 8, 9, 107 8 9 10

Total number prints: 1 + 2 + 3 + 4 = 10 = 4×5/2.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change j <= i and watch the shape change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: use (i + j) % 2 for row+column parity grids.

3. Console Formatting Drills

Practice print(..., end=" ") vs row newline without complex math.

Example: put print() inside the inner loop by mistake.

4. Character Substitution

Swap digits for letters, stars, or spaced output once the loop works.

Example: use print(k, end=" ") for spaced numbers on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for n = 10 still → 55.

6. Input Validation Labs

Pair the pattern with try/except ValueError and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner Python courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: learn k = 1 before the loops first; compare with custom start in Example 3.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Name Bounds Clearly

    Declare k once before both loops — not inside the outer loop.

  2. 2. Prefer try/except ValueError

    Wrap int(input()) in try/except ValueError so bad input does not leave rows unset.

  3. 3. Keep print() Outside

    Only call print() after the inner loop finishes the row.

  4. 4. Trace k on Paper

    Write row i, start k, and each k += 1 step before coding.

  5. 5. Dry-Run One Small n

    Trace rows = 4 on paper before coding larger demos.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put print() inside the inner loop.

Common Pitfalls

Mistakes that commonly break continuous number patterns.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use print(k, end=" "); k += 1; print() only after the inner loop.

  2. 2. Declaring k Inside the Outer Loop

    k resets each row and the sequence restarts at 1.

    → Assign k = 1 once before both loops.

  3. 3. Incrementing k in the Wrong Place

    Incrementing after the row newline (or skipping k += 1) skips or duplicates numbers.

    → Print k then k += 1 inside the inner loop.

  4. 4. Forgetting the Row Break

    Omitting print() glues every number onto one endless line.

    → Always end the row after the inner loop.

  5. 5. Blind int(input())

    Letters or empty input raise ValueError with bare int(input()).

    → Catch ValueError and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single digit

Output is just 1 on one line.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Large width

Output grows as n² characters — fine for labs, noisy for huge n.

Bad input

Non-numeric input

int(input()) raises ValueError — validate with try/except first.

k inside loop

k resets each row

Declaring k inside the outer loop restarts the sequence — not continuous.

No reset

Forgot k += 1

Without k += 1, the same number prints repeatedly on each row.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Fill-with-5 triangle

  • Two inner loops with sequence + padding
  • Review Program 19

2. Jump number triangle

  • Decreasing step variable per row
  • Continue with Program 21

3. Reset k each row

  • Declare k inside outer loop
  • Compare output — rows restart at 1

4. No trailing space

  • Print space only between numbers, not after the last
  • Harder follow-up after this page

Notes

  • Triangular sum. Total prints = rows(rows+1)/2 — O(n²) for n rows.
  • print(k, end=" "); k += 1 stays on the line; print() advances — mix them carefully.
  • Validate rows > 0 for interactive programs; rows = 1 should print a single 1.
  • This page is left-aligned. Centered pyramids need leading spaces — covered later in the series.

Quick Takeaway: declare k = 1 once, print k += 1 for i numbers per row, then break the line.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
Custom start (Example 3)O(rows²)O(1)
Wrap Up

🎉 Conclusion

The continuous number triangle is a compact lesson in running counters: declare k once, print k += 1 in the inner loop, and let the sequence continue across rows. Master the fixed-rows version, then try user input and a custom start value.

Practice the three examples above, then continue to Program 21 for the jump number triangle.

Keep k outside the outer loop — use k += 1 inside print(k, end=" "); k += 1 and validate rows when reading input.

💡 Best Practices

✅ Do

  • Explain k = 1 before both loops
  • Use print(k, end=" "); k += 1 in the inner loop
  • Validate rows ≥ 1 for interactive programs
  • Wrap int(input()) in try/except ValueError before using rows
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call print() inside the inner digit loop
  • Declare k inside the outer loop
  • Forget k += 1 after each print
  • Skip the newline after each row
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this continuous pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

Declare k

Once before loops

Code
+ 03

k += 1

Print then increment

Code
04

Grow width

Row i prints i nums

Shape
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

Because k is assigned once (k = 1) before the loops and incremented with k += 1 every time a number is printed — its value carries over to the next row.
Row 1 prints 1, so k becomes 2. Row 2 prints k twice: 2, then 3 after k += 1.
k resets each row and the sequence restarts at 1 — you get a growing triangle, not a continuous counter.
print(k, end=" ") stays on the same line with a trailing space. print() ends the current line. Numbers use end=" "; the row break uses print() after the inner loop.
Total prints are 1+2+...+n = n(n+1)/2.
Yes. Set k = 10 (or any value) before the loops — the sequence continues from that start (see Example 3).
O(n²) for n rows because total digit prints equal n(n+1)/2.
Use a try/except ValueError around int(input()) or check the raw string with .isdigit() before converting so bad input does not crash the script.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you Know? 🔊

A single counter k starts at 1 and increments with k += 1 on every print — numbers continue across rows instead of restarting. Total prints still equal n(n+1)/2 for n rows.

Continue to Program 21

Move on to the jump number triangle in the Python number-pattern series.

Program 21 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful