Python Number Triangle Pattern (Starting from 0)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An increasing number triangle from 0 uses zero-based loops: row i prints i + 1 values of i + j — so the first cell is 0.

Remember
Rule: for i from 0 to max_n
        for j from 0 to i
          print (i + j) and a space

0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10     ← max_n = 5

Follows the triangle from 1 in Program 33; next is the right-aligned incremental triangle in Program 35.

How to Solve It

Outer loop runs i = 0..max_n. Inner loop prints j = 0..i with the formula i + j, separated by spaces.

MethodIdeaBest for
Formula i + jWhen j = 0, value equals i — row starts at its indexLearning, interviews, exams
Compare with Program 33Same shape; zero-based loops replace i + j - 1Seeing how indexing changes the start

Pseudocode

Pseudocode
for i from 0 to max_n:
    for j from 0 to i:
        print (i + j) and a space
    print newline

Cheat sheet

GoalPattern
Grow each rowfor i in range(0, max_n + 1):
Print i+1 valuesfor j in range(0, i + 1):
Start at 0print(i + j, end=" ")
Row starts at iWhen j = 0, i + j = i
End of rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print(value, end=" ")Stays on the same lineEach number on the row
print()Ends the current lineAfter the inner loop finishes

Print values without a newline, then end the row once.

Live Preview

Change the max i and the zero-based triangle updates instantly — capped at 9 for readable demos.

Whole numbers from 0 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result max = 5 · 21 numbers
0 
1 2 
2 3 4 
3 4 5 6 
4 5 6 7 8 
5 6 7 8 9 10 

Worked Walkthrough — max_n = 5

Trace how each (i, j) pair maps to i + j.

iValuesCountPrints
0010
11, 221 2
22, 3, 432 3 4
33..643 4 5 6
44..854 5 6 7 8
55..1065 6 7 8 9 10

Total numbers = 1 + 2 + … + (max_n+1) = (max_n+1)(max_n+2)/2 → O(n²).

Python Programs

Three complete programs: fixed max_n = 5, input() variant, and a compact max_n = 2 demo. Use View Output to reveal sample results.

Example 1 — Fixed max_n = 5

Hard-coded max — zero-based loops print i + j with a trailing space.

Python
for i in range(0, 6):
    for j in range(0, i + 1):
        print(i + j, end=" ")
    print()

How It Works

1. Outer grows from 0. Row i prints exactly i + 1 numbers.

2. Formula fills the cells. i + j makes row i start at i and count upward.

3. Newline once. Call bare print() only after the inner loop finishes.

Example 2 — User Input Max

Read max_n with input(), validate, then use the same formula core.

Python
try:
    max_n = int(input("Enter max i: "))
except ValueError:
    print("Please enter a non-negative integer.")
    raise SystemExit(1)

if max_n < 0:
    print("Please enter a non-negative integer.")
    raise SystemExit(1)

for i in range(0, max_n + 1):
    for j in range(0, i + 1):
        print(i + j, end=" ")
    print()

How It Works

1. Prompt and validate. Catch ValueError; reject negative values before printing.

2. Same core. i + j matches Example 1 — only max_n comes from the user.

3. Safer input tip. Cap demos for readable output:

Safer input tip
if max_n < 0 or max_n > 9:
    print("Enter a whole number from 0 to 9.")
    raise SystemExit(1)

Example 3 — Compact max_n = 2

Same structure with only three rows — easy to confirm zero-based indexing on paper.

Python
max_n = 2

for i in range(0, max_n + 1):
    for j in range(0, i + 1):
        print(i + j, end=" ")
    print()

How It Works

1. Three rows. i = 0 → 0; i = 1 → 1 2; i = 2 → 2 3 4.

2. Trace on paper. If you start at i = 1, the leading 0 disappears and the shape matches Program 33 less cleanly.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for max 5 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

i = 1

Missing zero row

Starting at i = 1 or using i + j - 1 drops the leading 0. Keep range(0, max_n + 1) with i + j.

j to max_n

Rectangle instead of triangle

Inner range(0, max_n + 1) makes every row the same width. Use range(0, i + 1).

no space

Glued numbers

Without end=" ", values run together (12). Always print a trailing space.

print() inside

Broken rows

If bare print() sits inside the inner loop, you get one number per line. Call it only after the loop.

max_n = 0

Single value

Output is just 0. A good sanity check for input validation.

input()

Catch ValueError

Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact max = 2 (Example 3)O(n²)O(1)

Row i prints i + 1 numbers. Total = 1 + 2 + … + (n+1) = (n+1)(n+2)/2 for max_n = n → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Formula: each cell is i + j — first cell is 0 when both loops start at 0.
  • Zero-based width: inner loop runs j = 0..i so row i has i + 1 numbers.
  • Break the row: values with end=" " in the inner loop; bare print() after it finishes.
  • Complexity: O(n²) time from (n+1)(n+2)/2 prints; O(1) extra space.

One line: for each i from 0 to max_n, print i + j for j = 0..i with spaces, then print().

Frequently Asked Questions

An increasing triangle from 0: for max_n=5 you get 0 / 1 2 / 2 3 4 / 3 4 5 6 / 4 5 6 7 8 / 5 6 7 8 9 10 — each value is i + j with zero-based loops.
Because the loops start at i = 0 and j = 0, so i + j = 0.
j increases from 0 to i, so i + j increases by 1 each step — producing consecutive numbers.
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
Program 34 uses the formula i + j per cell. Program 35 is a right-aligned continuous counter triangle.
print(i + j, end=" ") keeps values separated on the same row. print() ends the row.
Use try/except ValueError around int(input()) and require max_n >= 0 — see Example 2.
O(n²) for max_n = n because total prints are 1 + 2 + … + (n+1) = (n+1)(n+2)/2.

Did you know?

Each printed value is computed as i + j. With i = 0 and j = 0 the first row prints 0; row i = 2 prints 2, 3, 4 — a zero-based left-shifted increasing triangle.

Next: Right-Aligned Incremental Triangle

Continue with a right-aligned triangle that counts continuously across rows.

Program 35 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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