Rotating Number Pattern in Python

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

What You’ll Learn

How to print a rotating number pattern in Python. Each row begins with the row number and then continues with 1, 2, 3... to keep the row length constant.

This pattern is a nice way to practice printing a row using two loops: one for the left part and one for the remaining part.

⭐ Pattern Output

For n = 5, the pattern looks like this:

Output
12345
21234
32123
43212
54321
1

Complete Python Program

Print the left part in descending order from i to 2, then print the remaining part from 1 upwards.

Python
n = 5

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

🧠 How It Works

1

Set the size

n = 5 defines the number of rows and the width of each row.

Setup
2

Outer loop controls the row

for i in range(1, n + 1) prints rows 1 through 5.

Row control
3

Left part (descending)

for j in range(i, 1, -1) prints the row start from i down to 2.

Left segment
4

Right part (ascending)

for k in range(1, n + 2 - i) prints the remaining digits from 1 upward to complete the row.

Right segment
=

Rotating sequence

Each row prints exactly n digits. Runtime is O(n²) because we print \(n\) digits for \(n\) rows.

2

Variation — User Input Version

Let the user choose n at runtime and print the same rotating pattern.

Python
n = int(input("Enter n: "))
if n < 1:
    raise ValueError("n must be at least 1")

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

💡 Tips for Enhancement

Try These

  • Print the same idea with letters (A, B, C...) instead of numbers
  • Add spaces between digits for readability
  • Create a left-rotating version by printing i..n then 1..i-1
  • Try making it a square matrix output with separators
  • Use string slicing (advanced) to rotate a base string

Avoid

  • Forgetting the second loop (the row won’t reach full width)
  • Using n too large without spacing (digits will run together)
  • Accepting invalid input without checks
  • Mixing 0-based and 1-based indices in the ranges

Key Takeaways

1

Each row is built in two parts: a descending start and an ascending finish.

2

Row i starts with i and ends at a fixed width n.

3

All rows have the same length, making it look like rotation.

4

Printing \(n\) digits for \(n\) rows gives O(n²) output.

❓ Frequently Asked Questions

It’s implemented with two loops, but the output resembles a rotation of the base sequence 12345.
Print i..n first, then 1..i-1 to get a left-rotated look.
Numbers become multi-digit and the pattern may look cramped. Use spacing or formatting for readability.
O(n²), because the program prints \(n\) values per row for \(n\) rows.

Explore More Python Number Patterns!

Rotations and shifts are a fun bridge between pattern printing and array manipulation.

All Number Patterns →
Did you know?

You can also generate this pattern by rotating a base string like "12345" using slicing, e.g. s[i:]+s[:i] (advanced approach).

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.

7 people found this page helpful