Python Palindrome Number Pattern (Increasing-Decreasing)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An increasing-decreasing pyramid prints each row as a palindrome: count up from i to the peak 2i - 1, then back down to i. Row i always has 2i - 1 digits.
Remember
Rule: m = i each row
print ascending i..(2i-1) with m += 1
m -= 2 ← skip the peak
print descending with m -= 1
1
232
34543
4567654
567898765 ← rows = 5
In Python set m = i each row, print the increasing half with print(m, end="") then m += 1, step back with m -= 2, then print the decreasing half before a bare print().
Approach
How to Solve It
Set m = i each row. Print the ascending half, step back with m -= 2, then print the descending half.
Method
Idea
Best for
Two halves + step-back
Ascend with m += 1, m -= 2, descend with m -= 1
Learning, interviews, exams
Rows input
Same logic with a user-chosen height
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows:
m = i
for j from 1 to i:
print m, then m = m + 1
m = m - 2
for k from 1 to (i - 1):
print m, then m = m - 1
print newline
Cheat sheet
Goal
Pattern
Pick each row
for i in range(1, rows + 1):
Start value
m = i
Ascending half
print(m, end=""); m += 1
Skip the peak
m -= 2
Descending half
for k in range(i - 1): print(m, end=""); m -= 1
Digits on row i
2 * i - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(m, end="")
Stays on the same line
Each digit in both halves
print()
Ends the current line
After both inner loops finish a row
Digits use end=""; a bare print() ends the row once.
Try it
Live Preview
Change the row count and the pyramid updates instantly — capped at 5 so every digit stays a single character (peak ≤ 9).
Whole numbers from 1 to 5. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 25 digits
1
232
34543
4567654
567898765
Trace
Worked Walkthrough — Row i = 3
Trace both halves for row 3 — ascending 345, step back, then descending 43.
Step
State
Prints
Start
m = 3
—
Ascend (3 times)
m += 1 prints 3, 4, 5
345
After ascend
m = 6, then m -= 2 → 4
—
Descend (2 times)
m -= 1 prints 4, then 3
43
Full row: 34543. Digits on row i = 2i - 1; total = n² → O(n²).
Code
Python Programs
Three complete programs: fixed rows = 5, user-input rows, and a compact rows = 3 demo. Use View Output for sample results.
Example 1 — Fixed rows = 5
Hard-coded height — ascend, step back with m -= 2, descend.
Python
rows = 5
for i in range(1, rows + 1):
m = i
for j in range(i):
print(m, end="")
m += 1
m -= 2
for k in range(i - 1):
print(m, end="")
m -= 1
print()
Output
1
232
34543
4567654
567898765
How It Works
1. Start each row. Set m = i so row 3 begins at 3, row 5 at 5.
2. Ascend to the peak. Print i digits with m += 1 — up to 2i - 1.
3. Step back, then descend.m -= 2 skips the peak; the second loop prints i - 1 digits going down.
Example 2 — User Input (rows)
Read the row count and build the same palindromic pyramid.
Python
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Please enter a positive whole number.")
raise SystemExit(1)
if rows < 1:
print("Please enter a positive whole number.")
raise SystemExit(1)
for i in range(1, rows + 1):
m = i
for j in range(i):
print(m, end="")
m += 1
m -= 2
for k in range(i - 1):
print(m, end="")
m -= 1
print()
2. Same core. Only the height changes from the literal 5 — both halves stay identical.
3. Single-digit tip. Cap demos so the peak stays a single digit (2 * rows - 1 ≤ 9), i.e. rows ≤ 5.
Example 3 — Compact rows = 3
Same two-half structure with a smaller height for quick paper tracing.
Python
rows = 3
for i in range(1, rows + 1):
m = i
for j in range(i):
print(m, end="")
m += 1
m -= 2
for k in range(i - 1):
print(m, end="")
m -= 1
print()
Output
1
232
34543
How It Works
1. Three rows. Row 1 skips descending; row 2 prints 232; row 3 prints 34543.
2. Trace on paper. Confirm m -= 2 after the peak so the middle digit is not doubled.
Edge Cases & Pitfalls
Check these before calling the solution done.
m - 1
Doubled peak
If you use m -= 1 (or skip the step-back), the peak digit prints twice. Keep m -= 2.
range(i)
Extra descending digit
The descending loop must run i - 1 times: range(i - 1). Using range(i) adds one too many.
print()
Broken rows
If print() sits inside either half (without end=""), each digit lands on its own line. Call bare print() only after both loops.
rows = 1
Single row
Output is just 1 — the descending loop never runs. A good sanity check.
rows > 5
Multi-digit values
Past 5, the peak exceeds 9 and values like 10 break the tight look. Cap demos at 5 or use spaced / fixed-width format.
Bad input
int(input()) raises
Wrap with try/except ValueError so non-numeric input does not crash the script.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
Row i prints 2i - 1 digits. Total = 1 + 3 + 5 + … + (2n - 1) = n². For n = 5 that is 25 digits.
Remember
Key Takeaways
Palindrome row: ascend i..(2i-1), then descend back to i.
Skip the peak: after ascending, m -= 2 so the middle digit is not printed twice.
print vs print(): digits use end=""; bare print() advances after both halves.
Next step: Program 53 prints a mirror diagonal (V-shaped) number pattern.
One line:m = i; print ascending, m -= 2, print descending, then print().
Frequently Asked Questions
Row 3 starts at 3, prints up to 5 (345), then prints back down to 3 (43) after m -= 2 — producing 34543.
Each row counts up from i to the peak 2i-1, then counts back down to i. The sequence reads the same left-to-right on each line.
After the increasing loop, m is one past the peak. Subtracting 2 moves it to the value just before the peak so the decreasing loop does not repeat the peak digit.
Step back with m -= 2 before the decreasing loop. The decreasing loop then runs i-1 times, skipping the peak.
Change rows or read it from user input with int(input()) inside try/except — see Example 2.
O(n²) for n rows because row i prints 2i-1 digits and 1+3+5+...+(2n-1) = n² total prints.
Program 51 uses a continuous counter with alternating direction across rows. Program 52 resets m = i each row and builds a palindromic line per row.
Yes. Print with print(m, end=" ") in both loops and trim trailing space if needed.
Use try/except ValueError around int(input()) so bad input does not crash the script.
One row prints 1 — the decreasing loop range(i - 1) never runs when i = 1.
🤔
Did you know?
Each row is palindromic: print i..(2i-1) ascending, then back down with m -= 2 to skip the peak. Row 3 prints 34543 — total digits = 1+3+5+…+(2n-1) = n² for n rows.