Rotating Number Pattern in Python

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

What You’ll Learn

How to print a rotating number pattern in Python like 12345, 23451, 34521, 45321, 54321.

The trick is to print an ascending part first, then append a descending tail back to 1.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
12345
23451
34521
45321
54321
1

Complete Python Program

The first inner loop prints i..5. The second inner loop prints i-1..1 (descending) to complete the row.

Python
max_num = 5

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

🧠 How It Works

1

Set the maximum number

max_num = 5 controls the width of each row and the number of rows.

Setup
2

Outer loop (rows)

for i in range(1, max_num + 1) picks the starting digit for the row.

Row control
3

Ascending part (i..max)

for j in range(i, max_num + 1) prints digits from the row start to 5.

Ascending
4

Descending tail (i-1..1)

for k in range(i - 1, 0, -1) appends digits back down to 1.

Descending
=

Rotating rows

Each row prints exactly max_num digits. Generalizing to n rows gives O(n²) runtime.

2

Variation — User Input Version

Let the user choose the maximum number (width) at runtime:

Python
max_num = int(input("Enter the maximum number: "))

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

💡 Tips for Enhancement

Try These

  • Validate input (reject max_num < 1) before printing
  • Add spaces between digits for readability
  • Replace digits with letters to create an alphabet variant
  • Print each row as a list instead of using multiple prints
  • Make it cyclic by appending numbers from 1 up to i-1 (instead of descending)

Avoid

  • Forgetting print() after each row
  • Mixing up loop ranges (off-by-one errors change the row length)
  • Hardcoding 5 everywhere (use a variable like max_num)
  • Assuming user input is always valid (wrap int() if needed)

Key Takeaways

1

Use an inner loop to print the ascending part from i to max_num.

2

Use a second inner loop to append a descending tail from i-1 to 1.

3

Each row has constant length (max_num digits) even though the split changes.

4

Generalized runtime is O(n²) for n rows.

❓ Frequently Asked Questions

Because after printing 3 4 5, the second loop prints 2 1 (descending from i-1 to 1).
Yes. Instead of printing i-1..1 descending, print 1..i-1 ascending to complete a cycle.
Set max_num to a new value (like 7). The loops will adjust accordingly.
O(n²) if generalized to n, since each of the n rows prints n digits.

Explore More Python Number Patterns!

From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.

All Number Patterns →
Did you know?

Patterns like this are often built by concatenating two sequences: an ascending prefix and a descending suffix. Thinking in halves makes many pattern problems easier.

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