Python Alternating Number Triangle Pattern (Zigzag)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An alternating zigzag number triangle shrinks from rows to 1. Odd lengths print 1..i; even lengths print i..1.
Remember
Rule: for i = n..1, if i odd print 1..i else print i..1
12345
4321
123
21
1 ← 5 rows
In Python you solve it with a descending outer loop plus if/else on i % 2: digits use print(j, end=""), then bare print() ends the row. Program 12 repeats one digit per row; this pattern prints a sequence and flips direction.
Approach
How to Solve It
Outer loop counts down from rows to 1. If i is odd, print 1..i; if even, print i..1. End each row with print().
Method
Idea
Best for
if / else loops
Parity picks ascending or descending inner loop
Learning, interviews, exams
start / stop / step
One inner range driven by three bounds
Leaner demos once parity clicks
Pseudocode
Pseudocode
for i from rows down to 1:
if i is even:
for j from i down to 1: print j
else:
for j from 1 to i: print j
print newline
Cheat sheet
Goal
Pattern
Shrink row length
for i in range(rows, 0, -1):
Odd length (ascend)
for j in range(1, i + 1): print(j, end="")
Even length (descend)
for j in range(i, 0, -1): print(j, end="")
Pick direction
if i % 2 == 0: ... else: ...
End the row
print()
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(j, end="")
Stays on the same line
Each digit in the sequence
print()
Ends the current line
After the inner loop
Print all digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the zigzag triangle updates instantly — capped at 9 so every digit stays a single character.
Whole numbers from 1 to 9 (single digits). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
12345
4321
123
21
1
Trace
Worked Walkthrough
Trace rows = 5 — each row length i, its parity, the inner range, and the printed row.
i
Parity
Inner j
Printed row
5
odd
1..5
12345
4
even
4..1
4321
3
odd
1..3
123
2
even
2..1
21
1
odd
1..1
1
Row length is always i; only the travel direction of j changes with parity. Total digits: n(n+1)/2.
Code
Python Programs
Three complete programs: fixed height, input(), and a start/stop/step one-loop form. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop shrinks the length; if/else picks ascending or descending digits.
Python
rows = 5
for i in range(rows, 0, -1):
if i % 2 == 0:
for j in range(i, 0, -1):
print(j, end="")
else:
for j in range(1, i + 1):
print(j, end="")
print()
Output
12345
4321
123
21
1
How It Works
1. Outer shrinks the length.i starts at rows and moves toward 1 — that is how many digits each row gets.
2. Parity picks the direction. Even i prints i..1; odd i prints 1..i.
3. End the row. Call bare print() only after the chosen inner loop finishes.
Example 2 — User Input
Read the row count from the keyboard. Prefer a try / except in real apps (shown in the tip below).
Python
rows = int(input("Enter the number of rows: "))
for i in range(rows, 0, -1):
if i % 2 == 0:
for j in range(i, 0, -1):
print(j, end="")
else:
for j in range(1, i + 1):
print(j, end="")
print()
Output (when user enters 4)
Enter the number of rows: 4
4321
123
21
1
How It Works
1. Same loop core. Only the source of rows changes from a literal to keyboard input.
2. Entering 4 starts even. The first row is length 4, so it opens with 4321.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter the number of rows: ").strip()
try:
rows = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Enter a positive whole number.")
raise SystemExit(1)
Example 3 — start / stop / step
One inner loop — compute direction up front from parity.
Python
rows = 5
for i in range(rows, 0, -1):
start, stop, step = (i, 0, -1) if i % 2 == 0 else (1, i + 1, 1)
for j in range(start, stop, step):
print(j, end="")
print()
Output
12345
4321
123
21
1
How It Works
1. Same zigzag rule. Odd lengths still climb; even lengths still fall.
2. Bounds replace the branch.start, stop, and step encode the two if/else loops in one range.
3. Learn the branch first. Prefer Examples 1–2 in class; use this form once the parity rule is clear.
Edge Cases & Pitfalls
Check these before calling the solution done.
print(i)
print(i, end="") instead of print(j, end="")
That repeats the row length. This pattern prints the sequence value of j.
swapped parity
Odd descends / even ascends
Swapping the branches flips the zigzag and no longer matches 12345 / 4321.
print() inside
Bare print(j) inside the inner loop
That puts each digit on its own line. Use print(j, end="") for digits; call bare print() only after the row finishes.
rows = 1
Single digit
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
multi-digit
rows > 9
print(10, end="") prints two characters. Labs usually stick to 1–9 for a clean single-digit look.
Analysis
Time and Space Complexity
Program
Time
Extra space
if / else loops (Examples 1–2)
O(n²)
O(1)
start / stop / step (Example 3)
O(n²)
O(1)
There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for i = n..1, odd lengths print 1..i; even lengths print i..1.
Print j: the sequence value lives in the inner counter, not the row length.
end= vs print(): digits stay on the line; bare print() advances after each row.
Next step: Program 14 keeps only odd-length descending rows.
One line: shrink i from n to 1; use i % 2 to print 1..i or i..1 with end="", then call print().
Frequently Asked Questions
The program checks whether i is even or odd. For odd i it prints 1..i ascending; for even i it prints i..1 descending, creating a zigzag direction.
The second row from the top has length i = 4, which is even. The even branch runs for j in range(i, 0, -1) and prints 4, 3, 2, 1.
print(j, end="") stays on the same line. print() ends the current line. Digits use end=""; the row break uses print() after the inner loop.
Program 12 repeats the row digit with for j in range(i, rows + 1) (11111, 2222, 333, 44, 5). Program 13 prints sequential digits 1..i or i..1 and alternates direction using i % 2.
O(n²) where n is the number of rows. Total print calls equal n+(n-1)+…+1 = n(n+1)/2.
Yes. Swap the if and else branches so odd rows descend and even rows ascend — the zigzag still works, just mirrored.
Wrap int(input()) in try/except ValueError, or check that the raw string is digits, and require rows >= 1.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Odd row length i prints 1..i; even row length prints i..1. The outer loop shrinks from rows to 1, and i % 2 flips direction — still O(n²) total digit prints.