Decreasing Repeated Numbers Pattern in Python

What You’ll Learn
How to print this decreasing repeated number pattern in Python:
111112222333221
The number of digits decreases each row, and the printed digit mirrors back down after reaching 3.
⭐ Pattern Output
For 5 rows, the pattern looks like this:
11111
2222
333
22
1Complete Python Program
The inner loop controls the row width (5 down to 1). The if chooses whether to print i (first half) or 6 - i (second half).
for i in range(1, 6):
for j in range(i, 6):
if i < 4:
print(i, end="")
else:
print(6 - i, end="")
print()🧠 How It Works
Outer loop selects the row
for i in range(1, 6) creates 5 rows and decides which digit will be repeated.
Inner loop controls width
for j in range(i, 6) runs 5, 4, 3, 2, 1 times as i increases, which shrinks the row each step.
Mirror the digit after 3
If i < 4, print i. Otherwise print 6 - i which produces 2 then 1.
New line per row
print() moves to the next row after each line.
Decreasing repeated digits
Total digits printed for 5 rows: 5+4+3+2+1 = 15, so time complexity is O(n²) for \(n\) rows.
Variation — User Input Version
Choose the number of rows. This version mirrors the digit after the midpoint while decreasing the width each row.
rows = int(input("Enter the number of rows: "))
for i in range(1, rows + 1):
for j in range(i, rows + 1):
if i < (rows + 1) / 2:
print(i, end="")
else:
print(rows + 1 - i, end="")
print()💡 Tips for Enhancement
Try These
- Validate input (reject
rows < 1) before printing - Print spaces between digits (then update the output)
- Use a different mirror rule (like 1,2,3,4,3,2,1) for longer patterns
- Build each row as a string for faster output when rows are large
- Replace digits with letters for similar alphabet patterns
Avoid
- Forgetting the newline
print()after each row - Changing the width loop without updating expected output
- Using floating comparisons without understanding the midpoint logic
- Assuming user input is always valid (wrap
int()conversion if needed)
Key Takeaways
Row width decreases from rows down to 1.
The printed digit mirrors back down after a midpoint.
The inner loop handles repetition; the if selects the digit.
Total characters printed grows quadratically, so runtime is O(n²).
❓ Frequently Asked Questions
6 - i turns i = 4 into 2 and i = 5 into 1, mirroring the digit values while the row width continues decreasing.print(value, end=" ") and ensure your expected output includes spaces.Explore More Python Number Patterns!
Practice loops and conditional logic with more number pattern programs.
Patterns that “switch” values after a midpoint often use a mirror expression like rows + 1 - i or max + 1 - i to reverse direction without changing the loop.
12 people found this page helpful
