Python Number Pattern (Rotating Digits)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A rotating number pattern prints a fixed-width row that starts at i, climbs to rows, then wraps back down through i-1..1.

Remember
Rule: print i..rows, then (i-1)..1  (always rows digits)

12345
23451
34521
45321
54321   ← 5 rows

Follows the decreasing continuous triangle in Program 38; next is the alternating 1 and 0 pattern in Program 40.

How to Solve It

One outer loop and two inner loops that rotate the digit sequence — tight digits with end="", then bare print().

MethodIdeaBest for
Forward + wrapPrint i..rows, then i-1..1Learning, interviews, exams
User-input rowsSame logic with a variable widthPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        print j
    for k from i-1 down to 1:
        print k
    print newline

Cheat sheet

GoalPattern
Next rotationfor i in range(1, rows + 1):
Forward segmentfor j in range(i, rows + 1): print(j, end="")
Wrap segmentfor k in range(i - 1, 0, -1): print(k, end="")
End the rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end="") / print(k, end="")Stays on the same lineEach digit (no spaces)
print()Ends the current lineAfter both inner loops

Print digits without a newline, then end the row once.

Live Preview

Change the row count and the rotating pattern updates instantly — each row stays the same width.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 5 digits/row
12345
23451
34521
45321
54321

Worked Walkthrough — rows = 4

Trace the forward climb and the wrap-around. Every row has exactly 4 digits.

iForward / wrapPrinted row
11234 / (none)1234
2234 / 12341
334 / 213421
44 / 3214321

Forward length is rows - i + 1; wrap length is i - 1. Sum = rows.

Python Programs

Three complete programs: fixed rows = 5, input() variant, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Two inner loops per row: forward i..rows, then wrap i-1..1.

Python
rows = 5

for i in range(1, rows + 1):
    for j in range(i, rows + 1):
        print(j, end="")

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

    print()

How It Works

1. Outer loop. i grows from 1 to 5 — each row starts one digit higher.

2. Forward segment. Print j from i to rows (the climb to the top).

3. Wrap segment. Print k from i - 1 down to 1 (skipped when i = 1).

Example 2 — User Input Rows

Read rows with input(), validate, then apply the same forward-and-wrap logic.

Python
try:
    rows = int(input("Enter 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):
    for j in range(i, rows + 1):
        print(j, end="")

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

    print()

How It Works

1. Prompt and validate. Catch ValueError; require rows >= 1 before printing.

2. Same core. Forward + wrap match Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable single-digit output:

Safer input tip
if rows < 1 or rows > 9:
    print("Enter a whole number from 1 to 9.")
    raise SystemExit(1)

Example 3 — Compact rows = 3

Same structure with only three rows — easy to confirm wrap length is i - 1.

Python
rows = 3

for i in range(1, rows + 1):
    for j in range(i, rows + 1):
        print(j, end="")

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

    print()

How It Works

1. Same rules. Forward prints i..rows; wrap prints i-1..1.

2. Quick check. Every row has exactly 3 digits — 123, 231, 321.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong wrap

Wrap ascending instead of descending

Using range(1, i) gives a pure cycle (23451 → 34512). This pattern wraps descending (34521).

newline early

print() between the two halves

That splits the rotation onto two lines. Call bare print() only after both loops finish.

extra spaces

Print a space after each digit

This pattern is tight digits only. Use print(j, end="") with no trailing space.

rows = 1

Single digit

The wrap loop does not run; only the forward loop prints 1.

rows > 9

Multi-digit cells

Values past 9 print as two characters and break the visual rotation. Cap demos at 9.

input()

Catch ValueError

Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Each of n rows prints exactly n digits → n² prints → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Rule: print i..rows, then i-1..1 — each row has exactly rows digits.
  • Wrap: use range(i - 1, 0, -1) so the leftover digits descend to 1.
  • end="" vs print(): digits stay on the line; bare print() advances after both halves.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row i, print i..rows then wrap with i-1..1.

Frequently Asked Questions

For 5 rows: 12345, 23451, 34521, 45321, 54321 — each row starts at the row number and wraps back to 1.
The first loop prints i..rows (forward segment). The second loop prints i-1 down to 1 (wrap segment). Together they always produce rows digits.
After printing 2 3 4 5, the wrap loop prints i-1..1 — for i=2 that prints 1.
Exactly rows digits every time — (rows - i + 1) forward plus (i - 1) wrap = rows.
Program 37 builds palindrome rows (i..2 then 1..i). Program 39 rotates: i..rows then i-1..1.
Program 38 prints a shrinking-width continuous sequence. Program 39 keeps fixed width and rotates the digits.
Use try/except ValueError around int(input()) and require rows >= 1 — see Example 2.
O(n²) for n rows because each row prints n digits.

Did you know?

Each row starts at i, prints i..rows, then wraps with i-1..1. Row i always prints exactly rows digits — total digits = n².

Next: Alternating 1 and 0 Pattern

Continue with a triangle that flips between 1 and 0.

Program 40 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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