Shape Rule
Column-wise fill
Fill column 1 with 1..rows, column 2 with the next block, and so on — then print each row left to right.

Program 55 prints a column-wise number triangle: fill a 2D list column by column with increasing numbers, then print row by row — a natural step after Program 54’s mirror diagonal diamond. This tutorial covers column-wise filling, row-wise printing, a live preview, worked Python examples, edge cases, and complexity.
Column-wise fill
Fill column 1 with 1..rows, column 2 with the next block, and so on — then print each row left to right.
tri[row][col]
int tri[rows+1][rows+1] stores values so fill order and print order can differ.
col outer, row inner
for col in range(1, rows + 1): for row in range(col, rows + 1): tri[row][col] = num; num += 1 — column-wise assignment.
row outer, col inner
for row in range(1, rows + 1): for col in range(1, row + 1): print(tri[row][col], end=" ") — standard triangle output.
rows = 3..9
Pick row count and draw the column-wise triangle in the browser.
Complexity
Total values = 1+2+…+n = n(n+1)/2 — classic triangular number complexity.
A column-wise number triangle fills numbers down each column first, then prints row by row — creating jumps like 2 6 and 3 7 10 instead of consecutive digits. With rows = 5, you get 1, 2 6, 3 7 10, 4 8 11 13, 5 9 12 14 15.
In Python, create a 2D list, fill with nested loops (col outer, row inner), then print with reversed nesting (row outer, col inner).
It bridges Program 54’s conditional patterns to 2D list storage — teaching fill order vs print order as separate steps.
Outer col, inner row = col..rows.
Outer row, inner col = 1..row.
Program 54 uses diagonal conditions; Program 55 uses a 2D list with column-wise filling.
Follow Program 54; continue to Program 56 next.
In short: fill tri[row][col] = num; num += 1 column-wise, then print tri[row][col] row-wise with spaces between values.
Given row count rows = 5, fill a triangle column-wise with increasing numbers, then print row-wise.
# rows = 5
//1
//2 6
//3 7 10
//4 8 11 13
//5 9 12 14 15 | Item | Type | Description |
|---|---|---|
rows | int | Triangle height — row i prints i values. |
tri[row][col] | 2D int array | 2D list storing filled values — 1-based indexing. |
num | int | Running counter incremented during column-wise fill. |
col (fill outer) | int | Column index — runs 1 to rows. |
row (fill inner) | int | Runs col..rows for each column during fill. |
| Max value | int | Largest printed number = rows*(rows+1)/2. |
create tri[rows+1][rows+1]
num = 1
for col from 1 to rows:
for row from col to rows:
tri[row][col] = num
num += 1
for row from 1 to rows:
for col from 1 to row:
print tri[row][col]
print newline | Approach | Idea | Best for |
|---|---|---|
| 2D list + column fill | Fill column-wise, print row-wise | This distinctive jump pattern |
| Row-wise fill | Standard 1, 2 3, 4 5 6 triangle | Comparison / simpler output |
| User-input rows | int(input()) | Flexible triangle size |
| Compact trace | rows = 3 on paper first | Quick dry-runs (6 cells total) |
| Fixed-width print | print(f"{val:3d}", end="") | Alignment when rows exceed 9 |
| Goal | Pattern |
|---|---|
| Declare array | tri = [[0] * (rows + 1) for _ in range(rows + 1)] |
| Fill column-wise | for col in range(1, rows + 1): for row in range(col, rows + 1): tri[row][col] = num; num += 1 |
| Print row-wise | for row in range(1, rows + 1): for col in range(1, row + 1): print(tri[row][col], end="") |
| Add spacing | if col < row: print(" ", end="") between values |
| End row | print() |
| Program 54 contrast | Program 54 uses diagonal conditions; Program 55 uses 2D list column fill |
Same column-wise triangle — three ways to set row count and trace the fill order.
rows = 5Hard-coded height for demos (15 values)
int(input())Read row count from console
rows = 36-cell triangle for paper tracing
col outerColumn-wise assignment
row outerRow-wise display
Reach for this pattern when teaching 2D lists, fill order vs print order, and triangular number sequences.
Natural follow-up after Program 54’s diamond — introduces 2D list storage and column-wise filling.
Fill in one order, print in another — a pattern used in matrices, grids, and game boards.
Total cells = n(n+1)/2 — links loops to the triangular number formula.
Compare column-wise fill with the next pattern in the series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in 2D lists, fill/print order separation, and O(n²) thinking.
Choose row count between 3 and 9 and draw the column-wise number triangle in the browser.
Three complete Python programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results.
Fill a 5-row triangle column-wise into a 2D list, then print row-wise with spaces.
rows = 5Hard-coded row count — fill with col outer and row = col..rows inner, then print with row outer and col = 1..row inner.
rows = 5
tri = [[0] * (rows + 1) for _ in range(rows + 1)]
num = 1
for col in range(1, rows + 1):
for row in range(col, rows + 1):
tri[row][col] = num
num += 1
for row in range(1, rows + 1):
for col in range(1, row + 1):
print(tri[row][col], end="")
if col < row:
print(" ", end="")
print() Column 1 fills rows 1–5 with 1–5. Column 2 fills rows 2–5 with 6–9. When printed row-wise, row 2 shows 2 6 — values from columns 1 and 2 of that row.
Read row count from the console with safe parsing.
Read rows from the console with int(input()) — reject invalid input gracefully.
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Please enter a positive integer.")
else:
if rows <= 0:
print("Please enter a positive integer.")
else:
tri = [[0] * (rows + 1) for _ in range(rows + 1)]
num = 1
for col in range(1, rows + 1):
for row in range(col, rows + 1):
tri[row][col] = num
num += 1
for row in range(1, rows + 1):
for col in range(1, row + 1):
print(tri[row][col], end="")
if col < row:
print(" ", end="")
print() Same column-fill then row-print core as Example 1; only the source of rows changes from a literal to user input.
Smaller row count for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace column fill (6 cells) before scaling to 5 rows.
rows = 3
tri = [[0] * (rows + 1) for _ in range(rows + 1)]
num = 1
for col in range(1, rows + 1):
for row in range(col, rows + 1):
tri[row][col] = num
num += 1
for row in range(1, rows + 1):
for col in range(1, row + 1):
print(tri[row][col], end="")
if col < row:
print(" ", end="")
print() Only six cells to fill — column 1 gets 1–3, column 2 gets 4–5, column 3 gets 6. Trace each assignment on paper before running rows = 5.
int tri[rows + 1][rows + 1]; — 1-based indexing for rows and columns.
for col in range(1, rows + 1): for row in range(col, rows + 1): tri[row][col] = num; num += 1.
for row in range(1, rows + 1): for col in range(1, row + 1): — print stored values with spaces.
Row 2 shows 2 6 because column 1 has 2 and column 2 has 6 at row 2 — not consecutive fill order.
Total values = n(n+1)/2 — O(n²) time, O(n²) array space.
rows = 5Trace column-wise fill assignments and the resulting row output.
| Column | Fills rows | Values assigned |
|---|---|---|
1 | 1..5 | 1, 2, 3, 4, 5 |
2 | 2..5 | 6, 7, 8, 9 |
3 | 3..5 | 10, 11, 12 |
4 | 4..5 | 13, 14 |
5 | 5 | 15 |
row | Columns printed | Row output |
|---|---|---|
1 | col 1 | 1 |
2 | col 1–2 | 2 6 |
3 | col 1–3 | 3 7 10 |
4 | col 1–4 | 4 8 11 13 |
5 | col 1–5 | 5 9 12 14 15 |
The jump from 2 to 6 on row 2 happens because column 2 was filled after column 1 — not because of a formula on the row itself.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Column-wise fill then row-wise print — two distinct loop phases.
Example: trace the fill table and row output table in the walkthrough.
Changing fill order (column vs row) completely changes the output — compare both on paper.
Example: row 5 shows all five columns: 5 9 12 14 15.
Practice print(..., end="") vs print() with multiple values per row.
Example: put print() inside the inner loop by mistake.
Total cells = n(n+1)/2 — the nth triangular number.
Example: Peak row 10 fills 55 cells — largest value is 55.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: Peak row 5 fills 15 cells — see the walkthrough table.
Pair the pattern with try/except ValueError and positive-row validation.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Swapping fill and print loop nesting without an array produces scrambled output.
Column-wise fill teaches real 2D list usage — not abstract loop drill.
Change rows, use fixed-width format, or switch to full rectangular table.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — 6 cells, output 1 / 2 4 / 3 5 6.
Small habits that keep number-pattern code clean.
Column-wise fill: for col in range(1, rows + 1): for row in range(col, rows + 1):.
Avoid using uninitialized rows when the user types letters instead of a number.
Only call print() after both inner loops finish the row.
Row-wise print: for row in range(1, rows + 1): for col in range(1, row + 1):.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly put print() inside the inner loop.
Mistakes that commonly break column-wise number triangle patterns.
Each number lands on its own line — you get a column, not a triangle.
→ Use print(tri[row][col], end=""); call print() only after the print inner loop.
Using row outer during fill instead of col gives the standard consecutive triangle.
→ Use for col in range(1, rows + 1): as the fill outer loop.
Output runs together like 2610 instead of 2 6 and 3 7 10.
→ Add if col < row: print(" ", end="") between values.
All numbers print on one long line without row breaks.
→ Add print() after both inner loops complete.
Letters or empty input leave rows unread or raise ValueError.
→ Wrap in try/except ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — the right loop does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Peak row 5 produces 9 lines — good for dry-runs.
Unchecked int(input()) raises ValueError — use try/except.
Row 9 scans 17 character positions — total work grows as O(n²).
Try these variations to lock in the pattern.
print(f"{tri[row][col]:3d}", end="") for alignmentcol outer, row = col..rows. Print: row outer, col = 1..row.print(..., end="") stays on the line; print() advances — call it after the print inner loop finishes each row.rows > 0 for interactive programs; largest value = rows*(rows+1)/2.n(n+1)/2 — fill and print each visit every cell once.Quick Takeaway: fill tri[row][col] = num; num += 1 column-wise, print tri[row][col] row-wise with spaces, then print().
| Program | Time | Extra space |
|---|---|---|
| Fill + print loops (Examples 1–3) | O(n²) | O(n²) for the array |
| Total values | n(n+1)/2 | Largest value also n(n+1)/2 |
The column-wise number triangle is a natural follow-up to Program 54: store values in a 2D list, fill column-wise, then print row-wise for the distinctive jump pattern. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 56 for the next pattern in the series.
Fill order (column first) creates the jumps — row 2 shows 2 6, not 2 3.
int tri[rows + 1][rows + 1]for col in range(1, rows + 1): for row in range(col, rows + 1): tri[row][col] = num; num += 1for row in range(1, rows + 1): for col in range(1, row + 1):if col < row: print(" ", end="")try/except for user inputprint() inside the print inner looprows = 3 dry-run before coding rows = 5Print the jump pattern the beginner-friendly way.
Fill column-wise, print row-wise
Definitiontri[row][col]
Codecol outer, row inner
Coderow outer, col inner
Logicn(n+1)/2 values
AnalysisNumbers are filled column-wise into a 2D list — column 1 gets 1..n, column 2 gets the next block, and so on — then printed row-wise. Total values = n(n+1)/2, so O(n²).
Move on to the next pattern in the Python number-pattern series.
12 people found this page helpful