Decreasing Repeated Numbers Pattern in Python

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print this decreasing repeated number pattern in Python:

  • 11111
  • 2222
  • 333
  • 22
  • 1

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:

Output
11111
2222
333
22
1
1

Complete 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).

Python
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

1

Outer loop selects the row

for i in range(1, 6) creates 5 rows and decides which digit will be repeated.

Row control
2

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.

Width
3

Mirror the digit after 3

If i < 4, print i. Otherwise print 6 - i which produces 2 then 1.

Logic
4

New line per row

print() moves to the next row after each line.

Line break
=

Decreasing repeated digits

Total digits printed for 5 rows: 5+4+3+2+1 = 15, so time complexity is O(n²) for \(n\) rows.

2

Variation — User Input Version

Choose the number of rows. This version mirrors the digit after the midpoint while decreasing the width each row.

Python
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

1

Row width decreases from rows down to 1.

2

The printed digit mirrors back down after a midpoint.

3

The inner loop handles repetition; the if selects the digit.

4

Total characters printed grows quadratically, so runtime is O(n²).

❓ Frequently Asked Questions

With 5 rows, 6 - i turns i = 4 into 2 and i = 5 into 1, mirroring the digit values while the row width continues decreasing.
Change the print to print(value, end=" ") and ensure your expected output includes spaces.
Yes. Use a different digit-selection rule and decide whether the width should keep decreasing or change direction. The same nested-loop structure still applies.
O(n²) for n rows, since total printed characters are proportional to 1+2+…+n.

Explore More Python Number Patterns!

Practice loops and conditional logic with more number pattern programs.

All Number Patterns →
Did you know?

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.

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